<?php

namespace App\Rules;

use App\Facades\Otp\Otp;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Translation\PotentiallyTranslatedString;

class VerificationCode implements ValidationRule
{
    public function __construct(protected string $phone_number){}

    /**
     * Run the validation rule.
     *
     * @param  \Closure(string): PotentiallyTranslatedString  $fail
     */
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        $verified = Otp::verifyOtpToken($this->phone_number, $value);

        if (!$verified) {
            $fail('The :attribute is not valid.')->translate();
        }
    }
}
