<?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 "=== Verifying Timing Patterns ===\n\n";

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

$matrix = QrCodeGenerator::generate("A", $config);
$size = 21;

echo "Horizontal Timing Pattern (row 6, columns 8-12):\n";
echo "Expected: █·█·█ (alternating dark-light-dark-light-dark)\n";
echo "Actual:   ";

for ($col = 8; $col <= 12; $col++) {
    $isDark = $matrix->getModuleAt(6, $col)->isDark();
    $expected = ($col % 2 === 0);  // Even columns should be dark
    
    if ($isDark !== $expected) {
        echo "❌";
    } else {
        echo ($isDark ? "█" : "·");
    }
}
echo "\n\n";

echo "Vertical Timing Pattern (column 6, rows 8-12):\n";
echo "Expected: █·█·█ (alternating dark-light-dark-light-dark)\n";
echo "Actual:   ";

for ($row = 8; $row <= 12; $row++) {
    $isDark = $matrix->getModuleAt($row, 6)->isDark();
    $expected = ($row % 2 === 0);  // Even rows should be dark
    
    if ($isDark !== $expected) {
        echo "❌";
    } else {
        echo ($isDark ? "█" : "·");
    }
}
echo "\n\n";

// Full check
$hErrors = 0;
for ($col = 8; $col <= 12; $col++) {
    $isDark = $matrix->getModuleAt(6, $col)->isDark();
    $expected = ($col % 2 === 0);
    if ($isDark !== $expected) $hErrors++;
}

$vErrors = 0;
for ($row = 8; $row <= 12; $row++) {
    $isDark = $matrix->getModuleAt($row, 6)->isDark();
    $expected = ($row % 2 === 0);
    if ($isDark !== $expected) $vErrors++;
}

if ($hErrors === 0 && $vErrors === 0) {
    echo "✅ TIMING PATTERNS ARE CORRECT!\n";
} else {
    echo "❌ TIMING PATTERNS HAVE ERRORS!\n";
    echo "   Horizontal errors: {$hErrors}\n";
    echo "   Vertical errors: {$vErrors}\n";
}

