<?php

declare(strict_types=1);

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

use App\Framework\QrCode\QrCodeGenerator;
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 "=== FINAL TEST: ALL CRITICAL FIXES APPLIED ===\n\n";

echo "Fixes applied:\n";
echo "1. ✅ isOccupied() - Format Info reserviert nur 30 Module (nicht 42)\n";
echo "2. ✅ MaskEvaluator::isFunctionPattern() - identisch zu isOccupied()\n";
echo "3. ✅ FinderPattern::drawPattern() - korrekter 1:1:3:1:1 Ring\n\n";

// Generate multiple test QR codes
$testData = [
    ['data' => 'A', 'description' => 'Single character'],
    ['data' => 'HELLO', 'description' => 'Simple text'],
    ['data' => '123', 'description' => 'Numbers'],
    ['data' => 'https://example.com', 'description' => 'URL']
];

foreach ($testData as $testCase) {
    $data = $testCase['data'];
    $description = $testCase['description'];

    echo "Generating QR Code for: \"{$data}\" ({$description})\n";

    $config = new QrCodeConfig(
        version: QrCodeVersion::fromNumber(1),
        errorCorrectionLevel: ErrorCorrectionLevel::M,
        encodingMode: EncodingMode::BYTE
    );

    $matrix = QrCodeGenerator::generate($data, $config);
    $size = 21;

    // Generate PNG
    $scale = 20;
    $quietZone = 4;
    $totalSize = ($size + 2 * $quietZone) * $scale;

    $image = imagecreate($totalSize, $totalSize);
    $white = imagecolorallocate($image, 255, 255, 255);
    $black = imagecolorallocate($image, 0, 0, 0);

    imagefill($image, 0, 0, $white);

    for ($row = 0; $row < $size; $row++) {
        for ($col = 0; $col < $size; $col++) {
            if ($matrix->getModuleAt($row, $col)->isDark()) {
                $x = ($quietZone + $col) * $scale;
                $y = ($quietZone + $row) * $scale;

                for ($dy = 0; $dy < $scale; $dy++) {
                    for ($dx = 0; $dx < $scale; $dx++) {
                        imagesetpixel($image, $x + $dx, $y + $dy, $black);
                    }
                }
            }
        }
    }

    $safeName = preg_replace('/[^a-zA-Z0-9]/', '-', $data);
    $filename = "qrcode-FINAL-{$safeName}.png";
    imagepng($image, "/var/www/html/public/{$filename}", 0);

    echo "  ✓ Generated: {$filename}\n";
    echo "  URL: https://localhost/{$filename}\n\n";
}

echo "=== Summary of Critical Fixes ===\n\n";

echo "BUG #1: Format Information Reservation\n";
echo "  ❌ OLD: Blockierte KOMPLETTE Zeile 8 + Spalte 8 (42 Module)\n";
echo "  ✅ NEW: Blockiert nur Format Info Positionen (30 Module)\n";
echo "  Impact: Daten können jetzt in freie Bereiche von Zeile/Spalte 8\n\n";

echo "BUG #2: MaskEvaluator Function Pattern Detection\n";
echo "  ❌ OLD: Benutzte falsche isFunctionPattern() Logik\n";
echo "  ✅ NEW: Identisch zu QrCodeGenerator::isOccupied()\n";
echo "  Impact: Masking greift nicht mehr in Format Info ein\n\n";

echo "BUG #3: FinderPattern Structure\n";
echo "  ❌ OLD: Fehlerhafter innerer weißer Ring\n";
echo "  ✅ NEW: Korrekter 1:1:3:1:1 Ring (Outer DARK, White Ring, 3x3 DARK)\n";
echo "  Impact: Scanner können Finder Patterns zuverlässig erkennen\n\n";

echo "=== BITTE TESTEN SIE DIESE QR-CODES! ===\n";
echo "Sie sollten JETZT scanbar sein mit jedem Standard-QR-Scanner.\n\n";

echo "Test-URLs:\n";
echo "1. https://localhost/qrcode-FINAL-A.png\n";
echo "2. https://localhost/qrcode-FINAL-HELLO.png\n";
echo "3. https://localhost/qrcode-FINAL-123.png\n";
echo "4. https://localhost/qrcode-FINAL-https---example-com.png\n";
