import { NextRequest, NextResponse } from 'next/server';
import { requirePermission } from '@/shared/admin/guards';
import { PERMISSIONS } from '@/shared/admin/permissions';
import { queryAuditLog } from '@/shared/admin/audit';

// --- ASA GENERATED START ---
// Route handler for admin/audit-log slice.
// Filterable, paginated audit log viewer.
// Security: requires admin.audit.view permission (server-side).
// --- ASA GENERATED END ---

// --- USER CODE START ---

export async function GET(request: NextRequest): Promise<NextResponse> {
  const { response } = await requirePermission(request, PERMISSIONS.AUDIT_VIEW);
  if (response) return response;

  const url = new URL(request.url);

  const result = await queryAuditLog({
    actor_id: url.searchParams.get('actor_id') ?? undefined,
    subject_id: url.searchParams.get('subject_id') ?? undefined,
    action: url.searchParams.get('action') ?? undefined,
    resource_type: url.searchParams.get('resource_type') ?? undefined,
    from_date: url.searchParams.get('from') ?? undefined,
    to_date: url.searchParams.get('to') ?? undefined,
    limit: parseInt(url.searchParams.get('limit') ?? '50'),
    offset: parseInt(url.searchParams.get('offset') ?? '0'),
  });

  return NextResponse.json(result);
}

// --- USER CODE END ---
