<?php
/**
 * Admin Reports
 */
require_once 'includes/header.php';

$period = $_GET['period'] ?? 'today';
$dateConditions = [
    'today' => "DATE(created_at) = CURDATE()",
    'week' => "created_at >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)",
    'month' => "created_at >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)",
    'year' => "created_at >= DATE_SUB(CURDATE(), INTERVAL 1 YEAR)"
];
$dateCondition = $dateConditions[$period] ?? "1=1";

$stats = [
    'total_users' => dbFetch("SELECT COUNT(*) as c FROM users WHERE $dateCondition")['c'] ?? 0,
    'total_deposits' => dbFetch("SELECT COALESCE(SUM(amount),0) as s FROM deposits WHERE status = 'approved' AND $dateCondition")['s'] ?? 0,
    'total_withdrawals' => dbFetch("SELECT COALESCE(SUM(amount),0) as s FROM withdrawals WHERE status = 'approved' AND $dateCondition")['s'] ?? 0,
    'total_trades' => dbFetch("SELECT COUNT(*) as c FROM trades WHERE $dateCondition")['c'] ?? 0,
    'trade_volume' => dbFetch("SELECT COALESCE(SUM(amount),0) as s FROM trades WHERE $dateCondition")['s'] ?? 0,
    'won_trades' => dbFetch("SELECT COUNT(*) as c FROM trades WHERE result = 'won' AND $dateCondition")['c'] ?? 0,
    'lost_trades' => dbFetch("SELECT COUNT(*) as c FROM trades WHERE result = 'lost' AND $dateCondition")['c'] ?? 0,
    'platform_profit' => dbFetch("SELECT COALESCE(SUM(CASE WHEN result = 'lost' THEN amount ELSE 0 END) - SUM(CASE WHEN result = 'won' THEN payout ELSE 0 END), 0) as p FROM trades WHERE result IN ('won','lost') AND $dateCondition")['p'] ?? 0
];
?>

<div class="page-header">
    <h1 class="page-title">Reports & Analytics</h1>
    <div class="d-flex gap-2">
        <a href="?period=today" class="btn <?= $period === 'today' ? 'btn-primary' : 'btn-secondary' ?>">Today</a>
        <a href="?period=week" class="btn <?= $period === 'week' ? 'btn-primary' : 'btn-secondary' ?>">Week</a>
        <a href="?period=month" class="btn <?= $period === 'month' ? 'btn-primary' : 'btn-secondary' ?>">Month</a>
        <a href="?period=year" class="btn <?= $period === 'year' ? 'btn-primary' : 'btn-secondary' ?>">Year</a>
        <a href="?period=all" class="btn <?= $period === 'all' ? 'btn-primary' : 'btn-secondary' ?>">All Time</a>
    </div>
</div>

<div class="row">
    <div class="col-md-3">
        <div class="stat-card">
            <div class="stat-icon blue"><i class="fas fa-users"></i></div>
            <div class="stat-value"><?= number_format($stats['total_users']) ?></div>
            <div class="stat-label">New Users</div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="stat-card">
            <div class="stat-icon green"><i class="fas fa-arrow-down"></i></div>
            <div class="stat-value"><?= CURRENCY_SYMBOL ?><?= number_format($stats['total_deposits']) ?></div>
            <div class="stat-label">Deposits</div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="stat-card">
            <div class="stat-icon red"><i class="fas fa-arrow-up"></i></div>
            <div class="stat-value"><?= CURRENCY_SYMBOL ?><?= number_format($stats['total_withdrawals']) ?></div>
            <div class="stat-label">Withdrawals</div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="stat-card">
            <div class="stat-icon <?= $stats['platform_profit'] >= 0 ? 'green' : 'red' ?>"><i class="fas fa-chart-pie"></i></div>
            <div class="stat-value"><?= CURRENCY_SYMBOL ?><?= number_format($stats['platform_profit']) ?></div>
            <div class="stat-label">Platform Profit</div>
        </div>
    </div>
</div>

<div class="row mt-4">
    <div class="col-md-6">
        <div class="card">
            <div class="card-header"><h4>Trading Summary</h4></div>
            <div class="card-body">
                <div class="d-flex justify-content-between mb-3">
                    <span>Total Trades</span>
                    <strong><?= number_format($stats['total_trades']) ?></strong>
                </div>
                <div class="d-flex justify-content-between mb-3">
                    <span>Trade Volume</span>
                    <strong><?= CURRENCY_SYMBOL ?><?= number_format($stats['trade_volume']) ?></strong>
                </div>
                <div class="d-flex justify-content-between mb-3">
                    <span class="text-green">Won Trades</span>
                    <strong class="text-green"><?= number_format($stats['won_trades']) ?></strong>
                </div>
                <div class="d-flex justify-content-between">
                    <span class="text-red">Lost Trades</span>
                    <strong class="text-red"><?= number_format($stats['lost_trades']) ?></strong>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-6">
        <div class="card">
            <div class="card-header"><h4>Financial Summary</h4></div>
            <div class="card-body">
                <div class="d-flex justify-content-between mb-3">
                    <span>Total Deposits</span>
                    <strong class="text-green"><?= CURRENCY_SYMBOL ?><?= number_format($stats['total_deposits']) ?></strong>
                </div>
                <div class="d-flex justify-content-between mb-3">
                    <span>Total Withdrawals</span>
                    <strong class="text-red"><?= CURRENCY_SYMBOL ?><?= number_format($stats['total_withdrawals']) ?></strong>
                </div>
                <div class="d-flex justify-content-between mb-3">
                    <span>Net Flow</span>
                    <strong><?= CURRENCY_SYMBOL ?><?= number_format($stats['total_deposits'] - $stats['total_withdrawals']) ?></strong>
                </div>
                <div class="d-flex justify-content-between">
                    <span>Platform Profit</span>
                    <strong class="<?= $stats['platform_profit'] >= 0 ? 'text-green' : 'text-red' ?>"><?= CURRENCY_SYMBOL ?><?= number_format($stats['platform_profit']) ?></strong>
                </div>
            </div>
        </div>
    </div>
</div>

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