import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { requireAdmin, unauthorized } from "@/lib/admin-api";
import { SponsorSlot } from "@prisma/client";

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

export async function PATCH(req: Request, ctx: Ctx) {
  const admin = await requireAdmin();
  if (!admin) return unauthorized();
  const { id } = await ctx.params;
  const body = await req.json();
  const s = await prisma.sponsor.update({
    where: { id },
    data: {
      ...(body.name != null && { name: body.name }),
      ...(body.logoUrl !== undefined && { logoUrl: body.logoUrl }),
      ...(body.url !== undefined && { url: body.url }),
      ...(body.slot && { slot: body.slot as SponsorSlot }),
      ...(body.active !== undefined && { active: Boolean(body.active) }),
      ...(body.sortOrder != null && { sortOrder: Number(body.sortOrder) }),
    },
  });
  return NextResponse.json(s);
}

export async function DELETE(_req: Request, ctx: Ctx) {
  const admin = await requireAdmin();
  if (!admin) return unauthorized();
  await prisma.sponsor.delete({ where: { id: (await ctx.params).id } });
  return NextResponse.json({ ok: true });
}
