<?php

declare(strict_types=1);

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

use App\Framework\Core\AppBootstrapper;
use App\Framework\Performance\EnhancedPerformanceCollector;
use App\Framework\DateTime\SystemClock;
use App\Framework\DateTime\SystemHighResolutionClock;
use App\Framework\Performance\MemoryMonitor;
use App\Framework\LiveComponents\ComponentRegistry;
use App\Framework\LiveComponents\ValueObjects\ComponentId;
use App\Application\LiveComponents\Counter\CounterState;
use App\Application\LiveComponents\Search\SearchState;

echo "=== LiveComponent State Refactoring Test ===\n\n";

try {
    // Bootstrap application
    $basePath = dirname(__DIR__, 2);
    $clock = new SystemClock();
    $highResClock = new SystemHighResolutionClock();
    $memoryMonitor = new MemoryMonitor();
    $collector = new EnhancedPerformanceCollector($clock, $highResClock, $memoryMonitor, enabled: false);
    $bootstrapper = new AppBootstrapper($basePath, $collector, $memoryMonitor);
    $app = $bootstrapper->bootstrapWeb();

    // Get container from app
    $container = $app->getContainer();
    $registry = $container->get(ComponentRegistry::class);

    // Test 1: Create Counter with empty state
    echo "Test 1: Create Counter with empty state\n";
    $counterId = ComponentId::create('counter', 'test-1');
    $counter = $registry->resolve($counterId, null);

    echo "✓ Counter created successfully\n";
    echo "  State type: " . get_class($counter->state) . "\n";
    echo "  Count: " . $counter->state->count . "\n";
    echo "  ID: " . $counter->getId()->toString() . "\n\n";

    // Test 2: Create Counter with existing state
    echo "Test 2: Create Counter with existing state\n";
    $stateData = ['count' => 42, 'last_update' => '12:34:56', 'render_count' => 5];
    $counter2 = $registry->resolve($counterId, $stateData);

    echo "✓ Counter created with state\n";
    echo "  State type: " . get_class($counter2->state) . "\n";
    echo "  Count: " . $counter2->state->count . "\n";
    echo "  Last Update: " . $counter2->state->lastUpdate . "\n\n";

    // Test 3: Test Counter actions
    echo "Test 3: Test Counter actions\n";
    $newState = $counter2->increment();
    echo "✓ Increment action works\n";
    echo "  New count: " . $newState->count . "\n";
    echo "  Return type: " . get_class($newState) . "\n\n";

    // Test 4: Create Search with empty state
    echo "Test 4: Create Search with empty state\n";
    $searchId = ComponentId::create('search', 'test-1');
    $search = $registry->resolve($searchId, null);

    echo "✓ Search created successfully\n";
    echo "  State type: " . get_class($search->state) . "\n";
    echo "  Query: '" . $search->state->query . "'\n";
    echo "  Target: " . $search->state->target . "\n\n";

    // Test 5: Create Search with existing state
    echo "Test 5: Create Search with existing state\n";
    $searchData = [
        'query' => 'test query',
        'target' => 'users',
        'results' => ['user1', 'user2'],
        'result_count' => 2,
        'execution_time_ms' => 15.5,
        'timestamp' => time()
    ];
    $search2 = $registry->resolve($searchId, $searchData);

    echo "✓ Search created with state\n";
    echo "  State type: " . get_class($search2->state) . "\n";
    echo "  Query: '" . $search2->state->query . "'\n";
    echo "  Result count: " . $search2->state->resultCount . "\n\n";

    // Test 6: Test Search actions
    echo "Test 6: Test Search actions\n";
    $newSearchState = $search2->search('new query', 'products');
    echo "✓ Search action works\n";
    echo "  New query: '" . $newSearchState->query . "'\n";
    echo "  Return type: " . get_class($newSearchState) . "\n\n";

    // Test 7: Verify State implements LiveComponentState
    echo "Test 7: Verify State implements LiveComponentState\n";
    echo "  CounterState::fromArray: " . (method_exists(CounterState::class, 'fromArray') ? '✓' : '✗') . "\n";
    echo "  CounterState::toArray: " . (method_exists(CounterState::class, 'toArray') ? '✓' : '✗') . "\n";
    echo "  SearchState::fromArray: " . (method_exists(SearchState::class, 'fromArray') ? '✓' : '✗') . "\n";
    echo "  SearchState::toArray: " . (method_exists(SearchState::class, 'toArray') ? '✓' : '✗') . "\n\n";

    echo "=== All Tests Passed! ===\n";

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