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

declare(strict_types=1);

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

use App\Application\LiveComponents\TabsComponent;

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

try {
    $component = new TabsComponent(
        id: 'tabs-test',
        initialData: [
            'tabs' => [
                ['id' => 'tab1', 'title' => 'Tab 1', 'content' => 'Content 1', 'closable' => true],
                ['id' => 'tab2', 'title' => 'Tab 2', 'content' => 'Content 2', 'closable' => true],
            ],
            'active_tab' => 'tab1',
            'max_tabs' => 10,
            'allow_close' => true,
            'allow_add' => true,
        ]
    );

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

    // Test switchTab() method
    echo "Testing switchTab() method...\n";
    $result = $component->switchTab('tab2');
    echo "  ✓ switchTab() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - Active tab: " . $result['active_tab'] . "\n";
    echo "  - Keys: " . implode(', ', array_keys($result)) . "\n\n";

    // Test addTab() method
    echo "Testing addTab() method...\n";
    $result = $component->addTab('New Tab', 'New content');
    echo "  ✓ addTab() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - Tab count: " . count($result['tabs']) . "\n";
    echo "  - New active tab: " . $result['active_tab'] . "\n\n";

    // Test closeTab() method
    $component2 = new TabsComponent(
        id: 'tabs-test-2',
        initialData: [
            'tabs' => [
                ['id' => 'tab1', 'title' => 'Tab 1', 'content' => 'Content 1', 'closable' => true],
                ['id' => 'tab2', 'title' => 'Tab 2', 'content' => 'Content 2', 'closable' => true],
            ],
            'active_tab' => 'tab1',
        ]
    );

    echo "Testing closeTab() method...\n";
    $result = $component2->closeTab('tab1');
    echo "  ✓ closeTab() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - Tab count: " . count($result['tabs']) . "\n";
    echo "  - Active tab after close: " . $result['active_tab'] . "\n\n";

    // Test updateTabContent() method
    echo "Testing updateTabContent() method...\n";
    $result = $component->updateTabContent('tab1', 'Updated content');
    echo "  ✓ updateTabContent() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - Updated: " . ($result['tabs'][0]['updated_at'] !== null ? 'YES' : 'NO') . "\n\n";

    // Test updateTabTitle() method
    echo "Testing updateTabTitle() method...\n";
    $result = $component->updateTabTitle('tab1', 'Updated Title');
    echo "  ✓ updateTabTitle() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - New title: " . $result['tabs'][0]['title'] . "\n\n";

    // Test reorderTabs() method
    echo "Testing reorderTabs() method...\n";
    $result = $component->reorderTabs(['tab2', 'tab1']);
    echo "  ✓ reorderTabs() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - First tab ID: " . $result['tabs'][0]['id'] . "\n\n";

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

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