<?php

namespace App\Modules\PhoneNumberVerification\Rules;

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

class PhoneNumberVerificationRule implements ValidationRule
{
    /**
     * Run the validation rule.
     *
     * @param  \Closure(string, ?string=): \Illuminate\Translation\PotentiallyTranslatedString  $fail
     */
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        $value = preg_replace('/\D/', '', $value); // @phpstan-ignore-line

        if (! is_numeric($value)) {
            $fail('Telefon belgisi diňe sanlardan ybarat bolmaly.');

            return;
        }

        $number = (int) $value;

        $isValid = ($number >= 61000000 && $number <= 65999999) || ($number >= 71000000 && $number <= 71999999);

        if (! $isValid) {
            $fail('Telefon belgisi nädogry aralykda.');
        }
    }
}
