<?php
/**
 * Simple Exchange Rate Helper
 * Fetches real-time exchange rates from a public API and caches them locally.
 */

function get_exchange_rates() {
    $cache_file = __DIR__ . '/../db/exchange_rates.json';
    $cache_time = 3600; // Cache for 1 hour

    if (file_exists($cache_file) && (time() - filemtime($cache_file) < $cache_time)) {
        return json_decode(file_get_contents($cache_file), true);
    }

    // Use a free public API
    $api_url = 'https://api.exchangerate-api.com/v4/latest/USD';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $api_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    $response = curl_exec($ch);
    curl_close($ch);

    if ($response) {
        $data = json_decode($response, true);
        if ($data && isset($data['rates'])) {
            // We want rates relative to USD (which is 1:1 with USDT for this purpose)
            $rates = $data['rates'];
            file_put_contents($cache_file, json_encode($rates));
            return $rates;
        }
    }

    // Fallback rates if API fails
    return [
        'USD' => 1,
        'EUR' => 0.92,
        'GBP' => 0.79,
        'CNY' => 7.23,
        'JPY' => 150.1,
        'HKD' => 7.82,
        'TWD' => 31.6,
        'KRW' => 1332.0
    ];
}

function get_rate($currency) {
    $rates = get_exchange_rates();
    return $rates[$currency] ?? 1.0;
}

function get_global_currencies() {
    return [
        'USD' => ['name' => __('usd_name'), 'flag' => '🇺🇸'],
        'EUR' => ['name' => __('eur_name'), 'flag' => '🇪🇺'],
        'GBP' => ['name' => __('gbp_name'), 'flag' => '🇬🇧'],
        'JPY' => ['name' => __('jpy_name'), 'flag' => '🇯🇵'],
        'CNY' => ['name' => __('cny_name'), 'flag' => '🇨🇳'],
        'HKD' => ['name' => __('hkd_name'), 'flag' => '🇭🇰'],
        'TWD' => ['name' => __('twd_name'), 'flag' => '🇹🇼'],
        'KRW' => ['name' => __('krw_name'), 'flag' => '🇰🇷'],
        'SGD' => ['name' => __('sgd_name'), 'flag' => '🇸🇬'],
        'MYR' => ['name' => __('myr_name'), 'flag' => '🇲🇾'],
        'THB' => ['name' => __('thb_name'), 'flag' => '🇹🇭'],
        'VND' => ['name' => __('vnd_name'), 'flag' => '🇻🇳'],
        'PHP' => ['name' => __('php_name'), 'flag' => '🇵🇭'],
        'IDR' => ['name' => __('idr_name'), 'flag' => '🇮🇩'],
        'AUD' => ['name' => __('aud_name'), 'flag' => '🇦🇺'],
        'CAD' => ['name' => __('cad_name'), 'flag' => '🇨🇦'],
        'CHF' => ['name' => __('chf_name'), 'flag' => '🇨🇭'],
        'BRL' => ['name' => __('brl_name'), 'flag' => '🇧🇷'],
        'RUB' => ['name' => __('rub_name'), 'flag' => '🇷🇺'],
        'INR' => ['name' => __('inr_name'), 'flag' => '🇮🇳'],
        'ZAR' => ['name' => __('zar_name'), 'flag' => '🇿🇦'],
        'TRY' => ['name' => __('try_name'), 'flag' => '🇹🇷'],
        'AED' => ['name' => __('aed_name'), 'flag' => '🇦🇪'],
        'SAR' => ['name' => __('sar_name'), 'flag' => '🇸🇦'],
        'MXN' => ['name' => __('mxn_name'), 'flag' => '🇲🇽'],
        'NZD' => ['name' => 'NZD', 'flag' => '🇳🇿'],
        'SEK' => ['name' => 'SEK', 'flag' => '🇸🇪'],
        'NOK' => ['name' => 'NOK', 'flag' => '🇳🇴'],
        'DKK' => ['name' => 'DKK', 'flag' => '🇩🇰'],
        'ILS' => ['name' => 'ILS', 'flag' => '🇮🇱'],
        'PLN' => ['name' => 'PLN', 'flag' => '🇵🇱'],
        'PKR' => ['name' => 'PKR', 'flag' => '🇵🇰'],
        'EGP' => ['name' => 'EGP', 'flag' => '🇪🇬'],
        'ARS' => ['name' => 'ARS', 'flag' => '🇦🇷'],
        'CLP' => ['name' => 'CLP', 'flag' => '🇨🇱'],
        'COP' => ['name' => 'COP', 'flag' => '🇨🇴'],
        'PEN' => ['name' => 'PEN', 'flag' => '🇵🇪'],
        'KWD' => ['name' => 'KWD', 'flag' => '🇰🇼'],
        'QAR' => ['name' => 'QAR', 'flag' => '🇶🇦'],
    ];
}
