<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class IndianMobile implements ValidationRule
{
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        $raw = trim((string) $value);
        $digits = preg_replace('/\D/', '', $raw);

        // Accept:
        // - 10 digits (e.g., 9876543210)  [common when +91 is shown as a UI prefix]
        // - +91XXXXXXXXXX / 91XXXXXXXXXX
        // - 0XXXXXXXXXX (sometimes pasted with leading 0)
        if (preg_match('/^(?:91|0)?([6-9]\d{9})$/', $digits)) {
            return;
        }

        $fail('The :attribute must be a valid Indian mobile number (10 digits, e.g., 9876543210).');
    }
}
