<?php

declare(strict_types=1);

/**
 * Logs (SPEC §11.9): the events table with a level filter, and the call
 * log. Read-only, paginated, newest first.
 */

require __DIR__ . '/_lib.php';
admin_require();
$db = atdt_db();

const PAGE_SIZE = 50;

$tab = (string)($_GET['tab'] ?? 'events') === 'calls' ? 'calls' : 'events';
$level = (string)($_GET['level'] ?? '');
$page = max(0, (int)($_GET['p'] ?? 0));
$levels = ['info', 'admin', 'security', 'error'];

if ($tab === 'events') {
    $where = in_array($level, $levels, true) ? ' WHERE level = ?' : '';
    $params = $where === '' ? [] : [$level];
    $total = (int)$db->val("SELECT COUNT(*) FROM events$where", $params);
    $rows = $db->all(
        "SELECT * FROM events$where ORDER BY id DESC LIMIT " . PAGE_SIZE . ' OFFSET ' . $page * PAGE_SIZE,
        $params
    );
} else {
    $total = (int)$db->val('SELECT COUNT(*) FROM call_log');
    $rows = $db->all('SELECT * FROM call_log ORDER BY id DESC LIMIT ' . PAGE_SIZE . ' OFFSET ' . $page * PAGE_SIZE);
}
$pages = max(1, (int)ceil($total / PAGE_SIZE));

/** Pager link keeping the current filter. */
function pager_qs(int $p): string
{
    return h('logs.php?' . http_build_query(array_merge($_GET, ['p' => $p])));
}

admin_page_start('Logs', 'logs');
?>

<p>
  <a class="btn small<?= $tab === 'events' ? '' : ' secondary' ?>" href="logs.php?tab=events">Events</a>
  <a class="btn small<?= $tab === 'calls' ? '' : ' secondary' ?>" href="logs.php?tab=calls">Call log</a>
</p>

<?php if ($tab === 'events'): ?>
<form method="get" class="row card">
  <input type="hidden" name="tab" value="events">
  <select name="level">
    <option value="">every level</option>
    <?php foreach ($levels as $l): ?>
    <option value="<?= $l ?>"<?= $level === $l ? ' selected' : '' ?>><?= $l ?></option>
    <?php endforeach; ?>
  </select>
  <button class="btn small">Filter</button>
  <span class="hint"><?= $total ?> row<?= $total === 1 ? '' : 's' ?> · pruned after 90 days</span>
</form>

<div class="card">
  <table class="list">
    <tr><th class="nowrap">When</th><th>Level</th><th>Actor</th><th>Message</th></tr>
    <?php if ($rows === []): ?><tr><td colspan="4" class="hint">Nothing logged<?= $level !== '' ? ' at that level' : '' ?>.</td></tr><?php endif; ?>
    <?php foreach ($rows as $e): ?>
    <tr>
      <td class="nowrap hint"><?= h(admin_stamp((string)$e['at'], 'Y-m-d H:i:s')) ?></td>
      <td><span class="chip <?= h((string)$e['level']) ?>"><?= h((string)$e['level']) ?></span></td>
      <td><code><?= h((string)$e['actor']) ?></code></td>
      <td><?= h((string)$e['msg']) ?></td>
    </tr>
    <?php endforeach; ?>
  </table>
</div>

<?php else: ?>
<div class="card">
  <table class="list">
    <tr><th class="nowrap">On</th><th class="nowrap">Off</th><th>Handle</th><th>Node</th><th>Summary</th></tr>
    <?php if ($rows === []): ?><tr><td colspan="5" class="hint">No calls yet.</td></tr><?php endif; ?>
    <?php foreach ($rows as $c): ?>
    <tr>
      <td class="nowrap hint"><?= h(admin_stamp((string)$c['on_at'], 'Y-m-d H:i:s')) ?></td>
      <td class="nowrap hint"><?= h($c['off_at'] === null ? 'on now' : admin_stamp((string)$c['off_at'], 'H:i')) ?></td>
      <td><?= h((string)$c['handle']) ?></td>
      <td><?= (int)$c['node'] ?></td>
      <td><?= h((string)($c['summary'] ?? '')) ?></td>
    </tr>
    <?php endforeach; ?>
  </table>
</div>
<?php endif; ?>

<?php if ($pages > 1): ?>
<p>
  <?php if ($page > 0): ?><a class="btn small secondary" href="<?= pager_qs($page - 1) ?>">← Newer</a><?php endif; ?>
  <span class="hint">page <?= $page + 1 ?> / <?= $pages ?></span>
  <?php if ($page < $pages - 1): ?><a class="btn small secondary" href="<?= pager_qs($page + 1) ?>">Older →</a><?php endif; ?>
</p>
<?php endif; ?>

<?php admin_page_end(); ?>
