import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { parseProject } from "@/lib/dto";
import { guard, run, readBody } from "@/lib/adminRoute";

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

export async function PUT(req: Request, { params }: Params) {
  const denied = await guard();
  if (denied) return denied;
  return run(async () => {
    const { id } = await params;
    const data = parseProject(await readBody(req));
    const updated = await prisma.project.update({ where: { id }, data });
    return NextResponse.json(updated);
  });
}

export async function DELETE(_req: Request, { params }: Params) {
  const denied = await guard();
  if (denied) return denied;
  return run(async () => {
    const { id } = await params;
    await prisma.project.delete({ where: { id } });
    return NextResponse.json({ ok: true });
  });
}
