<?php

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

// Debug das TUI Navigation Problem
use App\Framework\Console\Components\TuiState;
use App\Framework\Console\TuiView;
use App\Framework\Console\CommandGroupRegistry;
use App\Framework\Discovery\Results\DiscoveryRegistry;

echo "=== TUI NAVIGATION DEBUG ===\n";

try {
    // Mock Discovery Registry
    $discoveryRegistry = new class implements DiscoveryRegistry {
        public function interfaces() {
            return new class {
                public function get(string $interface) { return []; }
            };
        }
        public function attributes() {
            return new class {
                public function get(string $attributeClass) { return []; }
            };
        }
    };

    $groupRegistry = new CommandGroupRegistry($discoveryRegistry);
    $categories = $groupRegistry->getOrganizedCommands();

    echo "📊 Categories loaded: " . count($categories) . "\n";
    foreach ($categories as $index => $category) {
        echo "  [$index] {$category['name']} - Commands: " . count($category['commands']) . "\n";
    }
    echo "\n";

    // Test TuiState Navigation
    $state = new TuiState();
    $state->setCategories($categories);
    $state->setCurrentView(TuiView::CATEGORIES);

    echo "🔍 Initial TUI State:\n";
    echo "  Selected Category: " . $state->getSelectedCategory() . "\n";
    echo "  Current View: " . $state->getCurrentView()->name . "\n";
    echo "  Current Category: " . ($state->getCurrentCategory()['name'] ?? 'NULL') . "\n";
    echo "\n";

    // Test Navigation Up/Down
    echo "🧪 Testing Navigation:\n";

    for ($i = 0; $i < 3; $i++) {
        echo "Step $i - Before navigateDown(): " . $state->getSelectedCategory() . "\n";
        $state->navigateDown();
        echo "Step $i - After navigateDown(): " . $state->getSelectedCategory() . "\n";
    }

    echo "\n";

    for ($i = 0; $i < 5; $i++) {
        echo "Step $i - Before navigateUp(): " . $state->getSelectedCategory() . "\n";
        $state->navigateUp();
        echo "Step $i - After navigateUp(): " . $state->getSelectedCategory() . "\n";
    }

    echo "\n";

    // Test Category Bounds
    echo "🎯 Testing Bounds:\n";
    $state->setSelectedCategory(99);
    echo "Set to 99, actual: " . $state->getSelectedCategory() . "\n";
    $state->setSelectedCategory(-5);
    echo "Set to -5, actual: " . $state->getSelectedCategory() . "\n";

    echo "\n✅ Navigation logic test completed\n";

} catch (\Throwable $e) {
    echo "❌ ERROR: " . $e->getMessage() . "\n";
    echo "File: " . $e->getFile() . ":" . $e->getLine() . "\n";
    echo "Stack trace:\n" . $e->getTraceAsString() . "\n";
}