<?php

echo "=== Format Information Unmasking Verification ===\n\n";

// What we have in the QR code
$maskedFormat = 0b101111001111100;
$maskedBinary = sprintf('%015b', $maskedFormat);

echo "Masked Format (in QR code): {$maskedBinary}\n";

// XOR mask that should be applied
$xorMask = 0b101010000010010;
$xorBinary = sprintf('%015b', $xorMask);

echo "XOR Mask (standard):        {$xorBinary}\n";

// Unmask
$unmaskedFormat = $maskedFormat ^ $xorMask;
$unmaskedBinary = sprintf('%015b', $unmaskedFormat);

echo "Unmasked Format:            {$unmaskedBinary}\n\n";

// Decode unmasked
$ecBits = substr($unmaskedBinary, 0, 2);
$maskBits = substr($unmaskedBinary, 2, 3);
$bchBits = substr($unmaskedBinary, 5);

echo "Decoded:\n";
echo "  EC Level bits: {$ecBits} = ";
echo match($ecBits) {
    '01' => 'L',
    '00' => 'M',
    '11' => 'Q',
    '10' => 'H'
};
echo "\n";

echo "  Mask Pattern bits: {$maskBits} = Pattern " . bindec($maskBits) . "\n";
echo "  BCH Error Correction: {$bchBits}\n\n";

// Expected
echo "Expected (Level M, Mask 2):\n";
echo "  EC Level: 00 (M)\n";
echo "  Mask Pattern: 010 (2)\n\n";

if ($ecBits === '00' && $maskBits === '010') {
    echo "✅ FORMAT INFORMATION IS CORRECT!\n";
} else {
    echo "❌ FORMAT INFORMATION IS WRONG!\n";
}
