<?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 "=== Testing FIXED Format Info Reservation ===\n\n";

// Generate QR code for "A" with corrected isOccupied() logic
$config = new QrCodeConfig(
    version: QrCodeVersion::fromNumber(1),
    errorCorrectionLevel: ErrorCorrectionLevel::M,
    encodingMode: EncodingMode::BYTE
);

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

echo "Generated QR Code for 'A' with FIXED Format Info reservation\n";
echo "Version: 1 (21x21)\n";
echo "EC Level: M\n";
echo "Encoding: BYTE\n\n";

// 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);
                }
            }
        }
    }
}

$filename = "qrcode-FIXED-A.png";
imagepng($image, "/var/www/html/public/{$filename}", 0);

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

echo "=== Key Fix Applied ===\n";
echo "❌ OLD: Blocked ENTIRE row 8 and column 8 (42 modules)\n";
echo "✅ NEW: Block only Format Info positions (30 modules)\n\n";

echo "Now row 8 and column 8 have data modules in the middle section!\n";
echo "This allows proper data placement without colliding with Format Info.\n\n";

echo "Please test this QR code - it should NOW be scannable!\n";
