import { useDispatch } from "react-redux";

import {
  addModal,
  hideModalOnIndex,
} from "@/utils/store/features/ui/modal.slice";
import { settingsServices } from "@/app/services/api/settings.services";
import usePostManagement from "./usePostManagement";

import Confirm from "@/components/administrator/modals/Confirm/confirm";

export const useSettingManagement = () => {
  const dispatch = useDispatch();
  const { execute } = usePostManagement();

  const saveGeneralSetting = async (data, callback) => {
    await execute({
      call: async () => {
        const result = await settingsServices.updateGeneralSetting(data);

        if (callback && typeof callback === "function") callback(result);
      },
      onError: "Failed to update general setting! Contact Administrator!",
      onSuccess: "Success! Please refresh the page to take effect.",
    });
  };

  const testBridgeUrl = async (data, callback, error, finallyFn) => {
    const { url, api_key } = data;
    await execute({
      call: async () => {
        const result = await settingsServices.testBridgeUrl(url, api_key);

        if (callback && typeof callback === "function") callback(result);
      },
      onError: "URL is invalid or cannot be reached!",
      onSuccess: "Congratulations! You can edit content from this url.",
      errorAction: () => {
        if (error && typeof error === "function") error();
      },
      finallyAction: () => {
        if (finallyFn && typeof finallyFn === "function") finallyFn();
      },
    });
  };

  const updateEndpoints = async (data, callback, error, finallyFn) => {
    console.log({
      cekdata: data,
    });
    await execute({
      call: async () => {
        const result = await settingsServices.updateBridgeSetting({
          endpoints: data,
        });

        if (callback && typeof callback === "function") callback(result);
      },
      onSuccess: "Update Endpoints Successfully!",
      onError: "Failed to update endpoint! Contact Administrator!",
      finallyAction: () => {
        if (finallyFn && typeof finallyFn === "function") finallyFn();
      },
      errorAction: () => {
        if (error && typeof error === "function") error();
      },
    });
  };

  const getAuthorization = async (data, callback, error, finallyFn) => {
    await execute({
      call: async () => {
        const result = await settingsServices.getAuthorization(
          data.url,
          data.ID
        );

        if (callback && typeof callback === "function") callback(result);
      },
      onSuccess: "Authorization Successfully!",
      onError: "Failed to get authorization! Contact Administrator!",
      finallyAction: () => {
        if (finallyFn && typeof finallyFn === "function") finallyFn();
      },
      errorAction: () => {
        if (error && typeof error === "function") error();
      },
    });
  };

  const setDefaultGeneralSetting = async (callback) => {
    dispatch(
      addModal({
        status: true,
        component: Confirm,
        data: {
          confirmTitle: "Set Default Setting",
          confirmText: "Are you sure you want to set default setting?",
        },
        accept: {
          text: "Set Default",
          action: async (config) => {
            await execute({
              call: async () => {
                const result =
                  await settingsServices.setDefaultGeneralSetting();

                if (callback && typeof callback === "function")
                  callback(result);
              },
              onError:
                "Failed to update general setting! Contact Administrator!",
              onSuccess: "Success! Please refresh the page to take effect.",
              successAction: () => {
                dispatch(hideModalOnIndex(config?.data?.index));
              },
              errorAction: () => {
                dispatch(hideModalOnIndex(config?.data?.index));
              },
            });
          },
        },
      })
    );
  };

  return {
    saveGeneralSetting,
    setDefaultGeneralSetting,
    getAuthorization,
    testBridgeUrl,
    updateEndpoints,
  };
};
