<?php

declare(strict_types=1);

require_once __DIR__ . '/../../vendor/autoload.php';

use App\Framework\QrCode\ErrorCorrectionLevel;
use App\Framework\QrCode\QrCodeGenerator;

// Test QR code generation with format information
try {
    $generator = QrCodeGenerator::create();

    // Generate QR code for TOTP URL (shorter version)
    $testData = 'otpauth://totp/Test?secret=JBSWY3DPEHPK3PXP';
    echo "Generating QR code for: $testData\n\n";

    // Generate matrix directly with L error correction for higher capacity
    $matrix = $generator->generateMatrix($testData, ErrorCorrectionLevel::L);

    echo "QR Code Matrix (with format info and masking):\n";
    echo $matrix->toString('█', '░');
    echo "\n";

    echo "QR Code Statistics:\n";
    $stats = $matrix->getStatistics();
    foreach ($stats as $key => $value) {
        echo "  $key: " . (is_float($value) ? number_format($value, 3) : $value) . "\n";
    }

    // Generate SVG as well
    echo "\nGenerating SVG...\n";
    $svg = $generator->generateSvg($testData, ErrorCorrectionLevel::L);
    echo "SVG generated successfully! Length: " . strlen($svg) . " characters\n";

    // Analyze data
    echo "\nData Analysis:\n";
    $analysis = $generator->analyzeData($testData);
    foreach ($analysis as $key => $value) {
        if ($value instanceof \UnitEnum) {
            echo "  $key: " . $value->value . "\n";
        } elseif (is_float($value)) {
            echo "  $key: " . number_format($value, 3) . "\n";
        } else {
            echo "  $key: " . $value . "\n";
        }
    }

    echo "\n✅ QR Code generated successfully with format information!\n";
    echo "The QR code should now be scannable by standard QR code readers.\n";

} catch (Exception $e) {
    echo "❌ Error generating QR code: " . $e->getMessage() . "\n";
    echo "Trace: " . $e->getTraceAsString() . "\n";
}
