<?php
/**
 * admin/index.php
 * -----------------------------------------------------------------
 * Manual activation panel. Lists every email the dashboard has seen
 * (anyone who hit the free-trial wall and entered an email), with
 * their status. Activate someone here after you've confirmed their
 * payment via WhatsApp/email — the main dashboard will pick up the
 * change automatically next time it checks.
 * -----------------------------------------------------------------
 */
session_start();
require_once __DIR__ . '/../access-lib.php';

if (empty($_SESSION['clyde_admin'])) {
    header('Location: login.php');
    exit;
}

$flash = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email  = (string)($_POST['email'] ?? '');
    $action = (string)($_POST['action'] ?? '');
    $notes  = (string)($_POST['notes'] ?? '');

    if ($action === 'activate') {
        access_set_status($email, 'active', $notes);
        $flash = 'Activated ' . htmlspecialchars($email) . '.';
    } elseif ($action === 'deactivate') {
        access_set_status($email, 'trial', $notes);
        $flash = 'Set ' . htmlspecialchars($email) . ' back to trial.';
    } elseif ($action === 'update_notes') {
        $all = access_load_all();
        $key = access_normalize_email($email);
        if (isset($all[$key])) {
            $all[$key]['notes'] = $notes;
            access_save_all($all);
            $flash = 'Notes saved for ' . htmlspecialchars($email) . '.';
        }
    }
    // PRG pattern to avoid resubmission on refresh.
    header('Location: index.php?flash=' . urlencode($flash));
    exit;
}

$flash = isset($_GET['flash']) ? $_GET['flash'] : '';
$all = access_load_all();
// Newest first.
uasort($all, function($a, $b) {
    return strcmp($b['first_seen'] ?? '', $a['first_seen'] ?? '');
});
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin — Manual Access Control</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Oswald:wght@500;600;700&family=Inter:wght@400;500;600&family=JetBrains+Mono:wght@400;500;700&display=swap');
*{box-sizing:border-box;}
body{
  margin:0;padding:30px 20px 60px 20px;
  background:radial-gradient(ellipse at top,#1a1e22 0%,#15181b 55%);
  font-family:'Inter',sans-serif;color:#ece9e2;
}
.wrap{max-width:1000px;margin:0 auto;}
.topbar{display:flex;justify-content:space-between;align-items:flex-end;margin-bottom:20px;flex-wrap:wrap;gap:10px;}
.eyebrow{font-family:'JetBrains Mono',monospace;font-size:11px;letter-spacing:0.14em;color:#ffc52f;text-transform:uppercase;}
h1{font-family:'Oswald',sans-serif;text-transform:uppercase;font-size:26px;margin:4px 0 0 0;}
.logout{font-family:'JetBrains Mono',monospace;font-size:12px;color:#9aa1a8;text-decoration:none;border:1px solid #343a40;padding:7px 12px;border-radius:5px;}
.logout:hover{color:#ece9e2;border-color:#5c636a;}
.flash{
  background:rgba(82,209,124,0.1);border:1px solid rgba(82,209,124,0.4);color:#52d17c;
  padding:10px 14px;border-radius:6px;font-size:13px;margin-bottom:16px;
}
.summary{
  display:flex;gap:10px;margin-bottom:20px;flex-wrap:wrap;
}
.sumcard{
  background:#1c2126;border:1px solid #343a40;border-radius:6px;padding:12px 18px;min-width:120px;
}
.sumcard .n{font-family:'JetBrains Mono',monospace;font-size:24px;font-weight:700;}
.sumcard .l{font-family:'JetBrains Mono',monospace;font-size:10.5px;color:#9aa1a8;text-transform:uppercase;letter-spacing:0.06em;}
table{width:100%;border-collapse:collapse;background:#1c2126;border:1px solid #343a40;border-radius:6px;overflow:hidden;}
th{
  text-align:left;font-family:'JetBrains Mono',monospace;font-size:10.5px;letter-spacing:0.06em;
  text-transform:uppercase;color:#a8841f;padding:10px 14px;border-bottom:1px solid #343a40;background:#0e1113;
}
td{padding:11px 14px;border-bottom:1px solid #2a3036;font-size:13.5px;vertical-align:top;}
tr:last-child td{border-bottom:none;}
.email{font-family:'JetBrains Mono',monospace;font-size:12.5px;}
.badge{font-family:'JetBrains Mono',monospace;font-size:10px;padding:3px 8px;border-radius:3px;white-space:nowrap;}
.badge.active{background:rgba(82,209,124,0.15);color:#52d17c;border:1px solid rgba(82,209,124,0.4);}
.badge.trial{background:rgba(255,154,31,0.15);color:#ff9a1f;border:1px solid rgba(255,154,31,0.4);}
.notesform textarea{
  width:100%;min-width:160px;font-family:'Inter',sans-serif;font-size:12.5px;
  background:#0e1113;border:1px solid #343a40;color:#ece9e2;border-radius:5px;padding:7px 9px;resize:vertical;
}
.notesform button, .actionform button{
  font-family:'JetBrains Mono',monospace;font-size:11px;
  border:none;border-radius:5px;padding:7px 11px;cursor:pointer;margin-top:6px;margin-right:4px;
}
.btn-save{background:#343a40;color:#ece9e2;}
.btn-activate{background:#52d17c;color:#0e1113;font-weight:700;}
.btn-deactivate{background:rgba(255,82,82,0.15);color:#ff5252;border:1px solid rgba(255,82,82,0.4) !important;}
.muted{color:#5c636a;font-size:11.5px;font-family:'JetBrains Mono',monospace;}
.empty{padding:30px;text-align:center;color:#5c636a;font-family:'JetBrains Mono',monospace;font-size:13px;}
</style>
</head>
<body>
<div class="wrap">
  <div class="topbar">
    <div>
      <div class="eyebrow">Clyde 37-DE / RCI-1550 Dashboard</div>
      <h1>Manual Access Control</h1>
    </div>
    <a class="logout" href="logout.php">Log out</a>
  </div>

  <?php if ($flash): ?><div class="flash"><?php echo htmlspecialchars($flash); ?></div><?php endif; ?>

  <?php
    $total = count($all);
    $activeCount = count(array_filter($all, fn($r) => $r['status'] === 'active'));
    $trialCount = $total - $activeCount;
  ?>
  <div class="summary">
    <div class="sumcard"><div class="n"><?php echo $total; ?></div><div class="l">Total Emails</div></div>
    <div class="sumcard"><div class="n" style="color:#52d17c;"><?php echo $activeCount; ?></div><div class="l">Active</div></div>
    <div class="sumcard"><div class="n" style="color:#ff9a1f;"><?php echo $trialCount; ?></div><div class="l">Trial / Pending</div></div>
  </div>

  <?php if ($total === 0): ?>
    <div class="empty">No visitors have hit the trial wall yet. Once someone does, their email will show up here.</div>
  <?php else: ?>
  <table>
    <tr>
      <th>Email</th>
      <th>Status</th>
      <th>First Seen</th>
      <th>Activated At</th>
      <th>Notes</th>
      <th>Action</th>
    </tr>
    <?php foreach ($all as $email => $r): ?>
    <tr>
      <td class="email"><?php echo htmlspecialchars($r['email']); ?></td>
      <td><span class="badge <?php echo $r['status']; ?>"><?php echo strtoupper($r['status']); ?></span></td>
      <td class="muted"><?php echo htmlspecialchars($r['first_seen'] ?? ''); ?></td>
      <td class="muted"><?php echo htmlspecialchars($r['activated_at'] ?? '—'); ?></td>
      <td>
        <form class="notesform" method="POST">
          <input type="hidden" name="email" value="<?php echo htmlspecialchars($r['email']); ?>">
          <input type="hidden" name="action" value="update_notes">
          <textarea name="notes" rows="2" placeholder="e.g. WA number, payment ref"><?php echo htmlspecialchars($r['notes'] ?? ''); ?></textarea>
          <button type="submit" class="btn-save">Save Notes</button>
        </form>
      </td>
      <td>
        <?php if ($r['status'] === 'active'): ?>
          <form class="actionform" method="POST">
            <input type="hidden" name="email" value="<?php echo htmlspecialchars($r['email']); ?>">
            <input type="hidden" name="action" value="deactivate">
            <button type="submit" class="btn-deactivate">Deactivate</button>
          </form>
        <?php else: ?>
          <form class="actionform" method="POST">
            <input type="hidden" name="email" value="<?php echo htmlspecialchars($r['email']); ?>">
            <input type="hidden" name="action" value="activate">
            <button type="submit" class="btn-activate">Activate</button>
          </form>
        <?php endif; ?>
      </td>
    </tr>
    <?php endforeach; ?>
  </table>
  <?php endif; ?>
</div>
</body>
</html>