<?php

declare(strict_types=1);

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

use App\Framework\QrCode\QrCodeGenerator;
use App\Framework\QrCode\QrCodeRenderer;
use App\Framework\QrCode\ValueObjects\ErrorCorrectionLevel;
use App\Framework\QrCode\ValueObjects\QrCodeConfig;
use App\Framework\QrCode\ValueObjects\QrCodeVersion;
use App\Framework\QrCode\ValueObjects\EncodingMode;

echo "=== QR Code Phase 3 Test - Version 1-3 ===\n\n";

$testCases = [
    [
        'data' => "HELLO WORLD",  // 11 bytes
        'version' => 1,
        'description' => 'Version 1 - Simple text (11 bytes)'
    ],
    [
        'data' => "https://github.com",  // 18 bytes
        'version' => 1,
        'description' => 'Version 1 - URL (18 bytes)'
    ],
    [
        'data' => str_repeat("A", 30),  // 30 bytes
        'version' => 2,
        'description' => 'Version 2 - 30 bytes (requires Version 2)'
    ],
    [
        'data' => str_repeat("B", 50),  // 50 bytes
        'version' => 3,
        'description' => 'Version 3 - 50 bytes (requires Version 3)'
    ],
];

foreach ($testCases as $index => $testCase) {
    echo "=== Test Case " . ($index + 1) . ": {$testCase['description']} ===\n";
    echo "Data: '" . substr($testCase['data'], 0, 50) . (strlen($testCase['data']) > 50 ? '...' : '') . "'\n";
    echo "Length: " . strlen($testCase['data']) . " bytes\n";
    echo "Expected Version: {$testCase['version']}\n\n";

    try {
        // Create config
        $config = new QrCodeConfig(
            version: QrCodeVersion::fromNumber($testCase['version']),
            errorCorrectionLevel: ErrorCorrectionLevel::M,
            encodingMode: EncodingMode::BYTE
        );

        // Generate QR Code
        $matrix = QrCodeGenerator::generate($testCase['data'], $config);

        echo "✅ Generated successfully!\n";
        echo "  Matrix Size: {$matrix->getSize()}x{$matrix->getSize()}\n";
        echo "  Version: {$matrix->getVersion()->getVersionNumber()}\n";

        // Count modules
        $darkCount = 0;
        $lightCount = 0;
        $size = $matrix->getSize();

        for ($row = 0; $row < $size; $row++) {
            for ($col = 0; $col < $size; $col++) {
                if ($matrix->getModuleAt($row, $col)->isDark()) {
                    $darkCount++;
                } else {
                    $lightCount++;
                }
            }
        }

        $darkRatio = round(($darkCount / ($darkCount + $lightCount)) * 100, 2);
        echo "  Dark modules: {$darkCount} ({$darkRatio}%)\n";

        // Render SVG
        $renderer = new QrCodeRenderer();
        $svg = $renderer->renderSvg($matrix);

        $filename = "qrcode-v{$testCase['version']}-test" . ($index + 1) . ".svg";
        $outputPath = __DIR__ . '/../../public/' . $filename;
        file_put_contents($outputPath, $svg);

        echo "  ✅ SVG saved: {$filename}\n";
        echo "  URL: https://localhost/{$filename}\n";
    } catch (\Exception $e) {
        echo "❌ Error: {$e->getMessage()}\n";
        echo "  File: {$e->getFile()}:{$e->getLine()}\n";
    }

    echo "\n" . str_repeat("-", 60) . "\n\n";
}

echo "=== Test Summary ===\n";
echo "All test cases completed!\n";
echo "Scan the generated QR codes to verify they work correctly.\n";
