import type { FastifyInstance } from 'fastify'
import { db } from '../db/index.js'

export async function eventRoutes(app: FastifyInstance) {
  app.addHook('onRequest', app.authenticate)

  app.get('/api/events', async (req) => {
    const query = req.query as { limit?: string }
    const limit = Math.min(Number(query.limit) || 20, 100)
    const events = db.prepare('SELECT * FROM events ORDER BY created_at DESC, id DESC LIMIT ?').all(limit)
    return { events }
  })
}
