<?php
// Enable all error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('log_errors', 1);

echo "<h1>Debug 500 Error</h1>";
echo "<p>PHP Version: " . phpversion() . "</p>";

// Test 1: Can we load Laravel?
echo "<h2>Test 1: Load Laravel Bootstrap</h2>";
try {
    require __DIR__ . '/../vendor/autoload.php';
    echo "<p style='color:green'>✅ Autoloader loaded</p>";
} catch (Throwable $e) {
    echo "<p style='color:red'>❌ Error loading autoloader: " . $e->getMessage() . "</p>";
    die();
}

try {
    $app = require_once __DIR__ . '/../bootstrap/app.php';
    echo "<p style='color:green'>✅ Laravel app bootstrapped</p>";
} catch (Throwable $e) {
    echo "<p style='color:red'>❌ Error bootstrapping Laravel: " . $e->getMessage() . "</p>";
    echo "<pre>" . $e->getTraceAsString() . "</pre>";
    die();
}

// Test 2: Can we boot the kernel?
echo "<h2>Test 2: Boot Kernel</h2>";
try {
    $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
    echo "<p style='color:green'>✅ Kernel created</p>";
} catch (Throwable $e) {
    echo "<p style='color:red'>❌ Error creating kernel: " . $e->getMessage() . "</p>";
    echo "<pre>" . $e->getTraceAsString() . "</pre>";
    die();
}

// Test 3: Switch to Spanish locale
echo "<h2>Test 3: Switch to Spanish Locale</h2>";
try {
    app()->setLocale('es');
    echo "<p style='color:green'>✅ Locale set to: " . app()->getLocale() . "</p>";
} catch (Throwable $e) {
    echo "<p style='color:red'>❌ Error setting locale: " . $e->getMessage() . "</p>";
    echo "<pre>" . $e->getTraceAsString() . "</pre>";
}

// Test 4: Try translating some keys
echo "<h2>Test 4: Test Translations</h2>";
$keys = [
    'app/main.title',
    'app/register.title',
    'app/register.verification.title',
    'mail/verification.subject',
];

foreach ($keys as $key) {
    try {
        $translation = __($key);
        if ($translation === $key) {
            echo "<p style='color:orange'>⚠️ <code>$key</code> returned key itself (missing translation)</p>";
        } else {
            echo "<p style='color:green'>✅ <code>$key</code> = " . htmlspecialchars($translation) . "</p>";
        }
    } catch (Throwable $e) {
        echo "<p style='color:red'>❌ Error translating <code>$key</code>: " . $e->getMessage() . "</p>";
        echo "<pre>" . $e->getTraceAsString() . "</pre>";
    }
}

// Test 5: Simulate a registration redirect (what happens after registration)
echo "<h2>Test 5: Test Registration Flow</h2>";
try {
    // This is what happens after registration - redirect to verification notice
    echo "<p>After registration, user is redirected to: <code>/user/verification/notice</code></p>";
    echo "<p>This view uses these translation keys:</p>";
    echo "<ul>";
    echo "<li>app/register.verification.title</li>";
    echo "<li>app/register.verification.activate-account</li>";
    echo "<li>app/register.verification.welcome</li>";
    echo "<li>app/register.verification.registration-complete</li>";
    echo "<li>app/register.verification.email-sent</li>";
    echo "</ul>";

    $verificationKeys = [
        'app/register.verification.title',
        'app/register.verification.activate-account',
        'app/register.verification.welcome',
        'app/register.verification.registration-complete',
        'app/register.verification.email-sent',
    ];

    $allPresent = true;
    foreach ($verificationKeys as $key) {
        $trans = __($key);
        if ($trans === $key) {
            echo "<p style='color:red'>❌ Missing: <code>$key</code></p>";
            $allPresent = false;
        }
    }

    if ($allPresent) {
        echo "<p style='color:green'><strong>✅ All verification keys present!</strong></p>";
    }
} catch (Throwable $e) {
    echo "<p style='color:red'>❌ Error in registration flow test: " . $e->getMessage() . "</p>";
    echo "<pre>" . $e->getTraceAsString() . "</pre>";
}

echo "<hr>";
echo "<p><a href='/'>Go to homepage</a></p>";
