<?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';
require_admin($conn);

/* LOAD AUDIT LOGS */
$rows = [];
$res = $conn->query("
  SELECT a.*, u.full_name
  FROM audit_logs a
  LEFT JOIN users u ON u.id = a.actor_id
  ORDER BY a.id DESC
  LIMIT 300
");
while ($r = $res->fetch_assoc()) {
  $rows[] = $r;
}

$title = 'Audit Logs — Astrabank';
include __DIR__ . '/../../views/partials/head.php';
include __DIR__ . '/../../views/partials/admin_sidebar.php';
?>

<div class="container py-4">
  <div class="d-flex align-items-center justify-content-between mb-3">
    <h3 class="fw-bold m-0">Audit Logs</h3>
    <a class="btn btn-sm btn-outline-secondary" href="dashboard.php">Back</a>
  </div>

  <div class="card ab-card p-3">
    <div class="table-responsive">
      <table class="table align-middle">
        <thead>
          <tr>
            <th>Time</th>
            <th>Actor</th>
            <th>Action</th>
            <th>IP</th>
            <th>Meta</th>
          </tr>
        </thead>
        <tbody>
          <?php foreach ($rows as $r): ?>
            <tr>
              <td><?= e($r['created_at']) ?></td>
              <td><?= e($r['full_name'] ?? ('#' . $r['actor_id'])) ?></td>
              <td>
                <span class="badge text-bg-dark">
                  <?= e($r['action']) ?>
                </span>
              </td>
              <td><?= e($r['ip'] ?? '-') ?></td>
              <td>
                <code style="font-size:12px;white-space:pre-wrap">
                  <?= e($r['meta'] ?? '') ?>
                </code>
              </td>
            </tr>
          <?php endforeach; ?>

          <?php if (!$rows): ?>
            <tr>
              <td colspan="5" class="text-center text-muted py-4">
                No audit records found.
              </td>
            </tr>
          <?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>
</div>

<?php include __DIR__ . '/../../views/partials/foot.php'; ?>
