<?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 "=== Matrix Comparison: Our Implementation vs Python Reference ===\n\n";

// Generate our QR code
$config = new QrCodeConfig(
    version: QrCodeVersion::fromNumber(1),
    errorCorrectionLevel: ErrorCorrectionLevel::M,
    encodingMode: EncodingMode::BYTE
);

$ourMatrix = QrCodeGenerator::generate("HELLO WORLD", $config);
$size = $ourMatrix->getSize();

// Python reference matrix (from previous output)
$pythonMatrix = [
    "111111100010101111111",
    "100000101110001000001",
    "101110100010101011101",
    "101110100010101011101",
    "101110101011101011101",
    "100000100111001000001",
    "111111101010101111111",
    "000000000000000000000",
    "101010100100100010010",
    "011110001001000010001",
    "000111111101001011000",
    "111101011001110101110",
    "010011110101001110101",
    "000000001010001000101",
    "111111100000100101100",
    "100000100110001101000",
    "101110101100101111111",
    "101110100011010100010",
    "101110101111011101001",
    "100000100001110001011",
    "111111101101011100001",
];

// Convert our matrix to binary strings
$ourMatrixBinary = [];
for ($row = 0; $row < $size; $row++) {
    $rowStr = '';
    for ($col = 0; $col < $size; $col++) {
        $rowStr .= $ourMatrix->getModuleAt($row, $col)->isDark() ? '1' : '0';
    }
    $ourMatrixBinary[] = $rowStr;
}

echo "Comparing row by row:\n\n";

$totalDifferences = 0;
$differencePositions = [];

for ($row = 0; $row < $size; $row++) {
    $our = $ourMatrixBinary[$row];
    $python = $pythonMatrix[$row];
    
    $differences = 0;
    $diffPositions = [];
    
    for ($col = 0; $col < $size; $col++) {
        if ($our[$col] !== $python[$col]) {
            $differences++;
            $totalDifferences++;
            $diffPositions[] = $col;
        }
    }
    
    if ($differences > 0) {
        echo "Row {$row}: {$differences} differences at columns: " . implode(', ', $diffPositions) . "\n";
        echo "  Our:    {$our}\n";
        echo "  Python: {$python}\n\n";
        
        $differencePositions[] = ['row' => $row, 'cols' => $diffPositions];
    }
}

echo "\n=== Summary ===\n";
echo "Total differences: {$totalDifferences} modules out of " . ($size * $size) . "\n";
echo "Match percentage: " . number_format((1 - $totalDifferences / ($size * $size)) * 100, 2) . "%\n\n";

if ($totalDifferences === 0) {
    echo "✅ MATRICES ARE IDENTICAL!\n";
} else {
    echo "❌ MATRICES DIFFER!\n\n";
    
    // Analyze which areas differ
    echo "Analyzing difference patterns:\n";
    
    $functionPatternDiffs = 0;
    $dataDiffs = 0;
    
    foreach ($differencePositions as $diff) {
        $row = $diff['row'];
        foreach ($diff['cols'] as $col) {
            // Check if it's in function patterns
            if (
                ($row < 9 && $col < 9) ||
                ($row < 9 && $col >= $size - 8) ||
                ($row >= $size - 8 && $col < 9) ||
                $row === 6 || $col === 6 ||
                $row === 8 || $col === 8
            ) {
                $functionPatternDiffs++;
            } else {
                $dataDiffs++;
            }
        }
    }
    
    echo "  Function pattern differences: {$functionPatternDiffs}\n";
    echo "  Data area differences: {$dataDiffs}\n";
}
