import { NextRequest, NextResponse } from "next/server";
import { fetchBoardColumns } from "@/lib/monday";
import { requireApiAuth, handleApiError } from "@/lib/api-auth";
import { prisma } from "@/lib/db";

export async function POST(request: NextRequest) {
  try {
    await requireApiAuth();
    const body = await request.json().catch(() => ({}));
    const boardId = String(body.boardId || "");

    const settings = await prisma.appSettings.findUnique({ where: { id: "singleton" } });
    const token = settings?.mondayApiToken || "";

    if (!token || !boardId) {
      return NextResponse.json(
        { error: "API token and board ID are required" },
        { status: 400 }
      );
    }

    const columns = await fetchBoardColumns(token, boardId);
    return NextResponse.json({ columns });
  } catch (error) {
    return handleApiError(error);
  }
}
