import { NextResponse } from "next/server";
import { getServerSession } from "next-auth";
import { authOptions } from "@/lib/auth";
import prisma from "@/lib/prisma";

export async function PUT(req: Request, { params }: { params: Promise<{ id: string }> }) {
  try {
    const session = await getServerSession(authOptions);
    if (!session || session.user.role !== "ADMIN") {
      return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
    }

    const body = await req.json();
    const { id } = await params;
    const balance = await prisma.leaveBalance.update({
      where: { id },
      data: {
        allocated: body.allocated,
        used: body.used,
        pending: body.pending,
        carriedOver: body.carriedOver,
      },
    });
    return NextResponse.json(balance);
  } catch (error) {
    return NextResponse.json({ error: "Failed to update balance" }, { status: 500 });
  }
}
