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

export async function GET() {
  const boards = await prisma.board.findMany({
    where: { NOT: { status: 'archived' } },
    orderBy: { sortOrder: "asc" },
    select: {
      id: true,
      name: true,
      slug: true,
      description: true,
      sortOrder: true,
      readLevel: true,
      writeLevel: true,
      allowAnonymousPost: true,
      allowSecretComment: true,
      status: true,
      categoryId: true,
      mainPageViewTypeId: true,
      listViewTypeId: true,
      category: { select: { id: true, name: true, slug: true } },
    },
  });
  return NextResponse.json({ boards });
}

const createSchema = z.object({
  name: z.string().min(1),
  slug: z.string().min(1),
  description: z.string().optional(),
  sortOrder: z.coerce.number().int().optional(),
  readLevel: z.string().optional(),
  writeLevel: z.string().optional(),
  allowAnonymousPost: z.boolean().optional(),
  allowSecretComment: z.boolean().optional(),
  status: z.string().optional(),
  isAdultOnly: z.boolean().optional(),
  categoryId: z.string().nullable().optional(),
});

export async function POST(req: Request) {
  const body = await req.json().catch(() => ({}));
  const parsed = createSchema.safeParse(body);
  if (!parsed.success) return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 });
  const data = parsed.data;
  // sortOrder 기본: 같은 카테고리 내 마지막 다음 순서
  let sortOrder = data.sortOrder;
  if (!sortOrder) {
    const max = await prisma.board.aggregate({
      _max: { sortOrder: true },
      where: { categoryId: data.categoryId ?? undefined },
    });
    sortOrder = (max._max.sortOrder ?? 0) + 1;
  }
  const created = await prisma.board.create({ data: { ...data, sortOrder } });
  return NextResponse.json({ board: created }, { status: 201 });
}


