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

function h(string $s): string { return htmlspecialchars($s, ENT_QUOTES, 'UTF-8'); }

$rows = db_query(
    'SELECT a.id, a.activated_at, a.result, a.message, a.ip_address, a.machine_fingerprint,
            l.order_id, l.plan, l.email,
            r.registration_id, r.business_name
     FROM activations a
     LEFT JOIN licenses l ON l.id = a.license_id
     LEFT JOIN registrations r ON r.id = a.registration_id
     ORDER BY a.activated_at DESC LIMIT 300'
);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BizNode Admin — Activation Log</title>
<link rel="stylesheet" href="/admin/assets/style.css">
</head>
<body>
<?php include __DIR__ . '/partials/nav.php'; ?>
<main class="main">
  <h1 class="page-title">Activation Log</h1>
  <p class="result-count"><?= count($rows) ?> entries (latest 300)</p>
  <table class="data-table">
    <thead><tr>
      <th>Date / Time</th><th>Order ID</th><th>Business</th>
      <th>Email</th><th>Plan</th><th>Result</th><th>IP</th><th>Message</th>
    </tr></thead>
    <tbody>
    <?php foreach ($rows as $r): ?>
      <tr>
        <td><?= h(substr($r['activated_at'], 0, 16)) ?></td>
        <td><code><?= h($r['order_id'] ?? '—') ?></code></td>
        <td><?= h($r['business_name'] ?? '—') ?></td>
        <td><?= h($r['email'] ?? '—') ?></td>
        <td><?= h($r['plan'] ?? '—') ?></td>
        <td><span class="badge badge-<?= $r['result'] ?>"><?= h($r['result']) ?></span></td>
        <td><small><?= h($r['ip_address']) ?></small></td>
        <td><small><?= h($r['message']) ?></small></td>
      </tr>
    <?php endforeach; ?>
    <?php if (empty($rows)): ?><tr><td colspan="8" class="empty">No activations yet.</td></tr><?php endif; ?>
    </tbody>
  </table>
</main>
</body>
</html>
