const express = require('express');
const router = express.Router();
const { getDb } = require('../db');

router.get('/', (req, res) => {
  const db = getDb();
  const page = parseInt(req.query.page) || 1;
  const limit = 50;
  const offset = (page - 1) * limit;
  const total = db.prepare('SELECT COUNT(*) as cnt FROM audit_log').get().cnt;
  const entries = db.prepare('SELECT * FROM audit_log ORDER BY created_at DESC LIMIT ? OFFSET ?').all(limit, offset);
  res.render('audit-list', { entries, page, total, pages: Math.ceil(total / limit) });
});

module.exports = router;
