<?php
require_once __DIR__ . "/../includes/admin_auth.php"; // Must be first
require_once '../includes/db.php';

// ==========================
// DASHBOARD STATS
// ==========================

$totalUsers = 0;
$totalBalance = 0;
$totalTransfers = 0;
$pendingTransfers = 0;

$res = $conn->query("SELECT COUNT(*) AS total FROM users");
if ($res) $totalUsers = $res->fetch_assoc()['total'];

$res = $conn->query("SELECT COUNT(*) AS total FROM transfers");
if ($res) $totalTransfers = $res->fetch_assoc()['total'];

$res = $conn->query("SELECT COUNT(*) AS total FROM transfers WHERE status='pending'");
if ($res) $pendingTransfers = $res->fetch_assoc()['total'];

$res = $conn->query("SELECT SUM(balance) AS total FROM users");
if ($res) $totalBalance = $res->fetch_assoc()['total'] ?? 0;

// ==========================
// PAGE OUTPUT BUFFER
// ==========================

ob_start();
?>

<div class="main-content">

    <h2 class="page-title">Admin Dashboard</h2>

    <!-- STATS -->
    <div class="dashboard-grid">

        <div class="card">
            <h3>Total Users</h3>
            <p><?= $totalUsers ?></p>
        </div>

        <div class="card">
            <h3>Total Balance</h3>
            <p>$ <?= number_format($totalBalance, 2) ?></p>
        </div>

        <div class="card">
            <h3>Total Transfers</h3>
            <p><?= $totalTransfers ?></p>
        </div>

        <div class="card">
            <h3>Pending Transfers</h3>
            <p><?= $pendingTransfers ?></p>
        </div>

    </div>

    <!-- QUICK ACTIONS -->
    <div class="panel">

        <h3>Quick Actions</h3>

        <div class="actions">

            <a href="create_user.php" class="btn">Create User</a>
            <a href="manage_users.php" class="btn">Manage Users</a>
            <a href="transactions.php" class="btn">View Transactions</a>
            <a href="assign_token_steps.php" class="btn">Token Settings</a>
            <a href="currencies.php" class="btn">Currencies</a>
            <a href="system_settings.php" class="btn">System Settings</a>

        </div>

    </div>

</div>

<?php
$content = ob_get_clean();

// ==========================
// LOAD MASTER LAYOUT
// ==========================

$page_title = "Admin Dashboard";
include '../components/admin_layout.php';
?>