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

// Must match the PAGE_ID used in the admin route
// (src/app/api/admin/cms/class-levels/route.ts)
const PAGE_ID = BigInt(3);

export async function GET() {
  try {
    const classLevels = await prisma.cms_class_levels.findMany({
      where: {
        page_id: PAGE_ID,
        status: true,
      },
      orderBy: {
        sort_order: "asc",
      },
    });

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

    return NextResponse.json(
      { success: true, data },
      {
        status: 200,
        headers: {
          "Cache-Control": "no-store",
        },
      }
    );
  } catch (error) {
    console.error("Error fetching Class Levels:", error);

    return NextResponse.json(
      {
        success: false,
        message: "Failed to fetch Class Levels data",
        error: error instanceof Error ? error.message : String(error),
      },
      { status: 500 }
    );
  }
}