<?php

declare(strict_types=1);

namespace App\Framework\Vault\Console;

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;
use App\Framework\Vault\DatabaseVault;

/**
 * Vault Key Generation Command (standalone - no Vault dependency)
 */
final readonly class VaultKeyCommand
{
    #[ConsoleCommand('vault:generate-key', 'Generate a new Vault encryption key')]
    public function generateKey(ConsoleInput $input, ConsoleOutputInterface $output): ExitCode
    {
        $output->writeLine('🔐 Generating Vault encryption key...', ConsoleColor::CYAN);
        $output->newLine();

        try {
            $key = DatabaseVault::generateEncryptionKey();
            $encodedKey = DatabaseVault::encodeKey($key);

            $output->writeSuccess('✅ Encryption key generated successfully!');
            $output->newLine();
            $output->writeLine('VAULT_ENCRYPTION_KEY=' . $encodedKey, ConsoleColor::YELLOW);
            $output->newLine();

            $output->writeWarning('🚨 SECURITY WARNINGS:');
            $output->writeLine('• Store this key securely - it cannot be recovered if lost');
            $output->writeLine('• Add this to your .env file (never commit to version control)');
            $output->writeLine('• Use different keys for different environments');
            $output->newLine();

            $output->writeInfo('📋 Next steps:');
            $output->writeLine('1. Add the key to your .env file');
            $output->writeLine('2. Restart your application');
            $output->writeLine('3. Start using vault: php console.php vault:set my-secret "secret-value"');

            return ExitCode::SUCCESS;
        } catch (\Throwable $e) {
            $output->writeError('❌ Failed to generate encryption key: ' . $e->getMessage());

            return ExitCode::GENERAL_ERROR;
        }
    }
}
