#!/usr/bin/env php
<?php

declare(strict_types=1);

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

use App\Framework\LiveComponents\ValueObjects\ComponentId;

echo "Testing ComponentId Value Object\n";
echo "=================================\n\n";

try {
    // Test 1: Create from string
    echo "Test 1: Create from valid string format\n";
    $id = ComponentId::fromString('counter:demo');
    echo "  ✓ Name: " . $id->name . "\n";
    echo "  ✓ Instance ID: " . $id->instanceId . "\n";
    echo "  ✓ toString(): " . $id->toString() . "\n\n";

    // Test 2: Generate unique IDs
    echo "Test 2: Generate unique component IDs\n";
    $id1 = ComponentId::generate('search');
    $id2 = ComponentId::generate('search');
    echo "  ✓ ID1 name: " . $id1->name . "\n";
    echo "  ✓ ID2 name: " . $id2->name . "\n";
    echo "  ✓ IDs are unique: " . ($id1->instanceId !== $id2->instanceId ? 'YES' : 'NO') . "\n\n";

    // Test 3: Create with specific instance ID
    echo "Test 3: Create with specific instance ID\n";
    $id = ComponentId::create('modal', 'main-dialog');
    echo "  ✓ Name: " . $id->name . "\n";
    echo "  ✓ Instance ID: " . $id->instanceId . "\n";
    echo "  ✓ toString(): " . $id->toString() . "\n\n";

    // Test 4: __toString magic method
    echo "Test 4: Convert to string with __toString\n";
    $id = ComponentId::fromString('tabs:settings');
    echo "  ✓ String: " . ((string) $id) . "\n\n";

    // Test 5: Equality check
    echo "Test 5: Check equality correctly\n";
    $id1 = ComponentId::fromString('counter:demo');
    $id2 = ComponentId::fromString('counter:demo');
    $id3 = ComponentId::fromString('counter:other');
    echo "  ✓ id1 equals id2: " . ($id1->equals($id2) ? 'YES' : 'NO') . "\n";
    echo "  ✓ id1 not equals id3: " . (! $id1->equals($id3) ? 'YES' : 'NO') . "\n\n";

    // Test 6: Check component name
    echo "Test 6: Check component name\n";
    $id = ComponentId::fromString('search:main-form');
    echo "  ✓ Has name 'search': " . ($id->hasName('search') ? 'YES' : 'NO') . "\n";
    echo "  ✓ Does not have name 'counter': " . (! $id->hasName('counter') ? 'YES' : 'NO') . "\n\n";

    // Test 7: Valid name formats
    echo "Test 7: Accept valid name formats\n";
    $validNames = [
        'counter',
        'search-component',
        'data_table',
        'tab1',
        'live-component_v2',
    ];
    foreach ($validNames as $name) {
        $id = ComponentId::create($name, 'test');
        echo "  ✓ Valid name '{$name}': " . $id->name . "\n";
    }
    echo "\n";

    // Test 8: Complex instance IDs
    echo "Test 8: Handle complex instance IDs\n";
    $instanceId = 'user-123_session-abc.def';
    $id = ComponentId::create('profile', $instanceId);
    echo "  ✓ Instance ID: " . $id->instanceId . "\n";
    echo "  ✓ Full ID: " . $id->toString() . "\n\n";

    // Test 9: Parse ID with multiple colons
    echo "Test 9: Parse component ID with multiple colons\n";
    $id = ComponentId::fromString('component:instance:with:colons');
    echo "  ✓ Name: " . $id->name . "\n";
    echo "  ✓ Instance ID: " . $id->instanceId . "\n\n";

    // Test 10: Exception for empty ID
    echo "Test 10: Throw exception for empty ID\n";

    try {
        ComponentId::fromString('');
        echo "  ✗ Should have thrown exception\n";
    } catch (InvalidArgumentException $e) {
        echo "  ✓ Exception thrown: " . $e->getMessage() . "\n";
    }
    echo "\n";

    // Test 11: Exception for invalid format
    echo "Test 11: Throw exception for invalid format\n";

    try {
        ComponentId::fromString('invalid-format');
        echo "  ✗ Should have thrown exception\n";
    } catch (InvalidArgumentException $e) {
        echo "  ✓ Exception thrown: " . $e->getMessage() . "\n";
    }
    echo "\n";

    // Test 12: Exception for empty name
    echo "Test 12: Throw exception for empty name\n";

    try {
        ComponentId::fromString(':instance');
        echo "  ✗ Should have thrown exception\n";
    } catch (InvalidArgumentException $e) {
        echo "  ✓ Exception thrown: " . $e->getMessage() . "\n";
    }
    echo "\n";

    // Test 13: Exception for empty instance ID
    echo "Test 13: Throw exception for empty instance ID\n";

    try {
        ComponentId::fromString('component:');
        echo "  ✗ Should have thrown exception\n";
    } catch (InvalidArgumentException $e) {
        echo "  ✓ Exception thrown: " . $e->getMessage() . "\n";
    }
    echo "\n";

    // Test 14: Exception for invalid name characters
    echo "Test 14: Throw exception for invalid name characters\n";

    try {
        ComponentId::create('invalid/name', 'instance');
        echo "  ✗ Should have thrown exception\n";
    } catch (InvalidArgumentException $e) {
        echo "  ✓ Exception thrown: " . $e->getMessage() . "\n";
    }
    echo "\n";


    echo "=================================\n";
    echo "✅ All ComponentId tests passed!\n";

} catch (\Throwable $e) {
    echo "❌ Error: " . $e->getMessage() . "\n";
    echo "   File: " . $e->getFile() . ":" . $e->getLine() . "\n";
    exit(1);
}
