import { NextRequest, NextResponse } from "next/server";
import dbConnect from "@/lib/mongodb";
import CMS from "@/models/CMS";

export async function GET(request: NextRequest) {
  try {
    await dbConnect();

    const { searchParams } = new URL(request.url);
    const slug = searchParams.get("slug");

    if (!slug) {
      return NextResponse.json(
        {
          success: false,
          message: "Slug is required",
        },
        { status: 400 }
      );
    }

    const cms = await CMS.findOne({ slug }).lean();

    if (!cms) {
      return NextResponse.json(
        {
          success: false,
          message: "CMS page not found",
        },
        { status: 404 }
      );
    }

    return NextResponse.json({
      success: true,
      data: cms,
    });
  } catch (error: any) {
    return NextResponse.json(
      {
        success: false,
        message: error.message,
      },
      { status: 500 }
    );
  }
}