/**
 * @ai-summary Audit log feature routes
 * @ai-context Registers audit log API endpoints with admin authorization
 */

import { FastifyPluginAsync } from 'fastify';
import { AuditLogController } from './audit-log.controller';

interface AuditLogsQuery {
  search?: string;
  category?: string;
  severity?: string;
  startDate?: string;
  endDate?: string;
  limit?: string;
  offset?: string;
}

export const auditLogRoutes: FastifyPluginAsync = async (fastify) => {
  const controller = new AuditLogController();

  /**
   * GET /api/admin/audit-logs
   * Search audit logs with filters and pagination
   *
   * Query params:
   * - search: Text search on action field
   * - category: Filter by category (auth, vehicle, user, system, admin)
   * - severity: Filter by severity (info, warning, error)
   * - startDate: Filter by start date (ISO string)
   * - endDate: Filter by end date (ISO string)
   * - limit: Number of results (default 50, max 100)
   * - offset: Pagination offset
   */
  fastify.get<{ Querystring: AuditLogsQuery }>('/admin/audit-logs', {
    preHandler: [fastify.requireAdmin],
    handler: controller.getAuditLogs.bind(controller),
  });

  /**
   * GET /api/admin/audit-logs/export
   * Export filtered audit logs as CSV file
   *
   * Query params: same as /admin/audit-logs
   */
  fastify.get<{ Querystring: AuditLogsQuery }>('/admin/audit-logs/export', {
    preHandler: [fastify.requireAdmin],
    handler: controller.exportAuditLogs.bind(controller),
  });
};
