<?php
// 1. API Configuration
$api_key = '5166ce33c6b95ec8adba7f26e037f088'; // Your Direct API-Sports Key
$date = date('Y-m-d'); // Gets today's date automatically

// 2. We will fetch matches happening today. 
// (We are limiting it to the English Premier League - League ID 39, Season 2023 - to keep the data clean and readable for our test)
$url = "https://v3.football.api-sports.io/fixtures?league=39&season=2023&last=2";

// Initialize cURL session
$curl = curl_init();

curl_setopt_array($curl, [
    CURLOPT_URL => $url,
    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"
    ],
]);

// Execute the request
$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 Matrix 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-5xl mx-auto">
        <h1 class="text-2xl font-black text-white mb-2 text-cyan-400">API Connection Terminal</h1>
        <p class="text-xs text-slate-500 mb-8">Pinging: <?php echo htmlspecialchars($url); ?></p>

        <?php if ($err): ?>
            <div class="bg-rose-500/10 border border-rose-500 text-rose-400 p-4 rounded-xl">
                <strong>cURL Error:</strong> <?php echo htmlspecialchars($err); ?>
            </div>
        <?php else: ?>
            
            <?php 
            // Decode the JSON so we can format it nicely
            $data = json_decode($response, true); 
            ?>
            
            <div class="grid grid-cols-2 gap-4 mb-6 text-sm">
                <div class="bg-slate-900 border border-slate-800 p-4 rounded-xl">
                    <span class="text-slate-500 block">API Status</span>
                    <?php if(isset($data['errors']) && count($data['errors']) > 0): ?>
                        <span class="text-rose-400 font-bold">Error Detected</span>
                    <?php else: ?>
                        <span class="text-emerald-400 font-bold">Connected Successfully</span>
                    <?php endif; ?>
                </div>
                <div class="bg-slate-900 border border-slate-800 p-4 rounded-xl">
                    <span class="text-slate-500 block">Matches Found Today</span>
                    <span class="text-white font-bold"><?php echo isset($data['results']) ? $data['results'] : 0; ?></span>
                </div>
            </div>

            <h2 class="text-sm font-bold text-white mb-2">Raw JSON Payload (Database Blueprint):</h2>
            <div class="bg-[#0b1120] border border-slate-800 rounded-xl p-6 overflow-x-auto shadow-2xl">
                <pre class="text-[11px] text-emerald-400 leading-relaxed"><?php echo json_encode($data, JSON_PRETTY_PRINT); ?></pre>
            </div>

        <?php endif; ?>
    </div>

</body>
</html>