<?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;

echo "=== Generating QR Codes with Auto-Version ===\n\n";

$testData = ['A', 'HELLO', 'HELLO WORLD'];

foreach ($testData as $data) {
    // Use autoSize to let it pick the version
    $config = QrCodeConfig::autoSize($data, ErrorCorrectionLevel::M);
    
    $matrix = QrCodeGenerator::generate($data, $config);
    $size = $matrix->getSize();
    $version = $config->version->getVersionNumber();
    
    echo "Data: \"{$data}\"\n";
    echo "  Auto-selected version: {$version}\n";
    echo "  Matrix size: {$size}x{$size}\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-auto-" . urlencode($data) . ".png";
    imagepng($image, "/var/www/html/public/{$filename}", 0);
    
    echo "  Generated: {$filename}\n";
    echo "  Size: {$totalSize}x{$totalSize}px\n\n";
}

echo "Please test these QR codes (auto-version):\n";
echo "1. https://localhost/qrcode-auto-A.png\n";
echo "2. https://localhost/qrcode-auto-HELLO.png\n";
echo "3. https://localhost/qrcode-auto-HELLO+WORLD.png\n";

