import { prisma } from '@/lib/db'
import { apiRoute, paginated, pagination } from '@/lib/api/handler'

export const dynamic = 'force-dynamic'

export const GET = apiRoute({ permission: 'audit.read' }, async ({ searchParams }) => {
  const { page, pageSize, skip, take } = pagination(searchParams, 50)
  const action = searchParams.get('action')
  const where = action ? { action } : {}

  const [items, total] = await Promise.all([
    prisma.auditLog.findMany({ where, skip, take, orderBy: { ts: 'desc' } }),
    prisma.auditLog.count({ where }),
  ])

  return paginated(items, total, page, pageSize)
})
