import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { parseAffiliate } 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 = parseAffiliate(await readBody(req));
    const updated = await prisma.affiliate.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.affiliate.delete({ where: { id } });
    return NextResponse.json({ ok: true });
  });
}
