<?php
require_once '../config/database.php';
require_once '../includes/functions.php';
$pageTitle = 'Reports';
include 'header.php';

$conn = getConnection();
$totalUsers = $conn->query("SELECT COUNT(*) as c FROM users")->fetch_assoc()['c'];
$activeUsers = $conn->query("SELECT COUNT(*) as c FROM users WHERE status='active'")->fetch_assoc()['c'];
$totalEarnings = $conn->query("SELECT SUM(total_earned) as t FROM wallet_referral")->fetch_assoc()['t'] ?? 0;
$totalWithdrawals = $conn->query("SELECT SUM(net_amount) as t FROM withdrawals WHERE status='completed'")->fetch_assoc()['t'] ?? 0;
$pendingWithdrawals = $conn->query("SELECT COUNT(*) as c FROM withdrawals WHERE status='pending'")->fetch_assoc()['c'];
?>
<div class="container">
    <div class="row">
        <div class="col-md-3"><div class="card gradient-blue"><div class="wallet-label">TOTAL USERS</div><div class="wallet-amount"><?php echo $totalUsers; ?></div></div></div>
        <div class="col-md-3"><div class="card gradient-green"><div class="wallet-label">ACTIVE USERS</div><div class="wallet-amount"><?php echo $activeUsers; ?></div></div></div>
        <div class="col-md-3"><div class="card gradient-orange"><div class="wallet-label">TOTAL EARNINGS</div><div class="wallet-amount">Ksh <?php echo number_format($totalEarnings, 2); ?></div></div></div>
        <div class="col-md-3"><div class="card gradient-purple"><div class="wallet-label">WITHDRAWALS</div><div class="wallet-amount">Ksh <?php echo number_format($totalWithdrawals, 2); ?></div></div></div>
    </div>
    <div class="card">
        <h3>📊 Platform Statistics</h3>
        <table class="table">
            <tr><td><strong>Total Users:</strong></td><td><?php echo $totalUsers; ?></td></tr>
            <tr><td><strong>Active Users:</strong></td><td><?php echo $activeUsers; ?></td></tr>
            <tr><td><strong>Inactive Users:</strong></td><td><?php echo $totalUsers - $activeUsers; ?></td></tr>
            <tr><td><strong>Pending Withdrawals:</strong></td><td><?php echo $pendingWithdrawals; ?></td></tr>
            <tr><td><strong>Total Earned by Users:</strong></td><td>Ksh <?php echo number_format($totalEarnings, 2); ?></td></tr>
            <tr><td><strong>Total Withdrawn:</strong></td><td>Ksh <?php echo number_format($totalWithdrawals, 2); ?></td></tr>
            <tr><td><strong>Platform Balance:</strong></td><td>Ksh <?php echo number_format($totalEarnings - $totalWithdrawals, 2); ?></td></tr>
        </table>
    </div>
</div>
<?php include 'footer.php'; ?>
