<?php

declare(strict_types=1);

namespace App\Framework\Process\Console;

use App\Framework\Console\Attribute\ConsoleCommand;
use App\Framework\Console\ConsoleInput;
use App\Framework\Console\ExitCode;
use App\Framework\Process\Services\ComposerService;

/**
 * Composer Console Commands.
 */
final readonly class ComposerCommands
{
    public function __construct(
        private ComposerService $composerService
    ) {
    }

    #[ConsoleCommand(name: 'composer:diagnose', description: 'Run Composer diagnostics')]
    public function diagnose(ConsoleInput $input): int
    {
        echo "Running Composer diagnostics...\n\n";

        $result = $this->composerService->diagnose();

        echo "Composer Version: {$result->composerVersion}\n";
        echo "Status: " . ($result->isHealthy ? '✅ Healthy' : '❌ Issues Found') . "\n\n";

        if ($result->hasWarnings()) {
            echo "⚠️  Warnings:\n";
            foreach ($result->warnings as $warning) {
                echo "  - {$warning}\n";
            }
            echo "\n";
        }

        if ($result->hasErrors()) {
            echo "❌ Errors:\n";
            foreach ($result->errors as $error) {
                echo "  - {$error}\n";
            }
            echo "\n";
        }

        if ($result->isHealthy) {
            echo "✅ All checks passed!\n";
        }

        return $result->isHealthy ? ExitCode::SUCCESS : ExitCode::FAILURE;
    }

    #[ConsoleCommand(name: 'composer:outdated', description: 'List outdated Composer packages')]
    public function outdated(ConsoleInput $input): int
    {
        echo "Checking for outdated packages...\n\n";

        $packages = $this->composerService->getOutdatedPackages();

        if (empty($packages)) {
            echo "✅ All packages are up to date!\n";

            return ExitCode::SUCCESS;
        }

        echo "📦 Found " . count($packages) . " outdated package(s):\n\n";

        foreach ($packages as $package) {
            echo "  📌 {$package->name}\n";
            echo "     Current:  {$package->currentVersion}\n";

            if ($package->latestVersion !== null) {
                echo "     Latest:   {$package->latestVersion}\n";
            }

            echo "\n";
        }

        return ExitCode::SUCCESS;
    }

    #[ConsoleCommand(name: 'composer:validate', description: 'Validate composer.json')]
    public function validate(ConsoleInput $input): int
    {
        echo "Validating composer.json...\n\n";

        $isValid = $this->composerService->validate();

        if ($isValid) {
            echo "✅ composer.json is valid!\n";

            return ExitCode::SUCCESS;
        }

        echo "❌ composer.json validation failed!\n";

        return ExitCode::FAILURE;
    }

    #[ConsoleCommand(name: 'composer:installed', description: 'List installed Composer packages')]
    public function installed(ConsoleInput $input): int
    {
        echo "Loading installed packages...\n\n";

        $packages = $this->composerService->getInstalledPackages();

        if (empty($packages)) {
            echo "No packages installed.\n";

            return ExitCode::SUCCESS;
        }

        echo "📦 Installed Packages (" . count($packages) . "):\n\n";

        foreach ($packages as $package) {
            echo "  {$package->name} ({$package->currentVersion})\n";

            if ($package->description !== null) {
                echo "     {$package->description}\n";
            }
        }

        return ExitCode::SUCCESS;
    }

    #[ConsoleCommand(name: 'composer:version', description: 'Show Composer version')]
    public function version(ConsoleInput $input): int
    {
        $version = $this->composerService->getVersion();

        echo "Composer version: {$version}\n";

        return ExitCode::SUCCESS;
    }
}
