<?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 "=== QR Code Decode Test ===\n\n";

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

$matrix = $qrCodeGenerator = QrCodeGenerator::generate($data, $config);
$size = $matrix->getSize();

echo "Input: '{$data}'\n";
echo "Matrix Size: {$size}x{$size}\n\n";

// Extract format information
echo "=== Format Information ===\n";
$formatBits = '';
$cols = [0, 1, 2, 3, 4, 5, 7, 8, 20, 19, 18, 17, 16, 15, 14];
foreach ($cols as $col) {
    $formatBits .= $matrix->getModuleAt(8, $col)->isDark() ? '1' : '0';
}
echo "Format bits: {$formatBits}\n";

// Decode format info
$formatTable = [
    0 => '101010000010010',
    1 => '101000100100101',
    2 => '101111001111100',
    3 => '101101101001011',
    4 => '100010111111001',
    5 => '100000011001110',
    6 => '100111110010111',
    7 => '100101010100000',
];

$maskPattern = null;
foreach ($formatTable as $mask => $bits) {
    if ($formatBits === $bits) {
        $maskPattern = $mask;
        echo "Mask Pattern: {$mask}\n\n";
        break;
    }
}

if ($maskPattern === null) {
    echo "❌ Invalid format information!\n";
    exit(1);
}

// Visual representation
echo "=== Visual Matrix (first 21x21) ===\n";
echo "   ";
for ($col = 0; $col < 21; $col++) {
    echo str_pad((string)$col, 2, ' ', STR_PAD_LEFT);
}
echo "\n";

for ($row = 0; $row < 21; $row++) {
    echo str_pad((string)$row, 2, ' ', STR_PAD_LEFT) . ' ';
    for ($col = 0; $col < 21; $col++) {
        $module = $matrix->getModuleAt($row, $col);
        echo ($module->isDark() ? '██' : '  ');
    }
    echo "\n";
}

echo "\n=== Timing Pattern Check ===\n";
// Row 6 should alternate
$row6 = '';
for ($col = 8; $col < 13; $col++) {
    $row6 .= $matrix->getModuleAt(6, $col)->isDark() ? '1' : '0';
}
echo "Row 6 (cols 8-12): {$row6} (should be 10101)\n";

// Column 6 should alternate
$col6 = '';
for ($row = 8; $row < 13; $row++) {
    $col6 .= $matrix->getModuleAt($row, 6)->isDark() ? '1' : '0';
}
echo "Col 6 (rows 8-12): {$col6} (should be 10101)\n";

echo "\n=== Finder Pattern Check ===\n";
// Top-left finder pattern (should be 7x7 with specific pattern)
$finderExpected = [
    [1,1,1,1,1,1,1],
    [1,0,0,0,0,0,1],
    [1,0,1,1,1,0,1],
    [1,0,1,1,1,0,1],
    [1,0,1,1,1,0,1],
    [1,0,0,0,0,0,1],
    [1,1,1,1,1,1,1],
];

$finderMatch = true;
for ($row = 0; $row < 7; $row++) {
    for ($col = 0; $col < 7; $col++) {
        $actual = $matrix->getModuleAt($row, $col)->isDark() ? 1 : 0;
        if ($actual !== $finderExpected[$row][$col]) {
            $finderMatch = false;
            echo "❌ Mismatch at ({$row},{$col}): expected {$finderExpected[$row][$col]}, got {$actual}\n";
        }
    }
}

if ($finderMatch) {
    echo "✅ Top-left finder pattern correct\n";
}

echo "\n=== Dark Module Check ===\n";
$darkModuleRow = 4 * 1 + 9; // Version 1
$darkModule = $matrix->getModuleAt($darkModuleRow, 8);
echo "Dark module at ({$darkModuleRow}, 8): " . ($darkModule->isDark() ? '✅ Dark' : '❌ Light') . "\n";

echo "\n=== Conclusion ===\n";
echo "Format Information: " . ($maskPattern !== null ? '✅ Valid' : '❌ Invalid') . "\n";
echo "Finder Pattern: " . ($finderMatch ? '✅ Correct' : '❌ Incorrect') . "\n";
echo "Timing Pattern: Check manually above\n";
echo "Dark Module: " . ($darkModule->isDark() ? '✅ Present' : '❌ Missing') . "\n";
