import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { toast } from "sonner";
import { useTranslation } from "react-i18next";
import { useErrorHandler } from "@/shared/hooks/useErrorHandler";
import {
  removeUnitAdminRole,
  assignFirstsForSecond,
  assignSecondsForFirst,
  deleteUserRole,
  removeFirstsForSecond,
  removeSecondsForFirst,
  RemoveOrAssignUnitAdminPayload,
  removeOrgAdminRole,
  RemoveOrAssignOrgAdminPayload,
} from "@/super-admin/services/api/userRoleService";

export const useUserRoles = () => {
  const queryClient = useQueryClient();
  const { t } = useTranslation();
  const { handleError } = useErrorHandler(t);
  // Remove Unit Admin Role
  const { mutateAsync: removeAdminRole, isPending: isRemoving } = useMutation({
    mutationFn: (payload: RemoveOrAssignUnitAdminPayload) =>
      removeUnitAdminRole(payload),
    onSuccess: () => {
      toast.success("Unit admin role removed successfully");
      queryClient.invalidateQueries({ queryKey: ["organizationAdmins"] });
    },
    onError: (error) => {
      handleError(error);
    },
  });
  const { mutateAsync: removeUnitAdmin, isPending: isRemovingUnitAdmin } =
    useMutation({
      mutationFn: (payload: RemoveOrAssignOrgAdminPayload) =>
        removeOrgAdminRole(payload),
      onSuccess: () => {
        toast.success("Unit admin role removed successfully");
        queryClient.invalidateQueries({ queryKey: ["organizationAdmins"] });
      },
      onError: (error) => {
        handleError(error);
      },
    });

  // Assign Firsts for Second
  const { mutate: assignFirsts, isPending: isAssigningFirsts } = useMutation({
    mutationFn: (payload: { secondId: string; firstIds: string[] }) =>
      assignFirstsForSecond(payload),
    onSuccess: () => {
      toast.success("Firsts assigned successfully");
    },
    onError: (error) => {
      handleError(error);
    },
  });

  // Assign Seconds for First
  const { mutate: assignSeconds, isPending: isAssigningSeconds } = useMutation({
    mutationFn: (payload: { firstId: string; secondIds: string[] }) =>
      assignSecondsForFirst(payload),
    onSuccess: () => {
      toast.success("Seconds assigned successfully");
    },
    onError: (error) => {
      handleError(error);
    },
  });

  // Remove Firsts for Second
  const { mutate: removeFirsts, isPending: isRemovingFirsts } = useMutation({
    mutationFn: (payload: { secondId: string; firstIds: string[] }) =>
      removeFirstsForSecond(payload),
    onSuccess: () => {
      toast.success("Firsts removed successfully");
    },
    onError: (error) => {
      handleError(error);
    },
  });

  // Remove Seconds for First
  const { mutate: removeSeconds, isPending: isRemovingSeconds } = useMutation({
    mutationFn: (payload: { firstId: string; secondIds: string[] }) =>
      removeSecondsForFirst(payload),
    onSuccess: () => {
      toast.success("Seconds removed successfully");
    },
    onError: (error) => {
      handleError(error);
    },
  });

  // Delete User Role by ID
  const { mutate: deleteRole, isPending: isDeleting } = useMutation({
    mutationFn: (id: string) => deleteUserRole(id),
    onSuccess: () => {
      toast.success("Role deleted successfully");
    },
    onError: (error) => {
      handleError(error);
    },
  });

  return {
    removeAdminRole,
    removeUnitAdmin,
    assignFirsts,
    assignSeconds,
    removeFirsts,
    removeSeconds,
    deleteRole,
    isRemoving,
    isRemovingUnitAdmin,
    isAssigningFirsts,
    isAssigningSeconds,
    isRemovingFirsts,
    isRemovingSeconds,
    isDeleting,
  };
};
