import { NextRequest, NextResponse } from "next/server";
import { requireAdmin } from "@/lib/api-auth";
import { db, invites, auditLogs, eq } from "@epicure/db";
import { randomUUID } from "crypto";

interface RouteContext {
  params: Promise<{ id: string }>;
}

export async function DELETE(_req: NextRequest, { params }: RouteContext) {
  const { session, response } = await requireAdmin();
  if (response) return response;

  const { id } = await params;
  const [deleted] = await db.delete(invites).where(eq(invites.id, id)).returning();
  if (!deleted) {
    return NextResponse.json({ error: "Invite not found" }, { status: 404 });
  }

  await db.insert(auditLogs).values({
    id: randomUUID(),
    userId: session!.user.id,
    action: "admin.invite.revoke",
    targetType: "invite",
    targetId: id,
    createdAt: new Date(),
  });

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