import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { toast } from "sonner";
import { useTranslation } from "react-i18next";
import { useErrorHandler } from "@/shared/hooks/useErrorHandler";
import {
  employeePositionChangeRequestService,
  type BulkApproveEmployeePositionChangePayload,
  type EmployeePositionChangeDecisionPayload,
  type EmployeePositionChangeRequestParams,
  type TransferEmployeePositionPayload,
} from "@/user-management/services/api/employeePositionChangeRequestService";

export const EMPLOYEE_POSITION_CHANGE_REQUESTS_QUERY_KEY =
  "employee-position-change-requests";

interface DecisionVariables extends EmployeePositionChangeDecisionPayload {
  id: string;
}

export const useEmployeePositionChangeRequests = (
  params: EmployeePositionChangeRequestParams = {},
) => {
  const queryClient = useQueryClient();
  const { t } = useTranslation();
  const { handleError } = useErrorHandler(t);

  const requestsQuery = useQuery({
    queryKey: [EMPLOYEE_POSITION_CHANGE_REQUESTS_QUERY_KEY, params],
    queryFn: () => employeePositionChangeRequestService.list(params),
  });

  const getRequestByIdQuery = (id?: string | null) =>
    useQuery({
      queryKey: [EMPLOYEE_POSITION_CHANGE_REQUESTS_QUERY_KEY, id],
      queryFn: () => employeePositionChangeRequestService.getById(id!),
      enabled: !!id,
    });

  const invalidateRequests = () =>
    queryClient.invalidateQueries({
      queryKey: [EMPLOYEE_POSITION_CHANGE_REQUESTS_QUERY_KEY],
    });

  const transferMutation = useMutation({
    mutationFn: (payload: TransferEmployeePositionPayload) =>
      employeePositionChangeRequestService.transfer(payload),
    onSuccess: () => {
      invalidateRequests();
    },
    onError: (error) => {
      handleError(error);
    },
  });

  const bulkApproveMutation = useMutation({
    mutationFn: (payload: BulkApproveEmployeePositionChangePayload) =>
      employeePositionChangeRequestService.bulkApprove(payload),
    onSuccess: () => {
      toast.success("Employee position change requests approved successfully");
      invalidateRequests();
    },
    onError: (error) => {
      handleError(error);
    },
  });

  const approveMutation = useMutation({
    mutationFn: ({ id, comment }: DecisionVariables) =>
      employeePositionChangeRequestService.approve(id, { comment }),
    onSuccess: () => {
      toast.success("Employee position change request approved successfully");
      invalidateRequests();
    },
    onError: (error) => {
      handleError(error);
    },
  });

  const rejectMutation = useMutation({
    mutationFn: ({ id, comment }: DecisionVariables) =>
      employeePositionChangeRequestService.reject(id, { comment }),
    onSuccess: () => {
      toast.success("Employee position change request rejected successfully");
      invalidateRequests();
    },
    onError: (error) => {
      handleError(error);
    },
  });

  return {
    requestsQuery,
    getRequestByIdQuery,
    transfer: transferMutation.mutateAsync,
    isTransferring: transferMutation.isPending,
    bulkApprove: bulkApproveMutation.mutateAsync,
    isBulkApproving: bulkApproveMutation.isPending,
    approve: approveMutation.mutateAsync,
    isApproving: approveMutation.isPending,
    reject: rejectMutation.mutateAsync,
    isRejecting: rejectMutation.isPending,
  };
};
