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().min(2),
    ipv4Subnet: z.string().optional(),
    ipv6Subnet: z.string().optional(),
    gateway: z.string().optional(),
});

export async function POST(request: NextRequest) {
    const body = schema.parse(await request.json());

    try {
        const network = await prisma.network.update({
            where: {
                id: body.id,
            },
            data: {
                name: body.name,
                ipv4Subnet: body.ipv4Subnet,
                ipv6Subnet: body.ipv6Subnet,
                gateway: body.gateway,
            },
        });

        return NextResponse.json({ network }, { status: 201 });
    } 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 });
    }
}