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

export async function PATCH(req: Request, context: { params: Promise<{ id: string }> }) {
  const { id } = await context.params;
  const body = await req.json().catch(() => ({}));
  const data: any = {};
  for (const k of ["title", "imageUrl", "linkUrl", "active", "sortOrder", "startAt", "endAt"]) {
    if (k in body) data[k] = body[k];
  }
  if (data.startAt) data.startAt = new Date(data.startAt);
  if (data.endAt) data.endAt = new Date(data.endAt);
  const banner = await prisma.banner.update({ where: { id }, data });
  return NextResponse.json({ banner });
}

export async function DELETE(_: Request, context: { params: Promise<{ id: string }> }) {
  const { id } = await context.params;
  await prisma.banner.delete({ where: { id } });
  return NextResponse.json({ ok: true });
}


