<?php

declare(strict_types=1);

namespace App\Framework\Validation\Rules;

use App\Domain\Common\ValueObject\PhoneNumber;
use App\Framework\Validation\ValidationRule;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
final readonly class Phone implements ValidationRule
{
    public function __construct(
        private string $message = 'The field must be a valid phone number.'
    ) {
    }

    public function validate(mixed $value): bool
    {
        if ($value === null || $value === '') {
            return true; // Allow empty values (use Required rule for mandatory fields)
        }

        if (! is_string($value)) {
            return false;
        }

        return PhoneNumber::isValid($value);
    }

    public function getErrorMessages(): array
    {
        return [$this->message];
    }
}
