import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { getStudent } from "@/lib/studentAuth";

export async function DELETE(
  req: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  const student = await getStudent();
  if (!student) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });

  const studentId = student.id as number;
  const { id } = await params;
  const vacationId = Number(id);

  const vacation = await prisma.student_vacations.findUnique({ where: { id: vacationId } });

  if (!vacation || vacation.student_id !== studentId) {
    return NextResponse.json({ error: "Vacation request not found" }, { status: 404 });
  }
  if (vacation.status !== "pending") {
    return NextResponse.json({ error: "Only pending requests can be cancelled" }, { status: 400 });
  }

  await prisma.student_vacations.delete({ where: { id: vacationId } });

  return NextResponse.json({ data: { id: vacationId } });
}