<?php

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

// Test TUI navigation functionality
use App\Framework\Console\Components\TuiState;
use App\Framework\Console\Components\TuiInputHandler;
use App\Framework\Console\Components\TuiCommandExecutor;
use App\Framework\Console\CommandHistory;
use App\Framework\Console\TuiView;
use App\Framework\Console\TuiKeyCode;

echo "Testing TUI Navigation...\n";
echo "=========================\n\n";

try {
    // Create mock executor
    $mockExecutor = new class implements \App\Framework\Console\Components\TuiCommandExecutor {
        public function executeSelectedCommand(object $command): void {
            echo "Mock: Execute command\n";
        }
        public function executeCommand(string $commandName): void {
            echo "Mock: Execute command: $commandName\n";
        }
        public function validateSelectedCommand(object $command): void {
            echo "Mock: Validate command\n";
        }
        public function validateCommand(string $commandName): void {
            echo "Mock: Validate command: $commandName\n";
        }
        public function showSelectedCommandHelp(object $command): void {
            echo "Mock: Show help\n";
        }
        public function showCommandHelp(string $commandName): void {
            echo "Mock: Show help: $commandName\n";
        }
        public function showAllCommandsHelp(): void {
            echo "Mock: Show all commands help\n";
        }
        public function startInteractiveForm(object $command, \App\Framework\Console\Components\TuiState $state): void {
            echo "Mock: Start form\n";
        }
        public function findCommandObject(string $commandName): ?object {
            return null;
        }
    };

    $state = new TuiState();
    $history = new CommandHistory();
    $inputHandler = new TuiInputHandler($mockExecutor);

    // Setup test categories
    $categories = [
        'Database' => [
            'name' => 'Database',
            'description' => 'Database commands',
            'icon' => '🗄️',
            'commands' => []
        ],
        'Cache' => [
            'name' => 'Cache',
            'description' => 'Cache commands',
            'icon' => '⚡',
            'commands' => []
        ]
    ];

    $state->setCategories($categories);
    $state->setCurrentView(TuiView::CATEGORIES);
    $state->setRunning(true);

    // Test arrow navigation
    echo "Testing arrow key navigation:\n";
    echo "Initial category: " . $state->getSelectedCategory() . "\n";

    // Test arrow down
    $inputHandler->handleInput(TuiKeyCode::ARROW_DOWN->value, $state, $history);
    echo "After arrow down: " . $state->getSelectedCategory() . "\n";

    // Test arrow up
    $inputHandler->handleInput(TuiKeyCode::ARROW_UP->value, $state, $history);
    echo "After arrow up: " . $state->getSelectedCategory() . "\n";

    // Test bounds checking - try to go below 0
    $inputHandler->handleInput(TuiKeyCode::ARROW_UP->value, $state, $history);
    echo "After arrow up (should stay at 0): " . $state->getSelectedCategory() . "\n";

    echo "\n✅ TUI Navigation Test PASSED - Arrow key handling works!\n";

} catch (\Throwable $e) {
    echo "\n❌ TUI Navigation Test FAILED:\n";
    echo "Error: " . $e->getMessage() . "\n";
    echo "File: " . $e->getFile() . ":" . $e->getLine() . "\n";
    echo "\nStack trace:\n" . $e->getTraceAsString() . "\n";
}