<?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();
    
    // Fetch from transactions table
    $stmt = $db->prepare("SELECT * FROM transactions WHERE user_id = ? ORDER BY created_at DESC");
    $stmt->execute([$user_id]);
    $transactions = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    // Also fetch from orders (withdrawals/deposits) if they are not yet in transactions (for compatibility)
    // Actually, I just added transactions logging to withdraw, so new ones will be there.
    // For deposits, I should check if they are logged. 
    
    echo json_encode(['success' => true, 'data' => $transactions]);

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