<?php

declare(strict_types=1);

require_once __DIR__ . '/../../vendor/autoload.php';

use App\Domain\ValueObjects\EmailAddress;
use App\Framework\Core\ValueObjects\Hash;
use App\Framework\DI\DefaultContainer;
use App\Framework\Router\ValueObjects\RoutePath;
use App\Framework\Router\ValueObjects\TypedPlaceholder;
use App\Framework\TypeCaster\Casters\EmailCaster;
use App\Framework\TypeCaster\Casters\HashCaster;
use App\Framework\TypeCaster\TypeCasterRegistry;

echo "=== Testing Typed Route Parameters with TypeCasters ===\n\n";

// Setup TypeCaster Registry
$container = new DefaultContainer();
$registry = new TypeCasterRegistry($container);

// Register some casters
$emailCaster = new EmailCaster();
$registry->register(EmailCaster::class, $emailCaster);

$hashCaster = new HashCaster();
$registry->register(HashCaster::class, $hashCaster);

echo "1. Creating TypedPlaceholder for EmailAddress:\n";

try {
    $emailPlaceholder = TypedPlaceholder::for(
        EmailAddress::class,
        $registry,
        'userEmail'
    );

    echo "   ✅ Created typed placeholder: {$emailPlaceholder->toString()}\n";
    echo "   📧 Pattern: {$emailPlaceholder->getPattern()}\n";

    // Test conversion
    $testEmail = 'user@example.com';
    $convertedEmail = $emailPlaceholder->convertValue($testEmail);
    echo "   🔄 Converted '{$testEmail}' to: " . get_class($convertedEmail) . "\n";
    echo "   📋 Value: {$convertedEmail->value}\n\n";

} catch (\Throwable $e) {
    echo "   ❌ Error: {$e->getMessage()}\n\n";
}

echo "2. Creating TypedPlaceholder for Hash:\n";

try {
    $hashPlaceholder = TypedPlaceholder::for(
        Hash::class,
        $registry,
        'fileHash'
    );

    echo "   ✅ Created typed placeholder: {$hashPlaceholder->toString()}\n";
    echo "   🔐 Pattern: {$hashPlaceholder->getPattern()}\n";

    // Test conversion with proper SHA256 hash (64 characters)
    $testHash = 'a1b2c3d4e5f67890123456789012345678901234567890123456789012345678';
    $convertedHash = $hashPlaceholder->convertValue($testHash);
    echo "   🔄 Converted hash to: " . get_class($convertedHash) . "\n";
    echo "   📋 Value: {$convertedHash->toString()}\n\n";

} catch (\Throwable $e) {
    echo "   ❌ Error: {$e->getMessage()}\n\n";
}

echo "3. Creating RoutePath with TypedPlaceholders:\n";

try {
    $routePath = RoutePath::fromElements(
        'api',
        'users',
        TypedPlaceholder::for(EmailAddress::class, $registry, 'email'),
        'files',
        TypedPlaceholder::for(Hash::class, $registry, 'hash')
    );

    echo "   ✅ Created route: {$routePath->toString()}\n";
    echo "   🎯 Is dynamic: " . ($routePath->isDynamic() ? 'Yes' : 'No') . "\n";
    echo "   📝 Parameters: " . implode(', ', $routePath->getParameterNames()) . "\n\n";

} catch (\Throwable $e) {
    echo "   ❌ Error: {$e->getMessage()}\n\n";
}

echo "4. Testing unsupported type (should fail gracefully):\n";

try {
    $unsupportedPlaceholder = TypedPlaceholder::for(
        'NonExistentClass',
        $registry
    );
    echo "   ❌ This should not succeed!\n";
} catch (\Throwable $e) {
    echo "   ✅ Expected error: {$e->getMessage()}\n\n";
}

echo "=== Typed Route Parameters Test Completed ===\n";
