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

// set the revalidate variable
export const revalidate = 5;
// GET METHOD
export async function GET(req:Request)
{
    try{
        // connect to the db
        dbConnect();
        // get the settings from the db
        let doc = await userModel.findOne({} , 'settings')
        // return the success response
        return NextResponse.json({
            success: true,
            message: "requestTerminatedWithSuccess",
            data: doc,
        }, {
            status: 200,
            headers: {
                "content-type": "application/json"
            }
        })
    }catch(e)
    {
        // catch any error and return an error response
        return NextResponse.json({
            success: false,
            message: "serverError",
        }, {
            status: 500,
            headers: {
                "content-type": "application/json"
            }
        })
    }
} 
// PUT METHOD
export async function PUT(req:Request)
{
    try{
        // connect to the db
        dbConnect();
        // get the request payload
        const { payload } = await req.json();
        // get data from formData
        const appName : string | null = payload.appName as string | null,
        appNameEN : string | null = payload.appNameEN as string | null,
        gymName : string | null = payload.gymName as string | null,
        email : string | null = payload.email as string | null,
        phone : string | null = payload.phone as string | null,
        address : string | null = payload.address as string | null,
        showLogo : boolean | null = payload.showLogo as boolean | null,
        logo : string | null = payload.logo as string | null,
        currencySymbol : string | null = payload.currencySymbol as string | null;
        // update the doc
        let updated_doc = await userModel.updateMany({} , {
            $set: {
                settings: {
                    appName,
                    appNameEN,
                    gymName,
                    email,
                    phone,
                    address,
                    showLogo,
                    logo,
                    currencySymbol
                }
            }
        }, { new: true })
        // return the success response
        return NextResponse.json({
            success: true,
            message: "requestTerminatedWithSuccess",
            data: updated_doc,
        }, {
            status: 200,
            headers: {
                "content-type": "application/json"
            }
        })
    }catch(e)
    {
        // catch any error and return an error response
        return NextResponse.json({
            success: false,
            message: "serverError",
        }, {
            status: 500,
            headers: {
                "content-type": "application/json"
            }
        })
    }
}