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

export async function GET() {
  const items = await prisma.boardViewType.findMany({ orderBy: [{ scope: 'asc' }, { name: 'asc' }] });
  return NextResponse.json({ items });
}

const createSchema = z.object({
  key: z.string().min(1),
  name: z.string().min(1),
  scope: z.string().min(1), // 'main' | 'list' 등 자유 텍스트
});

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 exists = await prisma.boardViewType.findFirst({ where: { key: parsed.data.key } });
  if (exists) return NextResponse.json({ error: 'duplicate_key' }, { status: 409 });
  const created = await prisma.boardViewType.create({ data: parsed.data });
  return NextResponse.json({ item: created }, { status: 201 });
}


