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

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

    $user_id = $_SESSION['user_id'];

    $query = $conn->prepare("SELECT account_balance FROM users WHERE id = ?");
    $query->bind_param('i', $user_id);
    $query->execute();
    $query->bind_result($balance);

    if ($query->fetch()) {
        echo json_encode(['success' => true, 'balance' => number_format((float)$balance, 2, '.', '')]);
    } else {
        echo json_encode(['success' => false, 'message' => 'User not found']);
    }

    $query->close();
    $conn->close();
?>
