import dbConnect from "@/database/dbConnect";
import { NextResponse } from "next/server";
import serviceModel from "@/database/models/serviceModel";

// GET METHOD - Load all active services without pagination
export async function GET(req: Request) {
    try {
        // connect to the db
        await dbConnect();
        
        // get all active services
        const docs = await serviceModel.find({ status: "active" }).sort({ addedAt: -1 });
        
        // prepare the return data
        return NextResponse.json({
            success: true,
            message: "docsGetedSuccessfully",
            data: {
                docs,
                docs_count: docs.length,
            }
        }, {
            status: 200,
            headers: {
                "content-type": "application/json"
            }
        });
    } catch (e) {
        console.log(e);
        // catch any error and return an error response
        return NextResponse.json({
            success: false,
            message: "serverError",
        }, {
            status: 500,
            headers: {
                "content-type": "application/json"
            }
        });
    }
}

// set the revalidate variable
export const revalidate = 5;