<?php

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

// Test TUI complete functionality
use App\Framework\Console\Components\TuiState;
use App\Framework\Console\Components\TuiInputHandler;
use App\Framework\Console\Components\TuiCommandExecutor;
use App\Framework\Console\Components\TuiRenderer;
use App\Framework\Console\CommandHistory;
use App\Framework\Console\TuiView;
use App\Framework\Console\TuiKeyCode;
use App\Framework\Console\ConsoleOutput;
use App\Framework\Console\Screen\ScreenManager;

echo "Testing Complete TUI Functionality...\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;
        }
    };

    // Create mock output and screen manager
    $screenManager = new ScreenManager(new ConsoleOutput());
    $output = new ConsoleOutput();
    $output->screen = $screenManager;

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

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

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

    echo "✅ Initial Setup Complete\n";
    echo "Categories loaded: " . count($categories) . "\n";
    echo "Current view: " . $state->getCurrentView()->name . "\n";
    echo "Selected category: " . $state->getSelectedCategory() . "\n\n";

    // Test Arrow Down Navigation
    echo "Testing Arrow DOWN navigation:\n";
    echo "Before: Category " . $state->getSelectedCategory() . " ('{$categories[array_keys($categories)[$state->getSelectedCategory()]]['name']}')\n";

    $inputHandler->handleInput(TuiKeyCode::ARROW_DOWN->value, $state, $history);

    echo "After Arrow DOWN: Category " . $state->getSelectedCategory() . " ('{$categories[array_keys($categories)[$state->getSelectedCategory()]]['name']}')\n\n";

    // Test Arrow Up Navigation
    echo "Testing Arrow UP navigation:\n";
    echo "Before: Category " . $state->getSelectedCategory() . " ('{$categories[array_keys($categories)[$state->getSelectedCategory()]]['name']}')\n";

    $inputHandler->handleInput(TuiKeyCode::ARROW_UP->value, $state, $history);

    echo "After Arrow UP: Category " . $state->getSelectedCategory() . " ('{$categories[array_keys($categories)[$state->getSelectedCategory()]]['name']}')\n\n";

    // Test bounds checking - try to go below 0
    echo "Testing boundary protection (going below 0):\n";

    $inputHandler->handleInput(TuiKeyCode::ARROW_UP->value, $state, $history);

    echo "After Arrow UP (should stay at 0): Category " . $state->getSelectedCategory() . " ('{$categories[array_keys($categories)[$state->getSelectedCategory()]]['name']}')\n\n";

    // Test bounds checking - try to go above max
    echo "Testing boundary protection (going above max):\n";

    // Go to last item
    $state->setSelectedCategory(2); // Testing category
    echo "Set to last category (2): '{$categories[array_keys($categories)[$state->getSelectedCategory()]]['name']}'\n";

    $inputHandler->handleInput(TuiKeyCode::ARROW_DOWN->value, $state, $history);

    echo "After Arrow DOWN (should stay at 2): Category " . $state->getSelectedCategory() . " ('{$categories[array_keys($categories)[$state->getSelectedCategory()]]['name']}')\n\n";

    // Test rendering with actual state
    echo "Testing TUI Rendering:\n";
    echo "=====================\n";

    $state->setSelectedCategory(0);
    $renderer->render($state, $history);

    echo "\n✅ TUI Complete Test PASSED - All functionality works!\n";

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