import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { useTranslation } from "react-i18next";
import { useErrorHandler } from "@/shared/hooks/useErrorHandler";
import {
  getPositionTypeConfigurationsByUnitId,
  getPositionTypeConfigurationById,
  createPositionTypeConfiguration,
  updatePositionTypeConfiguration,
  CreatePositionTypeConfigurationPayload,
  UpdatePositionTypeConfigurationPayload,
} from "../services/api/positionTypeConfigurationService";

export const usePositionTypeConfiguration = (unitId?: string | null) => {
  const queryClient = useQueryClient();
  const { t } = useTranslation();
  const { handleError } = useErrorHandler(t);

  // GET: List by unitId
  const {
    data: configurationsData,
    isLoading: isLoadingConfigurations,
    isError: isConfigurationsError,
    refetch: refetchConfigurations,
  } = useQuery({
    queryKey: ["positionTypeConfigurations", unitId],
    queryFn: async () => {
      const response = await getPositionTypeConfigurationsByUnitId(unitId!);
      return response.data;
    },
    enabled: !!unitId,
  });

  // GET: Single by id
  const getConfigurationById = (id: string | null) => {
    // eslint-disable-next-line react-hooks/rules-of-hooks
    return useQuery({
      queryKey: ["positionTypeConfiguration", id],
      queryFn: async () => {
        const response = await getPositionTypeConfigurationById(id!);
        return response.data;
      },
      enabled: !!id,
    });
  };

  // POST: Create
  const createConfigurationMutation = useMutation({
    mutationFn: async (payload: CreatePositionTypeConfigurationPayload) => {
      const response = await createPositionTypeConfiguration(payload);
      return response.data;
    },
    onSuccess: () => {
      queryClient.invalidateQueries({
        queryKey: ["positionTypeConfigurations", unitId],
      });
    },
    onError: (error) => {
      handleError(error);
    },
  });

  // PUT: Update
  const updateConfigurationMutation = useMutation({
    mutationFn: async ({
      id,
      payload,
    }: {
      id: string;
      payload: UpdatePositionTypeConfigurationPayload;
    }) => {
      const response = await updatePositionTypeConfiguration(id, payload);
      return response.data;
    },
    onSuccess: () => {
      queryClient.invalidateQueries({
        queryKey: ["positionTypeConfigurations", unitId],
      });
    },
    onError: (error) => {
      handleError(error);
    },
  });

  return {
    // Queries
    configurations: configurationsData?.items || [],
    totalConfigurations: configurationsData?.count || 0,
    isLoadingConfigurations,
    isConfigurationsError,
    refetchConfigurations,
    getConfigurationById,

    // Mutations
    createConfiguration: createConfigurationMutation.mutateAsync,
    isCreatingConfiguration: createConfigurationMutation.isPending,
    updateConfiguration: updateConfigurationMutation.mutateAsync,
    isUpdatingConfiguration: updateConfigurationMutation.isPending,
  };
};
