import { NextResponse } from "next/server";
import { prisma } from "@/lib/db";
import { requireAuth } from "@/lib/auth/middleware";

export async function DELETE(req: Request, ctx: { params: Promise<{ id: string }> }) {
  const auth = await requireAuth(req);
  if (!auth.ok) return NextResponse.json({ error: "unauthorized" }, { status: auth.status });
  const { id } = await ctx.params;
  // Detach all projects from this target first (set to null)
  await prisma.project.updateMany({ where: { deployTargetId: id }, data: { deployTargetId: null } });
  await prisma.deployTarget.delete({ where: { id } });
  return NextResponse.json({ ok: true });
}
