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

const serializeBigInt = (data: any) =>
  JSON.parse(
    JSON.stringify(data, (_, value) =>
      typeof value === "bigint" ? value.toString() : value
    )
  );

export async function GET() {
  const admin = await getAdminUser();

  if (!admin) {
    return NextResponse.json(
      { success: false, message: "Unauthorized" },
      { status: 401 }
    );
  }

  try {
    const pages = await prisma.cms_pages.findMany({
      where: { status: true },
      orderBy: { page_name: "asc" },
      select: { id: true, page_name: true, slug: true },
    });

    return NextResponse.json({
      success: true,
      data: serializeBigInt(pages),
    });
  } catch (error: any) {
    return NextResponse.json(
      { success: false, message: error.message || "Failed to fetch pages" },
      { status: 500 }
    );
  }
}