<?php
session_start();
require_once '../db/config.php';

header('Content-Type: application/json');

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

$user_id = $_SESSION['user_id'];

try {
    $db = db();
    
    // Get USDT balance
    $stmt = $db->prepare("SELECT balance FROM users WHERE id = ?");
    $stmt->execute([$user_id]);
    $usdt = $stmt->fetchColumn();
    
    // Get other assets
    $stmt = $db->prepare("SELECT symbol, amount FROM user_assets WHERE user_id = ? AND amount > 0");
    $stmt->execute([$user_id]);
    $other_assets = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    $assets = [['symbol' => 'USDT', 'amount' => (float)$usdt]];
    foreach ($other_assets as $asset) {
        $assets[] = [
            'symbol' => $asset['symbol'],
            'amount' => (float)$asset['amount']
        ];
    }
    
    echo json_encode(['success' => true, 'data' => $assets]);

} catch (Exception $e) {
    echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}
