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

const schema = z.object({
    id: z.number(),
    name: z.string(),
    description: z.string(),
    networks: z.array(z.object({
        id: z.number().optional(),
        name: z.string().optional(),
        ipv4Subnet: z.string().optional(),
        ipv6Subnet: z.string().optional(),
        gateway: z.string().optional(),
    })),
});

export async function POST(request: NextRequest) {
    try {
        const body = schema.parse(await request.json());
        const { id, name, description, networks } = body;

        const site = await prisma.site.update({
            where: { id: Number(id) },
            data: { name, description },
        });

        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 });
    }
}
