import { NextRequest, NextResponse } from "next/server";
import prisma from "@/app/prisma";
import { z } from "zod/v4";

const schema = z.object({
    siteId: z.number(),
});

export async function DELETE(request: NextRequest) {
    const searchParams = request.nextUrl.searchParams;
    const siteId = schema.parse({ siteId: searchParams.get("siteId") });

    try {
        const site = await prisma.site.delete({
            where: { id: siteId.siteId },
        });

        return NextResponse.json(site);
    } catch (error: any) {
        if(error instanceof z.ZodError) {
            return NextResponse.json({ error: error.issues[0].message }, { status: 400 });
        }
        return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
    }
}