<?php
include 'config.php';

// JSON আউটপুট
header('Content-Type: application/json');

// মোট balance, winning এবং user সংখ্যা বের করা
$sql = "SELECT 
            SUM(CAST(balance AS DECIMAL(20,2))) AS total_balance, 
            SUM(CAST(winning AS DECIMAL(20,2))) AS total_winning,
            COUNT(*) AS total_users
        FROM users";

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

// AddMoney pending count
$sql_add = "SELECT COUNT(*) AS total_addmoney_pending FROM addmoney WHERE status = 'Pending'";
$res_add = $conn->query($sql_add);
$row_add = $res_add ? $res_add->fetch_assoc() : ['total_addmoney_pending' => 0];

// Withdraw pending count
$sql_with = "SELECT COUNT(*) AS total_withdraw_pending FROM withdraw WHERE status = 'Pending'";
$res_with = $conn->query($sql_with);
$row_with = $res_with ? $res_with->fetch_assoc() : ['total_withdraw_pending' => 0];

if ($result && $row = $result->fetch_assoc()) {
    echo json_encode([
        "total_balance"         => (float)$row['total_balance'],
        "total_winning"         => (float)$row['total_winning'],
        "total_users"           => (int)$row['total_users'],
        "total_addmoney_pending"=> (int)$row_add['total_addmoney_pending'],
        "total_withdraw_pending"=> (int)$row_with['total_withdraw_pending']
    ]);
} else {
    echo json_encode([
        "total_balance"         => 0,
        "total_winning"         => 0,
        "total_users"           => 0,
        "total_addmoney_pending"=> 0,
        "total_withdraw_pending"=> 0
    ]);
}

$conn->close();
?>