import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { requireRole } from "@/lib/auth";

type Params = { params: Promise<{ id: string }> };

export async function DELETE(_req: Request, { params }: Params) {
  const session = await requireRole("INSTITUTION_ADMIN");
  if (!session || !session.institutionId) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }

  const { id } = await params;
  const entry = await prisma.accountEntry.findFirst({
    where: { id: Number(id), institutionId: session.institutionId },
  });
  if (!entry) return NextResponse.json({ error: "Not found" }, { status: 404 });

  try {
    await prisma.accountEntry.delete({ where: { id: entry.id } });
    return NextResponse.json({ ok: true });
  } catch {
    return NextResponse.json({ error: "Could not delete voucher" }, { status: 500 });
  }
}
