<?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 "=== Format Information Pixel Check ===\n\n";

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

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

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

echo "=== Format Information Pixels (Row 8) ===\n";
echo "Expected for Mask 5, Level M: 10000001 1001110\n";
echo "Actual:\n";

// Horizontal format info (row 8)
// Left: cols 0-5, 7-8 (8 bits, skip 6)
// Right: cols 20-14 (7 bits, no skip)
$formatRow8 = '';
$cols = [0, 1, 2, 3, 4, 5, 7, 8, 20, 19, 18, 17, 16, 15, 14];
foreach ($cols as $col) {
    $isDark = $matrix->getModuleAt(8, $col)->isDark();
    $formatRow8 .= $isDark ? '1' : '0';
    echo "  Row 8, Col {$col}: " . ($isDark ? '█' : '░') . " (" . ($isDark ? '1' : '0') . ")\n";
}

echo "\nCombined horizontal: {$formatRow8}\n";

echo "\n=== Format Information Pixels (Column 8) ===\n";
$formatCol8 = '';
// Bottom: rows 20-14 (7 bits)
// Top: rows 8, 7, 5, 4, 3, 2, 1, 0 (8 bits, skip 6)
$rows = [20, 19, 18, 17, 16, 15, 14, 8, 7, 5, 4, 3, 2, 1, 0];
foreach ($rows as $row) {
    $isDark = $matrix->getModuleAt($row, 8)->isDark();
    $formatCol8 .= $isDark ? '1' : '0';
    echo "  Row {$row}, Col 8: " . ($isDark ? '█' : '░') . " (" . ($isDark ? '1' : '0') . ")\n";
}

echo "\nCombined vertical: {$formatCol8}\n";

echo "\n=== Analysis ===\n";
$expectedHorizontal = '100000011001110';
$expectedFirstByte = substr($expectedHorizontal, 0, 8);
$actualFirstByte = substr($formatRow8, 0, 8);

echo "Expected first 8 bits: {$expectedFirstByte}\n";
echo "Actual first 8 bits:   {$actualFirstByte}\n";
echo ($expectedFirstByte === $actualFirstByte ? "✅ MATCH!" : "❌ MISMATCH!") . "\n";
