<?php

declare(strict_types=1);

namespace App\Framework\TypeCaster\Casters;

use App\Framework\Core\ValueObjects\Hash;
use App\Framework\TypeCaster\TypeCasterInterface;
use InvalidArgumentException;

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

        return Hash::fromString($value);
    }

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

        return $value->toString();
    }

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

    public function getPattern(): string
    {
        // Hash pattern: typically SHA256 (64 hex characters) or other hex patterns
        return '[a-fA-F0-9]+';
    }
}
