<?php
// balance.php
if (session_status() === PHP_SESSION_NONE) session_start();
header('Content-Type: application/json');
require_once __DIR__ . '/config.php';

if (!isset($_SESSION['user_id'])) {
    echo json_encode(['ok' => false, 'msg' => 'Unauthorized']);
    exit;
}

$uid = (int)$_SESSION['user_id'];
$st = $pdo->prepare("SELECT balance, wager FROM users WHERE id = ?");
$st->execute([$uid]);
$user = $st->fetch();

if ($user) {
    echo json_encode([
        'ok' => true,
        'balance' => (float)$user['balance'],
        'wager' => (float)$user['wager'],
        'withdrawable' => ($user['wager'] <= 0) ? (float)$user['balance'] : 0
    ]);
} else {
    echo json_encode(['ok' => false]);
}