import { type NextRequest, NextResponse } from 'next/server';
import { requireGuildAccess, toApiErrorResponse } from '@/lib/auth';
import { writeDashboardAudit } from '@/lib/audit';
import { deleteGiveawayDashboard } from '@/lib/module-configs/giveaways';
import { prisma } from '@/lib/prisma';

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

export async function DELETE(_request: NextRequest, { params }: RouteParams) {
  const { guildId, giveawayId } = await params;
  try {
    const session = await requireGuildAccess(guildId);
    const deleted = await deleteGiveawayDashboard(guildId, giveawayId);
    if (!deleted) {
      return NextResponse.json({ error: 'Giveaway not found' }, { status: 404 });
    }

    await writeDashboardAudit(prisma, {
      guildId,
      actorUserId: session.user.id,
      action: 'giveaways.delete',
      path: `/dashboard/${guildId}/giveaways`,
      before: { giveawayId }
    });

    return NextResponse.json({ ok: true });
  } catch (error) {
    return toApiErrorResponse(error);
  }
}
