import { setNotification as setNotif } from "@/utils/store/features/ui/notification.slice";
import { useDispatch } from "react-redux";
import {
  addModal,
  hideModalOnIndex,
} from "@/utils/store/features/ui/modal.slice";
import Confirm from "@/components/administrator/modals/Confirm/confirm";

import { useCallback } from "react";
import { galleryServices } from "@/app/services/api/gallery.services";

const useGalleryManagement = () => {
  const dispatch = useDispatch();

  const execute = useCallback(
    async ({
      call,
      onError,
      onSuccess,
      successAction,
      errorAction,
      onLoaded,
    }) => {
      try {
        await call();

        dispatch(
          setNotif({
            status: true,
            message: onSuccess,
            timeout: 3000,
          })
        );

        if (successAction && typeof successAction === "function")
          successAction();
        return true;
      } catch (error) {
        dispatch(
          setNotif({
            status: true,
            message: onError,
            type: "error",
            timeout: 3000,
          })
        );

        if (errorAction && typeof errorAction === "function") errorAction();
        throw error;
      } finally {
        if (onLoaded && typeof onLoaded === "function") onLoaded();
      }
    },
    [dispatch]
  );

  const gallery = async ({ page, limit = 100, onSuccess, onLoaded }) => {
    await execute({
      call: async () => {
        const data = await galleryServices.getGallery({ page, limit });

        if (onSuccess && typeof onSuccess === "function") onSuccess(data);
      },
      onError: "Getting Media Failed! Contact Administrator.",
      onSuccess: "Getting Media Successfully!",
      onLoaded,
    });
  };

  const update = async (data, callback = () => {}) => {
    execute({
      call: async () => {
        const result = await galleryServices.updateGalleryById(data);
        if (callback && typeof callback === "function") callback(result);
      },
      onError: "Updating Media Failed! Contact Administrator.",
      onSuccess: "Updating Media Successfully!",
      successAction: () => {},
      errorAction: () => {},
    });
  };

  const deleteconfirm = async (data, callback = () => {}) => {
    dispatch(
      addModal({
        status: true,
        component: Confirm,
        data: {
          confirmTitle: "Delete Media Data",
          confirmText: "Are you sure you want to delete this media?",
        },
        accept: {
          text: "Delete",
          action: async (config) => {
            await execute({
              call: async () => {
                const result = await galleryServices.deleteMedia(data._id);
                if (callback && typeof callback === "function")
                  callback(result);
              },
              onError: "Deleting Media Failed! Contact Administrator.",
              onSuccess: "Deleted Media Successfully!",
              successAction: () => {
                dispatch(hideModalOnIndex(config?.data?.index));
              },
              errorAction: () => {
                dispatch(hideModalOnIndex(config?.data?.index));
              },
            });
          },
        },
      })
    );
  };

  const deleteconfirmMultipe = async (data, callback = () => {}) => {
    dispatch(
      addModal({
        status: true,
        component: Confirm,
        data: {
          confirmTitle: "Delete Media Data",
          confirmText: `Are you sure you want to delete ${data.length} media?`,
        },
        accept: {
          text: "Delete",
          action: async (config) => {
            await execute({
              call: async () => {
                const result = await galleryServices.deleteMediaMultiple(
                  data.map((d) => d._id)
                );
                if (callback && typeof callback === "function")
                  callback(result);
              },
              onError: "Deleting Media Failed! Contact Administrator.",
              onSuccess: `Deleted ${data.length} Media Successfully!`,
              successAction: () => {
                dispatch(hideModalOnIndex(config?.data?.index));
              },
              errorAction: () => {
                dispatch(hideModalOnIndex(config?.data?.index));
              },
            });
          },
        },
      })
    );
  };

  return { deleteconfirm, deleteconfirmMultipe, update, gallery };
};

export default useGalleryManagement;
