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

require_once __DIR__ . '/../config/config.php';

if (!defined('DATA_SOURCE') || DATA_SOURCE !== 'api') {
    die("Error: System is not running in API Data Source Mode. Please update your settings.");
}

$api_key = defined('FOOTBALL_API_KEY') ? FOOTBALL_API_KEY : '';
$api_url = defined('FOOTBALL_API_URL') ? FOOTBALL_API_URL : 'https://v3.football.api-sports.io/';

// Endpoint to check currently live matches globally
$endpoint = rtrim($api_url, '/') . "/fixtures?live=all";

$curl = curl_init();
curl_setopt_array($curl, [
    CURLOPT_URL => $endpoint,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_HTTPHEADER => [
        "x-apisports-key: " . $api_key,
        "Accept: application/json"
    ],
]);

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);

?>
<!DOCTYPE html>
<html lang="en" class="dark">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>API Global Coverage Test</title>
    <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-slate-950 text-slate-300 p-8 font-mono">
    <div class="max-w-4xl mx-auto">
        <h1 class="text-2xl font-black text-white mb-1 text-cyan-400">Global API Coverage Terminal</h1>
        <p class="text-[10px] text-slate-500 mb-6 uppercase tracking-widest">Testing availability of leagues, world cup, and live fixtures</p>

        <?php if ($err): ?>
            <div class="bg-rose-500/10 border border-rose-500 text-rose-400 p-4 rounded-xl text-xs">
                <strong>cURL Error:</strong> <?php echo htmlspecialchars($err); ?>
            </div>
        <?php else: ?>
            <?php $data = json_decode($response, true); ?>
            
            <div class="bg-slate-900 border border-slate-800 p-4 rounded-xl mb-6 text-sm flex justify-between items-center">
                <div>
                    <span class="text-slate-500 text-xs block uppercase tracking-wider">Live Global Matches Found</span>
                    <span class="text-white font-black text-lg"><?php echo isset($data['results']) ? $data['results'] : 0; ?></span>
                </div>
                <div class="text-right">
                    <span class="text-slate-500 text-xs block uppercase tracking-wider">Status</span>
                    <span class="<?php echo isset($data['errors']) && empty($data['errors']) ? 'text-emerald-400' : 'text-rose-400'; ?> font-black text-xs uppercase">
                        <?php echo empty($data['errors']) ? 'API Responding Perfectly' : 'Errors Detected'; ?>
                    </span>
                </div>
            </div>

            <h2 class="text-xs font-bold text-white mb-2 uppercase tracking-widest">JSON Response Preview (Live Matches Payload):</h2>
            <div class="bg-[#0b1120] border border-slate-800 rounded-xl p-6 overflow-x-auto h-96">
                <pre class="text-[10px] text-emerald-400 leading-relaxed"><?php echo json_encode($data, JSON_PRETTY_PRINT); ?></pre>
            </div>
            
            <div class="mt-6 bg-slate-900/60 border border-slate-800 p-4 rounded-xl text-[10px] text-slate-400 leading-normal">
                <p><strong>Note:</strong> If the response above returns <code>"results": 0</code> and an empty array, it simply means there are no football matches currently live at this exact second in the API's database. To explicitly check for official League IDs or World Cup availability, you can also navigate to <code><?php echo rtrim($api_url, '/'); ?>/leagues</code> in your browser while authenticated.</p>
            </div>
        <?php endif; ?>
    </div>
</body>
</html>