import usePostManagement from "@/hooks/usePostManagement";
import { useDispatch } from "react-redux";
import {
  addModal,
  hideModalOnIndex,
} from "@/utils/store/features/ui/modal.slice";
import Confirm from "@/components/administrator/modals/Confirm/confirm";
import {
  deleteMultipleUser as deleteMultipleUserData,
  deleteUserById,
  updateUserById,
  createEmptyUser as createEmptyUserData,
  updateUserStatusById,
} from "@/services/users";
import { useCallback } from "react";

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

  const createEmptyUser = async (callback) => {
    await execute({
      call: async () => {
        const result = await createEmptyUserData();
        if (callback && typeof callback === "function") callback(result);
      },
      onError: "Creating User Failed! Contact Administrator.",
      onSuccess: "Created User Successfully!",
    });
  };

  const updateUser = async (data, callback) => {
    execute({
      call: async () => {
        const result = await updateUserById(data);
        if (callback && typeof callback === "function") callback(result);
      },
      onError: "Updating User Failed! Contact Administrator.",
      onSuccess: "Updated User Successfully!",
    });
  };

  const deleteUser = async (id, callback) => {
    await execute({
      call: async () => {
        const result = await deleteUserById(id);
        if (callback && typeof callback === "function") callback(result);
      },
      onError: "Deleting User Failed! Contact Administrator.",
      onSuccess: "Deleted User Successfully!",
    });
  };

  const deleteMultipleUser = async (ids, callback) => {
    dispatch(
      addModal({
        status: true,
        component: Confirm,
        data: {
          confirmTitle: "Delete User",
          confirmText: `Are you sure you want to delete <span style="color:red; font-weight:700; margin: 0 5px">${ids.length}</span> users?`,
        },
        accept: {
          text: "Delete",
          action: async (config) => {
            await execute({
              call: async () => {
                const result = await deleteMultipleUserData(ids);
                if (callback && typeof callback === "function")
                  callback(result);
              },
              onError: "Deleting User Failed! Contact Administrator.",
              onSuccess: "Deleted User Successfully!",
              successAction: () => {
                dispatch(hideModalOnIndex(config?.data?.index));
              },
              errorAction: () => {
                dispatch(hideModalOnIndex(config?.data?.index));
              },
            });
          },
        },
      })
    );
  };

  const disableUser = useCallback(
    async (id, callback) => {
      dispatch(
        addModal({
          status: true,
          component: Confirm,
          data: {
            confirmTitle: "Update User Status",
            confirmText: `Are you sure you want to disable this user?`,
          },
          accept: {
            text: "Disable",
            action: async (config) => {
              await execute({
                call: async () => {
                  const result = await updateUserStatusById(id, false);
                  if (callback && typeof callback === "function")
                    callback(result);
                },
                onError: "Disabling User Failed! Contact Administrator.",
                onSuccess: "Disabling User Successfully!",
                successAction: () => {
                  dispatch(hideModalOnIndex(config?.data?.index));
                },
                errorAction: () => {
                  dispatch(hideModalOnIndex(config?.data?.index));
                },
              });
            },
          },
        })
      );
    },
    [dispatch, execute]
  );

  const enableUser = async (id, callback) => {
    await execute({
      call: async () => {
        const result = await updateUserStatusById(id, true);
        if (callback && typeof callback === "function") callback(result);
      },
      onError: "Enabling User Failed! Contact Administrator.",
      onSuccess: "Enabling User Successfully!",
      successAction: () => {},
      errorAction: () => {},
    });
  };

  return {
    createEmptyUser,
    updateUser,
    deleteUser,
    deleteMultipleUser,
    disableUser,
    enableUser,
  };
};
