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

declare(strict_types=1);

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

use App\Application\LiveComponents\SearchComponent;

echo "Testing SearchComponent Migration\n";
echo "==================================\n\n";

try {
    $search = new SearchComponent(
        id: 'search-test',
        initialData: []
    );

    echo "✓ SearchComponent created successfully\n";
    echo "  - ID: " . $search->getId() . "\n\n";

    // Test search() method
    echo "Testing search() method...\n";
    $result = $search->search('test', 'all');
    echo "  ✓ search() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - Keys: " . implode(', ', array_keys($result)) . "\n";
    echo "  - Query: " . $result['query'] . "\n";
    echo "  - Target: " . $result['target'] . "\n";
    echo "  - Result count: " . $result['result_count'] . "\n\n";

    // Test clear() method
    echo "Testing clear() method...\n";
    $result = $search->clear();
    echo "  ✓ clear() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - Keys: " . implode(', ', array_keys($result)) . "\n";
    echo "  - Query: '" . $result['query'] . "'\n";
    echo "  - Result count: " . $result['result_count'] . "\n\n";

    // Test changeTarget() method
    echo "Testing changeTarget() method...\n";
    $result = $search->changeTarget('users');
    echo "  ✓ changeTarget() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - Keys: " . implode(', ', array_keys($result)) . "\n";
    echo "  - Target: " . $result['target'] . "\n\n";

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

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