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

if (!isset($_SESSION['user_id'])) {
    echo json_encode(["success" => false, "allowed" => false, "message" => "Please login first!"]);
    exit;
}

try {
    global $pdo;
    $userId = $_SESSION['user_id'];
    
    // Deposit table se total successful deposit check karo
    $stmt = $pdo->prepare("SELECT SUM(amount) FROM deposit_requests WHERE user_id = ? AND status = 'success'");
    $stmt->execute([$userId]);
    $totalRecharge = $stmt->fetchColumn() ?: 0;

    if ($totalRecharge >= 0) {
        echo json_encode(["success" => true, "allowed" => true]);
    } else {
        echo json_encode(["success" => true, "allowed" => false, "message" => "Dear User, Please recharge a minimum of ₹300 to activate your account and start playing!"]);
    }
} catch (Throwable $e) {
    // Agar abhi deposit table nahi bani hai, toh error na de, testing ke liye allow kar de
    echo json_encode(["success" => true, "allowed" => true, "message" => "Dev Mode: Table missing, allowing bet."]);
}
?>