<?php

namespace App\Rules\User;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class Google2FaSecretKeyVerificationRule implements ValidationRule
{
    /**
     * Run the validation rule.
     *
     * @param  \Closure(string, ?string=): \Illuminate\Translation\PotentiallyTranslatedString  $fail
     */

     protected $google2FaServices;
     protected $key;
     public function __construct($google2FaServices, $key)
     {
        $this->google2FaServices = $google2FaServices;
        $this->key = $key;
     }

    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        $valid = $this->google2FaServices->verifyKey($this->key , $value);
        if(!$valid){
            $fail('OTP is invalid');
        }
    }
}
