<?php
// admin/audit.php — admin action audit log (super admins only)
require_once __DIR__ . '/../config.php';
superAdminGuard();
require_once __DIR__ . '/_layout.php';

$db = db();

// Filters
$fAction = trim($_GET['action_filter'] ?? '');
$where   = '';
$params  = [];
if ($fAction) { $where = 'WHERE action = ?'; $params[] = $fAction; }

// Pagination
$countStmt = $db->prepare("SELECT COUNT(*) FROM admin_log {$where}");
$countStmt->execute($params);
$total   = (int)$countStmt->fetchColumn();
$perPage = 50;
$pageCount = max(1, (int)ceil($total / $perPage));
$page    = max(1, min($pageCount, (int)($_GET['page'] ?? 1)));
$offset  = ($page - 1) * $perPage;

$stmt = $db->prepare("SELECT * FROM admin_log {$where} ORDER BY logged_at DESC LIMIT {$perPage} OFFSET {$offset}");
$stmt->execute($params);
$rows = $stmt->fetchAll();

// Distinct actions for the filter dropdown
$actions = $db->query('SELECT DISTINCT action FROM admin_log ORDER BY action')->fetchAll(PDO::FETCH_COLUMN);

layout_start('Audit Log', 'audit');
?>
<div class="page-header">
  <div><h1>Audit Log</h1>
  <div class="page-header-sub text-muted">Every admin action, most recent first</div></div>
  <form method="GET" style="display:flex;gap:8px">
    <select name="action_filter" class="form-control" style="width:auto;padding:7px 12px" onchange="this.form.submit()">
      <option value="">All actions</option>
      <?php foreach ($actions as $a): ?>
        <option value="<?= htmlspecialchars($a) ?>" <?= $fAction === $a ? 'selected' : '' ?>><?= htmlspecialchars($a) ?></option>
      <?php endforeach; ?>
    </select>
  </form>
</div>

<div class="card">
  <div class="table-wrap">
    <table>
      <thead>
        <tr><th>When</th><th>Admin</th><th>Action</th><th>Target</th><th>Detail</th><th>IP</th></tr>
      </thead>
      <tbody>
        <?php if (empty($rows)): ?>
          <tr><td colspan="6" class="text-muted" style="padding:32px;text-align:center">No log entries yet.</td></tr>
        <?php endif; ?>
        <?php foreach ($rows as $r): ?>
        <tr>
          <td class="text-muted text-sm" style="white-space:nowrap"><?= htmlspecialchars(date('M j, Y H:i', strtotime($r['logged_at']))) ?></td>
          <td><?= htmlspecialchars($r['admin_name'] ?? ('#' . $r['admin_id'])) ?></td>
          <td><span class="badge badge-gold"><?= htmlspecialchars($r['action']) ?></span></td>
          <td class="text-muted text-sm"><?= $r['target_type'] ? htmlspecialchars($r['target_type'] . ' #' . $r['target_id']) : '—' ?></td>
          <td class="text-muted text-sm"><?= htmlspecialchars($r['detail'] ?? '') ?></td>
          <td class="text-muted text-sm"><?= htmlspecialchars($r['ip'] ?? '') ?></td>
        </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
  <?php if ($pageCount > 1): ?>
  <?php $u = fn(int $p) => '?' . ($fAction ? 'action_filter=' . urlencode($fAction) . '&' : '') . 'page=' . $p; ?>
  <div class="card-footer" style="display:flex;justify-content:space-between;align-items:center;padding-top:14px">
    <span class="text-muted text-sm">Showing <?= $offset + 1 ?>–<?= min($offset + $perPage, $total) ?> of <?= $total ?></span>
    <div style="display:flex;gap:6px;align-items:center">
      <?php if ($page > 1): ?><a class="btn btn-outline btn-sm" href="<?= $u($page - 1) ?>">← Prev</a><?php endif; ?>
      <span class="text-muted text-sm">Page <?= $page ?> / <?= $pageCount ?></span>
      <?php if ($page < $pageCount): ?><a class="btn btn-outline btn-sm" href="<?= $u($page + 1) ?>">Next →</a><?php endif; ?>
    </div>
  </div>
  <?php endif; ?>
</div>
<?php layout_end(); ?>
