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

require_once "../include/db.php";

if (!isset($_SESSION['user_id'])) {
    echo json_encode(['html' => '']);
    exit;
}

$userId = intval($_SESSION['user_id']);
$limit = intval($_GET['limit'] ?? 10);

$sql = "SELECT * FROM trades 
    WHERE user_id = $userId AND status IN ('win', 'loss', 'draw') 
    ORDER BY closed_at DESC 
    LIMIT $limit";
$result = mysqli_query($conn, $sql);

ob_start();
if (mysqli_num_rows($result) === 0): ?>
    <div class="p-8 text-center text-slate-500 text-sm">No trade history yet</div>
<?php else: ?>
    <table class="w-full text-left text-sm">
        <thead class="bg-slate-50 dark:bg-slate-800/50 text-xs uppercase text-slate-500">
            <tr>
                <th class="px-4 py-3 font-medium">Asset</th>
                <th class="px-4 py-3 font-medium">Type</th>
                <th class="px-4 py-3 font-medium">Amount</th>
                <th class="px-4 py-3 font-medium">Result</th>
                <th class="px-4 py-3 font-medium">P&L</th>
                <th class="px-4 py-3 font-medium">Duration</th>
                <th class="px-4 py-3 font-medium">Closed</th>
            </tr>
        </thead>
        <tbody class="divide-y divide-slate-200 dark:divide-slate-700/50">
            <?php while ($trade = mysqli_fetch_assoc($result)): 
                $isWin = $trade['status'] === 'win';
                $pnl = floatval($trade['pnl'] ?? 0);
                $tradeType = strtoupper($trade['trade_type']);
            ?>
            <tr class="hover:bg-slate-50 dark:hover:bg-slate-800/30 transition-colors">
                <td class="px-4 py-3 font-medium text-slate-900 dark:text-white">
                    <div class="flex items-center gap-2">
                        <span class="text-[10px] px-1.5 py-0.5 rounded bg-slate-100 dark:bg-slate-700 text-slate-500"><?= strtoupper($trade['market'] ?? 'CRYPTO') ?></span>
                        <?= htmlspecialchars($trade['asset']) ?>
                    </div>
                </td>
                <td class="px-4 py-3">
                    <span class="text-xs font-bold <?= $tradeType === 'BUY' ? 'text-emerald-500' : 'text-rose-500' ?>">
                        <?= $tradeType ?>
                    </span>
                </td>
                <td class="px-4 py-3 text-slate-600 dark:text-slate-400">$<?= number_format($trade['amount'], 2) ?></td>
                <td class="px-4 py-3">
                    <span class="px-2 py-1 text-[10px] font-bold rounded-full <?= $isWin ? 'badge-win' : 'badge-loss' ?>">
                        <?= strtoupper($trade['status']) ?>
                    </span>
                </td>
                <td class="px-4 py-3 font-mono font-bold <?= $pnl >= 0 ? 'text-emerald-500' : 'text-rose-500' ?>">
                    <?= $pnl >= 0 ? '+' : '' ?>$<?= number_format(abs($pnl), 2) ?>
                </td>
                <td class="px-4 py-3 text-xs text-slate-500"><?= $trade['duration'] ?>m</td>
                <td class="px-4 py-3 text-xs text-slate-500"><?= date('M d, H:i', strtotime($trade['closed_at'])) ?></td