<?php
/**
 * Currency and Language Preference Handler
 * Manages user preferences with real-time conversion rates
 */

// CRITICAL: Configure session BEFORE starting it
if (session_status() !== PHP_SESSION_ACTIVE) {
    ini_set('session.use_strict_mode', 1);
    ini_set('session.use_only_cookies', 1);
    session_start();
}

header('Content-Type: application/json');

// Exchange rates (in production, fetch from an API)
$exchangeRates = [
    'eur' => 1.0,
    'usd' => 1.10,
    'gbp' => 0.87,
    'cad' => 1.48
];

// Language translations (basic implementation)
$translations = [
    'eng' => [
        'name' => 'English',
        'locale' => 'en_US'
    ],
    'ita' => [
        'name' => 'Italiano',
        'locale' => 'it_IT'
    ],
    'esp' => [
        'name' => 'Español',
        'locale' => 'es_ES'
    ],
    'ger' => [
        'name' => 'Deutsch',
        'locale' => 'de_DE'
    ]
];

$response = ['success' => false, 'message' => 'Invalid request'];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $rawData = file_get_contents('php://input');
    $data = @json_decode($rawData, true);
    
    if (is_array($data)) {
        if (!empty($data['type']) && !empty($data['value'])) {
            $type = strtolower($data['type']);
            $value = strtolower($data['value']);
            
            if ($type === 'language') {
                if (isset($translations[$value])) {
                    $_SESSION['selected_language'] = $value;
                    setlocale(LC_ALL, $translations[$value]['locale']);
                    session_write_close();
                    $response = [
                        'success' => true,
                        'message' => 'Language updated successfully',
                        'language' => $value,
                        'language_name' => $translations[$value]['name'],
                        'session_id' => session_id(),
                        'debug_session_value' => $_SESSION['selected_language'] ?? 'NOT SET'
                    ];
                } else {
                    $response = ['success' => false, 'message' => 'Invalid language'];
                }
            } elseif ($type === 'currency') {
                if (isset($exchangeRates[$value])) {
                    $_SESSION['selected_currency'] = $value;
                    session_write_close();
                    $response = [
                        'success' => true,
                        'message' => 'Currency updated successfully',
                        'currency' => $value,
                        'exchange_rate' => $exchangeRates[$value],
                        'session_id' => session_id(),
                        'debug_session_value' => $_SESSION['selected_currency'] ?? 'NOT SET'
                    ];
                } else {
                    $response = ['success' => false, 'message' => 'Invalid currency'];
                }
            }
        }
    }
} elseif ($_SERVER['REQUEST_METHOD'] === 'GET') {
    // Return current preferences
    $currentLang = $_SESSION['selected_language'] ?? 'eng';
    $currentCurr = $_SESSION['selected_currency'] ?? 'usd';
    
    $response = [
        'success' => true,
        'current_language' => $currentLang,
        'current_currency' => $currentCurr,
        'exchange_rate' => $exchangeRates[$currentCurr] ?? 1.0,
        'language_name' => $translations[$currentLang]['name'] ?? 'English'
    ];
}

echo json_encode($response);
?>
