<?php
require_once __DIR__ . '/../../app/helpers/functions.php';
require_once __DIR__ . '/../../app/middleware/auth.php';




session_boot();
require_login();

/** MySQLi connection */
$conn = require __DIR__ . '/../../app/config/database.php';

/** Admin guard */
require_admin($conn);

/** ---- helpers (safe scalar) ---- */
function scalar(mysqli $conn, string $sql, $default = 0) {
  try {
    $res = $conn->query($sql);
    if (!$res) return $default;
    $row = $res->fetch_row();
    return $row && isset($row[0]) ? $row[0] : $default;
  } catch (Throwable $e) {
    return $default;
  }
}

/** ---- KPIs ---- */
$totalUsers    = (int) scalar($conn, "SELECT COUNT(*) FROM users WHERE id<>1", 0);
$pendingWires  = (int) scalar($conn, "SELECT COUNT(*) FROM wire_transfers WHERE status='pending_review'", 0);
$pendingCrypto = (int) scalar($conn, "SELECT COUNT(*) FROM crypto_txn WHERE status='pending'", 0);
$pendingCards  = (int) scalar($conn, "SELECT COUNT(*) FROM card_requests WHERE status='pending'", 0);
$todayVol      = (float) scalar($conn, "SELECT COALESCE(SUM(amount),0) FROM ledger_entries WHERE created_at >= UTC_DATE() AND direction='debit'", 0);

/** ---- Chart data ---- */
$labels = [];
$volumes = [];
$res = $conn->query("
  SELECT DATE(created_at) d, COALESCE(SUM(amount),0) amt
  FROM ledger_entries
  WHERE created_at >= (UTC_DATE() - INTERVAL 6 DAY)
    AND direction='debit'
  GROUP BY DATE(created_at)
  ORDER BY d ASC
");
$map = [];
while ($res && ($r = $res->fetch_assoc())) {
  $map[$r['d']] = (float)$r['amt'];
}
for ($i = 6; $i >= 0; $i--) {
  $d = gmdate('Y-m-d', time() - ($i * 86400));
  $labels[] = $d;
  $volumes[] = $map[$d] ?? 0;
}

include __DIR__ . '/../../views/partials/admin_sidebar.php';
?>


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Admin Dashboard — Astrabank</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>

<body class="bg-light">

<div class="d-flex">


  <!-- ===== MAIN CONTENT ===== -->
  <main class="flex-fill p-4">

    <div class="mb-3">
      <h3 class="fw-bold mb-0">Admin Dashboard</h3>
      <div class="text-muted">Monitor approvals, users, and activity.</div>
    </div>

    <!-- KPI CARDS -->
    <div class="row g-3">
      <div class="col-md-3">
        <div class="card shadow-sm">
          <div class="card-body">
            <div class="text-muted small">Customers</div>
            <div class="fs-3 fw-bold"><?= number_format($totalUsers) ?></div>
          </div>
        </div>
      </div>

      <div class="col-md-3">
        <div class="card shadow-sm">
          <div class="card-body">
            <div class="text-muted small">Pending Wires</div>
            <div class="fs-3 fw-bold"><?= $pendingWires ?></div>
          </div>
        </div>
      </div>

      <div class="col-md-3">
        <div class="card shadow-sm">
          <div class="card-body">
            <div class="text-muted small">Pending Crypto</div>
            <div class="fs-3 fw-bold"><?= $pendingCrypto ?></div>
          </div>
        </div>
      </div>

      <div class="col-md-3">
        <div class="card shadow-sm">
          <div class="card-body">
            <div class="text-muted small">Pending Cards</div>
            <div class="fs-3 fw-bold"><?= $pendingCards ?></div>
          </div>
        </div>
      </div>
    </div>

    <!-- CHART -->
    <div class="card shadow-sm mt-4">
      <div class="card-body">
        <div class="fw-bold mb-2">7-Day Debit Volume</div>
        <canvas id="volChart" height="90"></canvas>
      </div>
    </div>

  </main>
</div>

<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.1/dist/chart.umd.min.js"></script>
<script>
new Chart(document.getElementById('volChart'), {
  type: 'line',
  data: {
    labels: <?= json_encode($labels) ?>,
    datasets: [{
      data: <?= json_encode($volumes) ?>,
      tension: 0.35
    }]
  },
  options: {
    plugins: { legend: { display:false } },
    scales: { y: { beginAtZero:true } }
  }
});
</script>

</body>
</html>
