<?php
require_once __DIR__ . '/../config/database.php';

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

$token = get_auth_token();

if (!$token) {
    http_response_code(401);
    echo json_encode(['error' => 'unauthorized']);
    exit;
}

$mode = $_GET['mode'] ?? 'blitz';
$validModes = ['blitz', 'rapid', 'bullet'];
if (!in_array($mode, $validModes)) {
    $mode = 'blitz';
}

$orderField = 'elo_' . $mode;
$res = supabase_rest('GET', "profiles?select=id,username,display_name,elo_blitz,elo_rapid,elo_bullet&order={$orderField}.desc&limit=50", [], SUPABASE_SERVICE_KEY);

echo json_encode(['players' => $res['data'] ?? []]);
