<?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 main_balance FROM wallets WHERE user_id = ?");
    $stmt->execute([$_SESSION['user_id']]);
    $bal = $stmt->fetchColumn() ?: 0.00;
    
    echo json_encode(["success" => true, "balance" => number_format((float)$bal, 2, '.', '')]);
} catch(Exception $e) { 
    echo json_encode(["success" => false]); 
}
?>