import { description } from "@/config/default";
import { updateSettings } from "@/models/settings";
import { response } from "@/utils/helper";

export async function PATCH(request) {
  try {
    const { searchParams } = new URL(request.url);
    const query = Object.fromEntries(searchParams.entries());
    let body = await request.json();
    let unset = null;

    if (query?.setDefault) {
      unset = { CMS_ID: "" };
      body = { data: description };
    }

    if (!body?.data)
      return response(400, "Bad Request", null, {
        error: "Data is unvalid or empty",
      });

    const result = await updateSettings(body.data, "general", unset);

    return response(200, "Set settings successfully", result);
  } catch (error) {
    console.log(error);
    return response(500, "Internal Server Error", null, {
      error: "Failed to set settings",
    });
  }
}
