<?php
// get_leaderboard.php
header('Content-Type: application/json');
require 'db.php';

try {
    // We only show guests who have confirmed access (access_granted_at set) and have a photo
    $stmt = $pdo->query("SELECT name, points, photo_path, vodka_ml, whisky_ml, liqueur_ml FROM guests WHERE access_granted_at IS NOT NULL AND photo_path IS NOT NULL AND photo_path != '' ORDER BY points DESC, name ASC");
    $leaderboard = $stmt->fetchAll();

    echo json_encode([
        'status' => 'success',
        'leaderboard' => $leaderboard
    ]);
} catch (Exception $e) {
    echo json_encode(['status' => 'error', 'message' => 'Błąd pobierania rankingu']);
}
?>