<?php

declare(strict_types=1);

namespace App\Framework\TypeCaster\Casters;

use App\Domain\ValueObjects\EmailAddress;
use App\Framework\TypeCaster\TypeCasterInterface;
use InvalidArgumentException;

final readonly class EmailCaster implements TypeCasterInterface
{
    public function fromString(string $value): EmailAddress
    {
        if (empty($value)) {
            throw new InvalidArgumentException('Email value cannot be empty');
        }

        return new EmailAddress($value);
    }

    public function toString(mixed $value): string
    {
        if (! $value instanceof EmailAddress) {
            throw new InvalidArgumentException('Value must be an EmailAddress instance');
        }

        return $value->value;
    }

    public function supports(string $type): bool
    {
        return $type === EmailAddress::class;
    }

    public function getPattern(): string
    {
        // Simple pattern for routes - detailed validation is in EmailAddress constructor
        return '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}';
    }
}
