<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "<h1>Translation Test</h1>";

$lang = $_GET['lang'] ?? 'es';
echo "<p>Testing language: <strong>$lang</strong></p>";

$basePath = dirname(__DIR__) . '/lang/' . $lang;

$files = [
    'app/register.php',
    'app/main.php',
    'mail/verification.php',
    'auth.php',
    'passwords.php',
    'validation.php',
];

foreach ($files as $file) {
    $fullPath = $basePath . '/' . $file;
    echo "<h3>$file</h3>";

    if (!file_exists($fullPath)) {
        echo "<p style='color:red'>❌ File does not exist!</p>";
        continue;
    }

    try {
        $data = include($fullPath);
        if (!is_array($data)) {
            echo "<p style='color:red'>❌ File did not return an array!</p>";
        } else {
            echo "<p style='color:green'>✅ Valid - " . count($data) . " keys</p>";

            if ($file === 'app/register.php') {
                $verificationKeys = array_filter(array_keys($data), fn($k) => str_starts_with($k, 'verification.'));
                echo "<p>Verification keys: " . count($verificationKeys) . "</p>";
                if (count($verificationKeys) > 0) {
                    echo "<ul>";
                    foreach ($verificationKeys as $key) {
                        echo "<li><code>$key</code> = " . htmlspecialchars(substr($data[$key], 0, 50)) . "...</li>";
                    }
                    echo "</ul>";
                }
            }
        }
    } catch (Throwable $e) {
        echo "<p style='color:red'>❌ Error: " . htmlspecialchars($e->getMessage()) . "</p>";
        echo "<pre>" . htmlspecialchars($e->getTraceAsString()) . "</pre>";
    }
}

echo "<hr><p>Test other languages: ";
foreach (['en', 'ro', 'es', 'fr', 'de', 'it', 'pl', 'ru', 'pt', 'tr'] as $l) {
    echo "<a href='?lang=$l'>$l</a> | ";
}
echo "</p>";
