<?php

declare(strict_types=1);

namespace App\Framework\Console\Examples;

use App\Framework\Console\CommandGroup;
use App\Framework\Console\CommandWorkflow;
use App\Framework\Console\ConsoleColor;
use App\Framework\Console\ConsoleCommand;
use App\Framework\Console\ConsoleInput;
use App\Framework\Console\ConsoleOutputInterface;
use App\Framework\Console\ExitCode;

/**
 * Demo commands to showcase the new command groups and workflow system
 */
#[CommandGroup(
    name: 'Workflow Demo',
    description: 'Demo commands showcasing workflow and group features',
    icon: '🎭',
    priority: 100,
    tags: ['demo', 'workflow', 'showcase']
)]
final readonly class WorkflowDemoCommand
{
    #[ConsoleCommand('workflow:setup', 'Set up a complete development environment')]
    #[CommandWorkflow(
        name: 'Development Setup',
        description: 'Complete development environment setup workflow',
        steps: [
            [
                'command' => 'workflow:check-system',
                'description' => 'Check system requirements',
                'optional' => false,
            ],
            [
                'command' => 'workflow:install-deps',
                'description' => 'Install dependencies',
                'optional' => false,
                'retryCount' => 2,
            ],
            [
                'command' => 'workflow:setup-db',
                'description' => 'Set up database',
                'optional' => false,
                'environment' => ['DB_SETUP' => 'true'],
            ],
            [
                'command' => 'workflow:seed-data',
                'description' => 'Seed initial data',
                'optional' => true,
                'condition' => 'development',
            ],
        ],
        prerequisites: ['database'],
        stopOnError: true,
        rollbackSteps: [
            [
                'command' => 'workflow:cleanup-db',
                'description' => 'Clean up database on failure',
            ],
        ]
    )]
    public function setupWorkflow(ConsoleInput $input, ConsoleOutputInterface $output): ExitCode
    {
        $output->writeLine('🚀 Development setup workflow started!', ConsoleColor::BRIGHT_GREEN);
        $output->writeLine('This workflow will be handled by the WorkflowExecutor.', ConsoleColor::GRAY);

        return ExitCode::SUCCESS;
    }

    #[ConsoleCommand('workflow:check-system', 'Check system requirements')]
    public function checkSystem(ConsoleInput $input, ConsoleOutputInterface $output): ExitCode
    {
        $output->writeLine('🔍 Checking system requirements...', ConsoleColor::CYAN);

        $checks = [
            'PHP Version' => PHP_VERSION_ID >= 80200,
            'Extensions' => extension_loaded('pdo'),
            'Memory Limit' => (int)ini_get('memory_limit') >= 128,
            'Disk Space' => disk_free_space('.') > 1024 * 1024 * 100, // 100MB
        ];

        foreach ($checks as $check => $result) {
            $status = $result ? '✅' : '❌';
            $color = $result ? ConsoleColor::GREEN : ConsoleColor::RED;
            $output->writeLine("  {$status} {$check}", $color);
        }

        return ExitCode::SUCCESS;
    }

    #[ConsoleCommand('workflow:install-deps', 'Install project dependencies')]
    public function installDependencies(ConsoleInput $input, ConsoleOutputInterface $output): ExitCode
    {
        $output->writeLine('📦 Installing dependencies...', ConsoleColor::CYAN);

        $steps = [
            'Composer dependencies',
            'NPM packages',
            'Framework assets',
        ];

        foreach ($steps as $step) {
            $output->writeLine("  Installing {$step}...", ConsoleColor::WHITE);
            usleep(500000); // Simulate installation time
            $output->writeLine("  ✅ {$step} installed", ConsoleColor::GREEN);
        }

        return ExitCode::SUCCESS;
    }

    #[ConsoleCommand('workflow:setup-db', 'Set up database and run migrations')]
    public function setupDatabase(ConsoleInput $input, ConsoleOutputInterface $output): ExitCode
    {
        $output->writeLine('🗄️ Setting up database...', ConsoleColor::CYAN);

        $steps = [
            'Create database',
            'Run migrations',
            'Set up indexes',
        ];

        foreach ($steps as $step) {
            $output->writeLine("  {$step}...", ConsoleColor::WHITE);
            usleep(300000); // Simulate operation time
            $output->writeLine("  ✅ {$step} completed", ConsoleColor::GREEN);
        }

        return ExitCode::SUCCESS;
    }

    #[ConsoleCommand('workflow:seed-data', 'Seed initial development data')]
    public function seedData(ConsoleInput $input, ConsoleOutputInterface $output): ExitCode
    {
        $output->writeLine('🌱 Seeding development data...', ConsoleColor::CYAN);

        $datasets = [
            'Test users',
            'Sample content',
            'Configuration data',
        ];

        foreach ($datasets as $dataset) {
            $output->writeLine("  Seeding {$dataset}...", ConsoleColor::WHITE);
            usleep(200000); // Simulate seeding time
            $output->writeLine("  ✅ {$dataset} seeded", ConsoleColor::GREEN);
        }

        return ExitCode::SUCCESS;
    }

    #[ConsoleCommand('workflow:cleanup-db', 'Clean up database (rollback operation)')]
    public function cleanupDatabase(ConsoleInput $input, ConsoleOutputInterface $output): ExitCode
    {
        $output->writeLine('🧹 Cleaning up database...', ConsoleColor::YELLOW);

        $steps = [
            'Drop test tables',
            'Clear cache',
            'Reset connections',
        ];

        foreach ($steps as $step) {
            $output->writeLine("  {$step}...", ConsoleColor::WHITE);
            usleep(100000); // Simulate cleanup time
            $output->writeLine("  ✅ {$step} completed", ConsoleColor::GREEN);
        }

        return ExitCode::SUCCESS;
    }

    #[ConsoleCommand('workflow:status', 'Show workflow and group information')]
    public function showStatus(ConsoleInput $input, ConsoleOutputInterface $output): ExitCode
    {
        $output->writeLine('📋 Workflow System Status', ConsoleColor::BRIGHT_CYAN);
        $output->newLine();

        $output->writeLine('Command Groups:', ConsoleColor::BRIGHT_WHITE);
        $output->writeLine('  • This command belongs to: "Workflow Demo" group', ConsoleColor::WHITE);
        $output->writeLine('  • Group icon: 🎭', ConsoleColor::WHITE);
        $output->writeLine('  • Group priority: 100', ConsoleColor::WHITE);
        $output->writeLine('  • Group tags: demo, workflow, showcase', ConsoleColor::WHITE);

        $output->newLine();
        $output->writeLine('Available Workflows:', ConsoleColor::BRIGHT_WHITE);
        $output->writeLine('  • Development Setup (workflow:setup)', ConsoleColor::WHITE);
        $output->writeLine('    - 4 steps with prerequisites and rollback', ConsoleColor::GRAY);
        $output->writeLine('    - Stops on error with automatic cleanup', ConsoleColor::GRAY);

        $output->newLine();
        $output->writeLine('💡 Try running "workflow:setup" to see the workflow in action!', ConsoleColor::BRIGHT_YELLOW);

        return ExitCode::SUCCESS;
    }
}
