<?php
/**
 * DIAGNOSTIC TEST - Check what's actually happening
 */

if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

$current_lang = $_SESSION['selected_language'] ?? 'NOT SET';
$current_curr = $_SESSION['selected_currency'] ?? 'NOT SET';

// Direct session change test
if (isset($_GET['test_lang'])) {
    $_SESSION['selected_language'] = $_GET['test_lang'];
    $current_lang = $_GET['test_lang'];
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>DIAGNOSTIC TEST</title>
    <style>
        body { font-family: Arial; padding: 30px; background: #f5f5f5; }
        .container { max-width: 800px; margin: 0 auto; background: white; padding: 30px; border-radius: 10px; }
        .box { background: #f0f0f0; padding: 20px; margin: 20px 0; border-radius: 5px; }
        button { padding: 10px 15px; margin: 5px; background: #2196F3; color: white; border: none; border-radius: 5px; cursor: pointer; }
        table { width: 100%; border-collapse: collapse; margin: 10px 0; }
        td { padding: 10px; border: 1px solid #ddd; }
    </style>
</head>
<body>

<div class="container">
    <h1>🔍 DIAGNOSTIC TEST</h1>

    <div class="box" style="background: #e8f5e9;">
        <h2>Current Session Values</h2>
        <table>
            <tr>
                <td><strong>Language</strong></td>
                <td><code style="font-size: 18px; color: #2196F3;"><?php echo $current_lang; ?></code></td>
            </tr>
            <tr>
                <td><strong>Currency</strong></td>
                <td><code style="font-size: 18px; color: #2196F3;"><?php echo $current_curr; ?></code></td>
            </tr>
            <tr>
                <td><strong>Session ID</strong></td>
                <td><code><?php echo session_id(); ?></code></td>
            </tr>
        </table>
    </div>

    <div class="box">
        <h2>Test 1: Direct Session Change</h2>
        <p>Click to change language directly via URL (tests if sessions work):</p>
        <button onclick="location.href='?test_lang=ENG'">Set ENG</button>
        <button onclick="location.href='?test_lang=ITA'">Set ITA</button>
        <button onclick="location.href='?test_lang=ESP'">Set ESP</button>
        <button onclick="location.href='?test_lang=GER'">Set GER</button>
        <p style="color: #666;">If language above changes, sessions ARE working ✓</p>
    </div>

    <div class="box">
        <h2>Test 2: Check API</h2>
        <button onclick="testAPI()">Test API Call</button>
        <p style="color: #666;">Check console (F12) for response</p>
    </div>

    <div class="box">
        <h2>Test 3: Check Function Exists</h2>
        <button onclick="checkFunctions()">Check changeLanguage() & changeCurrency()</button>
        <p style="color: #666;">Check console (F12) for result</p>
    </div>

    <div class="box">
        <h2>Files Check</h2>
        <table>
            <tr>
                <td>api_set_preference.php</td>
                <td><?php echo file_exists(__DIR__ . '/api_set_preference.php') ? '✓' : '✗'; ?></td>
            </tr>
            <tr>
                <td>config/currency_converter.php</td>
                <td><?php echo file_exists(__DIR__ . '/config/currency_converter.php') ? '✓' : '✗'; ?></td>
            </tr>
        </table>
    </div>

</div>

<script>
console.log('DIAGNOSTIC LOADED');
console.log('Current Language:', '<?php echo $current_lang; ?>');
console.log('Current Currency:', '<?php echo $current_curr; ?>');

function testAPI() {
    console.log('Testing API...');
    fetch('/emporio-sport/api_set_preference.php', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ type: 'language', value: 'TEST' })
    })
    .then(r => r.json())
    .then(data => console.log('API Response:', data))
    .catch(e => console.error('API Error:', e));
}

function checkFunctions() {
    console.log('changeLanguage exists:', typeof changeLanguage === 'function' ? '✓' : '✗');
    console.log('changeCurrency exists:', typeof changeCurrency === 'function' ? '✓' : '✗');
}
</script>

</body>
</html>
