import { prisma } from "@/lib/prisma";
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(req: Request) {
  try {
    const { searchParams } = new URL(req.url);
    const pageIdParam = searchParams.get("page_id");

    const page_id = pageIdParam ? BigInt(pageIdParam) : BigInt(1);

    const data = await prisma.cms_training_approach.findMany({
      where: {
        page_id,
        status: true,
      },
      orderBy: { sort_order: "asc" },
    });

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