<?php
header('Content-Type: application/json; charset=utf-8');
include 'connection.php';

try {
    // Fetch distinct draw numbers with their latest date
    $sql = "SELECT Draw_Number, MIN(Draw_Date) AS Draw_Date 
            FROM Past_Draw_History 
            GROUP BY Draw_Number 
            ORDER BY Draw_Number DESC";

    $result = $conn->query($sql);

    $draws = [];
    while ($row = $result->fetch_assoc()) {
        $draws[] = [
            "Draw_Number" => $row['Draw_Number'],
            "Draw_Date"   => $row['Draw_Date']
        ];
    }

    echo json_encode([
        "status" => "success",
        "data"   => $draws
    ]);

} catch (Exception $e) {
    echo json_encode([
        "status" => "error",
        "message" => $e->getMessage()
    ]);
}
?>
