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

declare(strict_types=1);

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

use App\Application\LiveComponents\LivePresenceComponent;

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

try {
    $component = new LivePresenceComponent(
        id: 'live-presence-test',
        initialData: [
            'users' => [
                [
                    'user_id' => 'user1',
                    'user_name' => 'John Doe',
                    'avatar_url' => null,
                    'status' => 'online',
                    'joined_at' => time(),
                    'last_seen' => time(),
                ],
            ],
            'show_avatars' => true,
            'show_names' => true,
            'show_count' => true,
            'max_visible_users' => 10,
            'presence_timeout' => 300,
        ]
    );

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

    // Test userJoined() method
    echo "Testing userJoined() method...\n";
    $result = $component->userJoined('user2', 'Jane Doe', 'https://example.com/avatar.jpg');
    echo "  ✓ userJoined() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - User count: " . count($result['users']) . "\n";
    echo "  - Keys: " . implode(', ', array_keys($result)) . "\n\n";

    // Test userLeft() method
    echo "Testing userLeft() method...\n";
    $result = $component->userLeft('user1');
    echo "  ✓ userLeft() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - User count: " . count($result['users']) . "\n\n";

    // Test updateActivity() method
    $component2 = new LivePresenceComponent(
        id: 'live-presence-test-2',
        initialData: [
            'users' => [
                [
                    'user_id' => 'user1',
                    'user_name' => 'John Doe',
                    'status' => 'away',
                    'last_seen' => time() - 100,
                ],
            ],
        ]
    );

    echo "Testing updateActivity() method...\n";
    $result = $component2->updateActivity('user1');
    echo "  ✓ updateActivity() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - Status updated: " . ($result['users'][0]['status'] === 'online' ? 'YES' : 'NO') . "\n\n";

    // Test updateStatus() method
    echo "Testing updateStatus() method...\n";
    $result = $component2->updateStatus('user1', 'busy');
    echo "  ✓ updateStatus() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - New status: " . $result['users'][0]['status'] . "\n\n";

    // Test cleanupInactive() method
    $component3 = new LivePresenceComponent(
        id: 'live-presence-test-3',
        initialData: [
            'users' => [
                ['user_id' => 'active', 'last_seen' => time()],
                ['user_id' => 'inactive', 'last_seen' => time() - 500],
            ],
            'presence_timeout' => 300,
        ]
    );

    echo "Testing cleanupInactive() method...\n";
    $result = $component3->cleanupInactive();
    echo "  ✓ cleanupInactive() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - User count after cleanup: " . count($result['users']) . "\n";
    echo "  - Inactive users removed: " . (count($result['users']) === 1 ? 'YES' : 'NO') . "\n\n";

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

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