<?php

declare(strict_types=1);

namespace App\Framework\Console\Layout\Commands;

use App\Framework\Console\Attributes\ConsoleCommand;
use App\Framework\Console\ExitCode;
use App\Framework\Console\Input\ConsoleInput;
use App\Framework\Console\Layout\ResponsiveOutput;
use App\Framework\Console\Layout\TerminalSize;
use App\Framework\Console\Output\ConsoleOutput;

final readonly class LayoutTestCommand
{
    #[ConsoleCommand(
        name: 'layout:test',
        description: 'Test responsive terminal layout features'
    )]
    public function testLayout(ConsoleInput $input, ConsoleOutput $output): ExitCode
    {
        $responsiveOutput = ResponsiveOutput::create($output);
        $terminalSize = $responsiveOutput->getTerminalSize();

        $output->writeLine("<yellow>Responsive Terminal Layout Test</yellow>\n");

        // Show terminal info
        $responsiveOutput->writeKeyValue($terminalSize->toArray(), 'Terminal Information:');
        $output->writeLine('');

        // Test responsive table
        $output->writeLine('<yellow>Responsive Table Test:</yellow>');
        $headers = ['Command', 'Executions', 'Success Rate', 'Avg Time', 'Category'];
        $rows = [
            ['analytics:usage', '156', '98.7%', '1.2s', 'Analytics'],
            ['health:check', '89', '100%', '0.8s', 'Monitoring'],
            ['security:whoami', '45', '100%', '0.3s', 'Security'],
            ['make:migration', '23', '95.7%', '2.1s', 'Database'],
            ['cache:clear', '78', '100%', '0.5s', 'System'],
        ];

        $responsiveOutput->writeTable($headers, $rows);
        $output->writeLine('');

        // Test key-value display
        $output->writeLine('<yellow>Key-Value Display Test:</yellow>');
        $data = [
            'Framework Version' => '1.0.0',
            'PHP Version' => PHP_VERSION,
            'Memory Usage' => memory_get_usage(true) . ' bytes',
            'Environment' => $_ENV['APP_ENV'] ?? 'development',
            'Debug Mode' => 'Enabled',
        ];

        $responsiveOutput->writeKeyValue($data);
        $output->writeLine('');

        // Test list display
        $features = [
            'Terminal size detection',
            'Responsive breakpoints',
            'Adaptive table layouts',
            'Text truncation for small screens',
            'Multi-column support for wide terminals',
            'Progress bars with terminal-aware sizing',
        ];

        $responsiveOutput->writeList($features, 'Layout Features:');
        $output->writeLine('');

        // Test progress bar
        $output->writeLine('<yellow>Progress Bar Test:</yellow>');
        for ($i = 0; $i <= 100; $i += 20) {
            $responsiveOutput->writeProgressBar($i, 100, 'Processing');
            if ($i < 100) {
                usleep(200000); // 0.2 second delay
            }
        }
        $output->writeLine('');

        // Test columns (only on wider terminals)
        if ($terminalSize->getBreakpoint()->getColumnCount() > 1) {
            $output->writeLine('<yellow>Multi-Column Test:</yellow>');
            $columns = [
                [
                    'title' => 'Commands',
                    'items' => ['analytics:usage', 'health:check', 'security:whoami'],
                ],
                [
                    'title' => 'Status',
                    'items' => ['Active', 'Active', 'Active'],
                ],
                [
                    'title' => 'Category',
                    'items' => ['Analytics', 'Health', 'Security'],
                ],
            ];

            $responsiveOutput->writeColumns($columns);
        } else {
            $output->writeLine('<yellow>Multi-Column Test:</yellow> <gray>Skipped (terminal too narrow)</gray>');
        }

        $output->writeLine('');
        $output->writeLine('<green>✓ Responsive layout test completed!</green>');

        return ExitCode::SUCCESS;
    }

    #[ConsoleCommand(
        name: 'layout:info',
        description: 'Show terminal size and breakpoint information'
    )]
    public function showInfo(ConsoleInput $input, ConsoleOutput $output): ExitCode
    {
        $terminalSize = TerminalSize::detect();
        $json = $input->hasOption('json');

        if ($json) {
            $output->writeLine(json_encode($terminalSize->toArray(), JSON_PRETTY_PRINT));

            return ExitCode::SUCCESS;
        }

        $responsiveOutput = ResponsiveOutput::create($output);
        $output->writeLine("<yellow>Terminal Information</yellow>\n");

        $responsiveOutput->writeKeyValue($terminalSize->toArray());

        $output->writeLine('');
        $breakpoint = $terminalSize->getBreakpoint();
        $output->writeLine("<yellow>Breakpoint Details:</yellow>");
        $output->writeLine("Current: {$breakpoint->value} - {$breakpoint->getDescription()}");
        $output->writeLine("Column Count: {$breakpoint->getColumnCount()}");
        $output->writeLine("Table Width: {$breakpoint->getTableWidth()}");
        $output->writeLine("Max Text Length: {$breakpoint->getMaxTextLength()}");
        $output->writeLine("Should Truncate: " . ($breakpoint->shouldTruncateText() ? 'Yes' : 'No'));

        return ExitCode::SUCCESS;
    }
}
