<?php
/**
 * User Transactions Page
 */
$pageTitle = 'Transactions';
require_once 'includes/header.php';

$filter = $_GET['type'] ?? 'all';
$page = max(1, (int)($_GET['page'] ?? 1));
$perPage = 25;

$whereClause = "user_id = :user_id";
$params = ['user_id' => $user['id']];

if ($filter !== 'all') {
    $whereClause .= " AND type = :type";
    $params['type'] = $filter;
}

$totalTx = dbCount('transactions', $whereClause, $params);
$totalPages = ceil($totalTx / $perPage);
$offset = ($page - 1) * $perPage;

$transactions = dbFetchAll(
    "SELECT * FROM transactions 
     WHERE {$whereClause}
     ORDER BY created_at DESC 
     LIMIT {$perPage} OFFSET {$offset}",
    $params
);

// Summary stats
$summary = dbFetch(
    "SELECT 
        SUM(CASE WHEN type = 'deposit' THEN amount ELSE 0 END) as total_deposits,
        SUM(CASE WHEN type = 'withdrawal' THEN ABS(amount) ELSE 0 END) as total_withdrawals,
        SUM(CASE WHEN type = 'trade_win' THEN amount ELSE 0 END) as total_winnings,
        SUM(CASE WHEN type = 'trade_loss' THEN ABS(amount) ELSE 0 END) as total_losses
     FROM transactions WHERE user_id = :id",
    ['id' => $user['id']]
);
?>

<style>
.summary-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1rem; margin-bottom: 1.5rem; }
@media (max-width: 992px) { .summary-grid { grid-template-columns: repeat(2, 1fr); } }
.filter-bar { display: flex; gap: 0.5rem; flex-wrap: wrap; }
.tx-icon {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
}
.tx-icon.deposit { background: rgba(0, 208, 156, 0.1); color: var(--accent-green); }
.tx-icon.withdrawal { background: rgba(255, 82, 82, 0.1); color: var(--accent-red); }
.tx-icon.trade { background: rgba(59, 130, 246, 0.1); color: var(--accent-blue); }
.tx-icon.bonus { background: rgba(251, 191, 36, 0.1); color: var(--accent-yellow); }
</style>

<div class="summary-grid">
    <div class="stat-card">
        <div class="stat-icon green"><i class="fas fa-arrow-down"></i></div>
        <div class="stat-value text-green"><?= formatCurrency($summary['total_deposits'] ?? 0) ?></div>
        <div class="stat-label">Total Deposits</div>
    </div>
    <div class="stat-card">
        <div class="stat-icon red"><i class="fas fa-arrow-up"></i></div>
        <div class="stat-value text-red"><?= formatCurrency($summary['total_withdrawals'] ?? 0) ?></div>
        <div class="stat-label">Total Withdrawals</div>
    </div>
    <div class="stat-card">
        <div class="stat-icon blue"><i class="fas fa-trophy"></i></div>
        <div class="stat-value text-green"><?= formatCurrency($summary['total_winnings'] ?? 0) ?></div>
        <div class="stat-label">Total Winnings</div>
    </div>
    <div class="stat-card">
        <div class="stat-icon yellow"><i class="fas fa-chart-line"></i></div>
        <div class="stat-value"><?= formatCurrency($summary['total_losses'] ?? 0) ?></div>
        <div class="stat-label">Total Losses</div>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <h3 class="card-title">Transaction History</h3>
        <div class="filter-bar">
            <a href="?type=all" class="btn btn-sm <?= $filter === 'all' ? 'btn-primary' : 'btn-secondary' ?>">All</a>
            <a href="?type=deposit" class="btn btn-sm <?= $filter === 'deposit' ? 'btn-primary' : 'btn-secondary' ?>">Deposits</a>
            <a href="?type=withdrawal" class="btn btn-sm <?= $filter === 'withdrawal' ? 'btn-primary' : 'btn-secondary' ?>">Withdrawals</a>
            <a href="?type=trade_win" class="btn btn-sm <?= $filter === 'trade_win' ? 'btn-primary' : 'btn-secondary' ?>">Wins</a>
            <a href="?type=trade_loss" class="btn btn-sm <?= $filter === 'trade_loss' ? 'btn-primary' : 'btn-secondary' ?>">Losses</a>
        </div>
    </div>
    <div class="card-body p-0">
        <?php foreach ($transactions as $tx): ?>
        <div class="d-flex align-items-center gap-3 p-3" style="border-bottom: 1px solid var(--border-color);">
            <div class="tx-icon <?= strpos($tx['type'], 'deposit') !== false || strpos($tx['type'], 'bonus') !== false ? 'deposit' : (strpos($tx['type'], 'withdrawal') !== false || strpos($tx['type'], 'loss') !== false ? 'withdrawal' : 'trade') ?>">
                <?php
                $icon = 'fa-exchange-alt';
                if (strpos($tx['type'], 'deposit') !== false) $icon = 'fa-arrow-down';
                elseif (strpos($tx['type'], 'withdrawal') !== false) $icon = 'fa-arrow-up';
                elseif (strpos($tx['type'], 'win') !== false) $icon = 'fa-trophy';
                elseif (strpos($tx['type'], 'loss') !== false) $icon = 'fa-times';
                elseif (strpos($tx['type'], 'bonus') !== false) $icon = 'fa-gift';
                elseif (strpos($tx['type'], 'referral') !== false) $icon = 'fa-users';
                ?>
                <i class="fas <?= $icon ?>"></i>
            </div>
            <div class="flex-1">
                <div class="font-semibold"><?= ucwords(str_replace('_', ' ', $tx['type'])) ?></div>
                <div class="text-muted" style="font-size: 0.75rem;">
                    <?= $tx['transaction_id'] ?> • <?= $tx['description'] ?>
                </div>
            </div>
            <div class="text-right">
                <div class="font-mono font-semibold <?= $tx['amount'] >= 0 ? 'text-green' : 'text-red' ?>">
                    <?= $tx['amount'] >= 0 ? '+' : '' ?><?= formatCurrency($tx['amount']) ?>
                </div>
                <div class="text-muted" style="font-size: 0.7rem;">
                    Bal: <?= formatCurrency($tx['balance_after']) ?>
                </div>
            </div>
            <div class="text-muted" style="font-size: 0.75rem; width: 100px; text-align: right;">
                <?= formatDateTime($tx['created_at'], 'd M Y') ?><br>
                <?= formatDateTime($tx['created_at'], 'H:i') ?>
            </div>
        </div>
        <?php endforeach; ?>
        
        <?php if (empty($transactions)): ?>
        <div class="p-4 text-center text-muted">No transactions found</div>
        <?php endif; ?>
        
        <?php if ($totalPages > 1): ?>
        <div class="p-3 d-flex justify-content-center gap-2">
            <?php if ($page > 1): ?>
            <a href="?type=<?= $filter ?>&page=<?= $page - 1 ?>" class="btn btn-sm btn-secondary">
                <i class="fas fa-chevron-left"></i> Prev
            </a>
            <?php endif; ?>
            <span class="px-3 py-2">Page <?= $page ?> of <?= $totalPages ?></span>
            <?php if ($page < $totalPages): ?>
            <a href="?type=<?= $filter ?>&page=<?= $page + 1 ?>" class="btn btn-sm btn-secondary">
                Next <i class="fas fa-chevron-right"></i>
            </a>
            <?php endif; ?>
        </div>
        <?php endif; ?>
    </div>
</div>

<?php require_once 'includes/footer.php'; ?>
