<?php

declare(strict_types=1);

namespace App\Framework\Deployment\Ssl\Commands;

use App\Framework\Config\Environment;
use App\Framework\Console\Attributes\ConsoleCommand;
use App\Framework\Console\ConsoleOutput;
use App\Framework\Console\ExitCode;
use App\Framework\Deployment\Ssl\Services\SslCertificateService;
use App\Framework\Deployment\Ssl\ValueObjects\SslConfiguration;

/**
 * Console command to test SSL configuration without obtaining a certificate
 */
#[ConsoleCommand(
    name: 'ssl:test',
    description: 'Test SSL configuration without obtaining a certificate (dry-run)'
)]
final readonly class SslTestCommand
{
    public function __construct(
        private SslCertificateService $sslService,
        private Environment $environment,
        private ConsoleOutput $output
    ) {}

    public function execute(): ExitCode
    {
        $this->output->writeLine('🧪 Testing SSL Configuration...');
        $this->output->writeLine('');

        try {
            // Load configuration from environment
            $config = SslConfiguration::fromEnvironment($this->environment);

            // Display configuration
            $this->displayConfiguration($config);

            // Run dry-run test
            $this->output->writeLine('Running dry-run test with Let\'s Encrypt...');
            $this->output->writeLine('This will verify your configuration without obtaining a certificate.');
            $this->output->writeLine('');

            $success = $this->sslService->test($config);

            if ($success) {
                $this->output->writeLine('');
                $this->output->writeLine('✅ Configuration test passed!');
                $this->output->writeLine('');
                $this->output->writeLine('Your domain, DNS, and webroot configuration are correct.');
                $this->output->writeLine('You can now run "ssl:init" to obtain a real certificate.');
            } else {
                $this->output->writeLine('');
                $this->output->writeLine('❌ Configuration test failed!');
                $this->output->writeLine('');
                $this->output->writeLine('Please check:');
                $this->output->writeLine('  - Domain DNS is correctly configured and pointing to this server');
                $this->output->writeLine('  - Port 80 is open and accessible from the internet');
                $this->output->writeLine('  - Webroot directory (' . $config->certbotWwwDir->toString() . ') is accessible');
                $this->output->writeLine('  - No firewall or proxy blocking Let\'s Encrypt validation requests');
            }

            $this->output->writeLine('');

            return $success ? ExitCode::SUCCESS : ExitCode::FAILURE;

        } catch (\Exception $e) {
            $this->output->writeLine('');
            $this->output->writeLine('❌ Error: ' . $e->getMessage());
            $this->output->writeLine('');
            return ExitCode::FAILURE;
        }
    }

    private function displayConfiguration(SslConfiguration $config): void
    {
        $this->output->writeLine('Configuration:');
        $this->output->writeLine('─────────────────────────────────────────');
        $this->output->writeLine('Domain:         ' . $config->domain->value);
        $this->output->writeLine('Email:          ' . $config->email->value);
        $this->output->writeLine('Mode:           ' . $config->mode->value);
        $this->output->writeLine('Webroot:        ' . $config->certbotWwwDir->toString());
        $this->output->writeLine('Config Dir:     ' . $config->certbotConfDir->toString());
        $this->output->writeLine('─────────────────────────────────────────');
        $this->output->writeLine('');
    }
}
