import { WarningListQuerySchema } from '@nexumi/shared';
import { type NextRequest, NextResponse } from 'next/server';
import { requireGuildAccess, toApiErrorResponse } from '@/lib/auth';
import { listWarnings } from '@/lib/module-configs/warnings';

interface RouteParams {
  params: Promise<{ guildId: string }>;
}

export async function GET(request: NextRequest, { params }: RouteParams) {
  const { guildId } = await params;
  try {
    await requireGuildAccess(guildId);
    const searchParams = request.nextUrl.searchParams;
    const query = WarningListQuerySchema.parse({
      userId: searchParams.get('userId') ?? undefined
    });
    const warnings = await listWarnings(guildId, query);
    return NextResponse.json({ warnings });
  } catch (error) {
    return toApiErrorResponse(error);
  }
}
