<?php
session_start();
require_once 'config.php';
header('Content-Type: application/json');

if (!isset($_SESSION['user_id'])) { echo json_encode(["success"=>false]); exit; }

try {
    global $pdo;
    $stmt = $pdo->prepare("SELECT period, selection, amount, status, win_amount, created_at FROM bets WHERE user_id = ? ORDER BY id DESC LIMIT 20");
    $stmt->execute([$_SESSION['user_id']]);
    $history = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo json_encode(["success" => true, "data" => $history]);
} catch (Throwable $e) {
    echo json_encode(["success" => false, "data" => []]);
}
?>