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

export async function GET() {
  try {
    const albums = await prisma.cms_gallery_albums.findMany({
      where: { status: true },
      orderBy: { sort_order: "asc" },
      select: {
        id: true,
        title: true,
        slug: true,
        cover: true,
      },
    });

    const data = JSON.parse(
      JSON.stringify(albums, (_, 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 gallery albums:", error);

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