import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { useTranslation } from "react-i18next";
import { toast } from "sonner";

import { useErrorHandler } from "@/shared/hooks/useErrorHandler";
import {
  getArchivedUnits,
  restoreUnit,
  softDeleteUnit,
} from "@/user-management/services/api/unitService";
import {
  getArchivedPositions,
  restorePosition,
  softDeletePosition,
} from "@/user-management/services/api/positionService";
import {
  getArchivedOrganizations,
  restoreOrganization,
  softDeleteOrganization,
} from "@/shared/services/organizationsService";

export const useArchivedUnits = (parentId: string | undefined) => {
  return useQuery({
    queryKey: ["archived-units", parentId],
    queryFn: async () => {
      if (!parentId) return null;
      const { data } = await getArchivedUnits(parentId);
      return data;
    },
    staleTime: 0,
    refetchOnMount: "always",
    refetchOnWindowFocus: false,
  });
};

export const useArchivedOrganizations = () => {
  return useQuery({
    queryKey: ["archived-organizations"],
    queryFn: async () => {
      const { data } = await getArchivedOrganizations();
      return data;
    },
    staleTime: 0,
    refetchOnMount: "always",
    refetchOnWindowFocus: false,
  });
};

export const useArchivedPositions = (parentId: string | undefined) => {
  return useQuery({
    queryKey: ["archived-positions", parentId],
    queryFn: async () => {
      if (!parentId) return null;
      const { data } = await getArchivedPositions(parentId);
      return data;
    },
    staleTime: 0,
    refetchOnMount: "always",
    refetchOnWindowFocus: false,
  });
};

export const useArchiveActions = () => {
  const queryClient = useQueryClient();
  const { t } = useTranslation();
  const { handleError } = useErrorHandler(t);

  const invalidateArchivedUnits = () => {
    queryClient.invalidateQueries({ queryKey: ["archived-units"] });
    queryClient.invalidateQueries({ queryKey: ["unitList"] });
    queryClient.invalidateQueries({ queryKey: ["unitChildren"] });
  };

  const invalidateArchivedPositions = () => {
    queryClient.invalidateQueries({ queryKey: ["archived-positions"] });
    queryClient.invalidateQueries({ queryKey: ["positions"] });
  };

  const invalidateArchivedOrganizations = () => {
    queryClient.invalidateQueries({ queryKey: ["archived-organizations"] });
    queryClient.invalidateQueries({ queryKey: ["organizations"] });
    queryClient.invalidateQueries({ queryKey: ["organizationsResponse"] });
  };

  const softDeleteUnitMutation = useMutation({
    mutationFn: (id: string) => softDeleteUnit(id),
    onSuccess: () => {
      toast.success(t("archive.unitArchived", "Unit archived"));
      invalidateArchivedUnits();
    },
    onError: handleError,
  });

  const restoreUnitMutation = useMutation({
    mutationFn: (id: string) => restoreUnit(id),
    onSuccess: () => {
      toast.success(t("archive.unitRestored", "Unit restored"));
      invalidateArchivedUnits();
    },
    onError: handleError,
  });

  const softDeletePositionMutation = useMutation({
    mutationFn: (id: string) => softDeletePosition(id),
    onSuccess: () => {
      toast.success(t("archive.positionArchived", "Position archived"));
      invalidateArchivedPositions();
    },
    onError: handleError,
  });

  const restorePositionMutation = useMutation({
    mutationFn: (id: string) => restorePosition(id),
    onSuccess: () => {
      toast.success(t("archive.positionRestored", "Position restored"));
      invalidateArchivedPositions();
    },
    onError: handleError,
  });

  const softDeleteOrganizationMutation = useMutation({
    mutationFn: (id: string) => softDeleteOrganization(id),
    onSuccess: () => {
      toast.success(t("archive.organizationArchived", "Organization archived"));
      invalidateArchivedOrganizations();
    },
    onError: handleError,
  });

  const restoreOrganizationMutation = useMutation({
    mutationFn: (id: string) => restoreOrganization(id),
    onSuccess: () => {
      toast.success(
        t("archive.organizationRestored", "Organization restored"),
      );
      invalidateArchivedOrganizations();
    },
    onError: handleError,
  });

  return {
    softDeleteUnit: softDeleteUnitMutation.mutate,
    isArchivingUnit: softDeleteUnitMutation.isPending,
    restoreUnit: restoreUnitMutation.mutate,
    isRestoringUnit: restoreUnitMutation.isPending,
    softDeletePosition: softDeletePositionMutation.mutate,
    isArchivingPosition: softDeletePositionMutation.isPending,
    restorePosition: restorePositionMutation.mutate,
    isRestoringPosition: restorePositionMutation.isPending,
    softDeleteOrganization: softDeleteOrganizationMutation.mutate,
    isArchivingOrganization: softDeleteOrganizationMutation.isPending,
    restoreOrganization: restoreOrganizationMutation.mutate,
    isRestoringOrganization: restoreOrganizationMutation.isPending,
  };
};
