<?php
/**
 * SmartForex Engine - User Personal Trade History API
 * Location: public_html/api/user_history.php
 */

error_reporting(0);
ini_set('display_errors', 0);

define('DB_HOST', 'localhost');
define('DB_NAME', 'rakshita_relay');
define('DB_USER', 'rakshita_relay');
define('DB_PASS', 'rehman@169');

try {
    $pdo = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=utf8mb4", DB_USER, DB_PASS, [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
    ]);
} catch (PDOException $e) {
    exit(json_encode([]));
}

session_start();
if (!isset($_SESSION['user_id'])) {
    exit(json_encode([]));
}

$user_id = $_SESSION['user_id'];

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

// Fetch complete closed and cancelled ledger for current user context
$stmt = $pdo->prepare("SELECT symbol, type, amount, leverage, entry_price, close_price, profit_loss, status, opened_at FROM trades WHERE user_id = ? ORDER BY id DESC");
$stmt->execute([$user_id]);
echo json_encode($stmt->fetchAll());
exit;