<?php

declare(strict_types=1);

namespace App\Rules\SignalIdentifier;

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

/**
 * Валидатор signal_identifier для signal_type=site.
 *
 * Требует валидный домен в нижнем регистре, без протокола/пути.
 * Spec: docs/superpowers/specs/2026-05-10-supplier-integration-design.md §3.1
 */
class DomainIdentifier implements ValidationRule
{
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        if (! is_string($value) || $value === '') {
            $fail('Source domain is required.');

            return;
        }

        if (! preg_match('/^[a-z0-9-]+(\.[a-z0-9-]+)+$/', $value)) {
            $fail('Source domain must be lowercase, only letters/digits/hyphens, with a TLD (no protocol/path).');
        }
    }
}
