<?php
require_once __DIR__ . '/../includes/bootstrap.php';
admin_require_login();

// Dashboard metrics
$totalReg      = db_row('SELECT COUNT(*) as n FROM registrations')['n'] ?? 0;
$activeTrials  = db_row("SELECT COUNT(*) as n FROM registrations WHERE status = 'trial' AND trial_expires_at > NOW()")['n'] ?? 0;
$expiredTrials = db_row("SELECT COUNT(*) as n FROM registrations WHERE status = 'expired'")['n'] ?? 0;
$converted     = db_row("SELECT COUNT(*) as n FROM registrations WHERE status = 'converted'")['n'] ?? 0;
$activeLic     = db_row("SELECT COUNT(*) as n FROM licenses WHERE status = 'active' AND expiry_at > NOW()")['n'] ?? 0;
$revokedLic    = db_row("SELECT COUNT(*) as n FROM licenses WHERE status = 'revoked'")['n'] ?? 0;
$todayChecks   = db_row("SELECT COUNT(*) as n FROM trial_checks WHERE checked_at > CURDATE()")['n'] ?? 0;

// Recent registrations
$recentRegs = db_query(
    'SELECT registration_id, business_name, email, status, registered_at, trial_expires_at
     FROM registrations ORDER BY registered_at DESC LIMIT 8'
);

// Recent activations
$recentActivations = db_query(
    'SELECT a.activated_at, a.result, a.ip_address, l.order_id, l.plan
     FROM activations a
     LEFT JOIN licenses l ON l.id = a.license_id
     ORDER BY a.activated_at DESC LIMIT 6'
);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BizNode Admin — Dashboard</title>
<link rel="stylesheet" href="/admin/assets/style.css">
</head>
<body>
<?php include __DIR__ . '/partials/nav.php'; ?>
<main class="main">
  <h1 class="page-title">Dashboard</h1>

  <!-- Metric cards -->
  <div class="metric-grid">
    <div class="metric-card">
      <div class="metric-value"><?= $totalReg ?></div>
      <div class="metric-label">Total Registrations</div>
    </div>
    <div class="metric-card metric-green">
      <div class="metric-value"><?= $activeTrials ?></div>
      <div class="metric-label">Active Trials</div>
    </div>
    <div class="metric-card metric-amber">
      <div class="metric-value"><?= $expiredTrials ?></div>
      <div class="metric-label">Expired Trials</div>
    </div>
    <div class="metric-card metric-blue">
      <div class="metric-value"><?= $converted ?></div>
      <div class="metric-label">Converted (Licensed)</div>
    </div>
    <div class="metric-card metric-green">
      <div class="metric-value"><?= $activeLic ?></div>
      <div class="metric-label">Active Licenses</div>
    </div>
    <div class="metric-card metric-red">
      <div class="metric-value"><?= $revokedLic ?></div>
      <div class="metric-label">Revoked Licenses</div>
    </div>
    <div class="metric-card">
      <div class="metric-value"><?= $todayChecks ?></div>
      <div class="metric-label">Trial Checks Today</div>
    </div>
  </div>

  <!-- Quick actions -->
  <div class="section-row">
    <a href="/admin/generate_license.php" class="btn btn-primary">+ Generate License</a>
    <a href="/admin/registrations.php"    class="btn btn-outline">View Registrations</a>
    <a href="/admin/licenses.php"         class="btn btn-outline">View Licenses</a>
    <a href="/admin/activations.php"      class="btn btn-outline">Activation Log</a>
  </div>

  <!-- Recent registrations -->
  <h2 class="section-title">Recent Registrations</h2>
  <table class="data-table">
    <thead><tr>
      <th>ID</th><th>Business</th><th>Email</th>
      <th>Status</th><th>Registered</th><th>Trial Expires</th>
    </tr></thead>
    <tbody>
    <?php foreach ($recentRegs as $r): ?>
      <tr>
        <td><code><?= h($r['registration_id']) ?></code></td>
        <td><?= h($r['business_name']) ?></td>
        <td><?= h($r['email']) ?></td>
        <td><span class="badge badge-<?= $r['status'] ?>"><?= h($r['status']) ?></span></td>
        <td><?= h(substr($r['registered_at'], 0, 16)) ?></td>
        <td><?= h(substr($r['trial_expires_at'], 0, 16)) ?></td>
      </tr>
    <?php endforeach; ?>
    <?php if (empty($recentRegs)): ?>
      <tr><td colspan="6" class="empty">No registrations yet.</td></tr>
    <?php endif; ?>
    </tbody>
  </table>

  <!-- Recent activations -->
  <h2 class="section-title">Recent Activations</h2>
  <table class="data-table">
    <thead><tr>
      <th>Order ID</th><th>Plan</th><th>Result</th><th>IP</th><th>At</th>
    </tr></thead>
    <tbody>
    <?php foreach ($recentActivations as $a): ?>
      <tr>
        <td><code><?= h($a['order_id'] ?? '—') ?></code></td>
        <td><?= h($a['plan'] ?? '—') ?></td>
        <td><span class="badge badge-<?= $a['result'] ?>"><?= h($a['result']) ?></span></td>
        <td><?= h($a['ip_address']) ?></td>
        <td><?= h(substr($a['activated_at'], 0, 16)) ?></td>
      </tr>
    <?php endforeach; ?>
    <?php if (empty($recentActivations)): ?>
      <tr><td colspan="5" class="empty">No activations yet.</td></tr>
    <?php endif; ?>
    </tbody>
  </table>
</main>
</body>
</html>
<?php function h(string $s): string { return htmlspecialchars($s, ENT_QUOTES, 'UTF-8'); } ?>
