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 ["name", "slug", "description", "sortOrder", "readLevel", "writeLevel", "allowAnonymousPost", "allowSecretComment", "status", "isAdultOnly", "categoryId", "mainPageViewTypeId", "listViewTypeId"]) {
    if (k in body) data[k] = body[k];
  }
  if ("requiredTags" in body) {
    try { data.requiredTags = typeof body.requiredTags === "string" ? JSON.parse(body.requiredTags) : body.requiredTags; } catch { /* ignore invalid */ }
  }
  if ("requiredFields" in body) {
    try { data.requiredFields = typeof body.requiredFields === "string" ? JSON.parse(body.requiredFields) : body.requiredFields; } catch { /* ignore invalid */ }
  }
  const updated = await prisma.board.update({ where: { id }, data });
  return NextResponse.json({ board: updated });
}

export async function DELETE(req: Request, context: { params: Promise<{ id: string }> }) {
  const { id } = await context.params;
  // Soft delete: mark as archived instead of physical deletion
  const updated = await prisma.board.update({ where: { id }, data: { status: 'archived' } });
  return NextResponse.json({ board: updated });
}


