import type { Request, Response } from "express";
import { prisma } from "../../config/prisma.js";
import { asyncHandler } from "../../utils/asyncHandler.js";

export const listAuditLogs = asyncHandler(async (req: Request, res: Response) => {
  const { entityType, limit, userId } = req.query;
  const where: any = {};
  if (entityType) where.entityType = String(entityType);
  if (userId) where.userId = Number(userId);

  const logs = await prisma.auditLog.findMany({
    where,
    orderBy: { createdAt: "desc" },
    take: Number(limit || 200),
  });
  res.json({ success: true, data: logs });
});
