import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import {
  AddReferenceNumberPayload,
  DeleteReferenceNumberPayload,
  unitConfigurationService,
  UpdateExternalReferencePrefixPayload,
  UpdateInternalMemoPrefixPayload,
} from "@/shared/services/unitConfigurationService";

export const useGetReferenceNumbers = (unitId: string) =>
  useQuery({
    queryKey: ["unit-reference-numbers", unitId],
    queryFn: () => unitConfigurationService.getReferenceNumbers(unitId),
    enabled: !!unitId,
  });

export const useGetTagBasedReferences = (unitId: string, tagId?: string) =>
  useQuery({
    queryKey: ["tag-based-reference-numbers", unitId, tagId],
    queryFn: () =>
      unitConfigurationService.getTagBasedReferences(unitId, tagId),
    enabled: !!unitId,
  });

export const useGetUnitConfiguration = (unitId: string) =>
  useQuery({
    queryKey: ["unit-configuration", unitId],
    queryFn: () => unitConfigurationService.getUnitConfiguration(unitId),
    enabled: !!unitId,
  });

export const useAddReferenceNumber = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({
      unitId,
      payload,
    }: {
      unitId: string;
      payload: AddReferenceNumberPayload;
    }) => unitConfigurationService.addReferenceNumber(unitId, payload),
    onSuccess: (_, variables) => {
      queryClient.invalidateQueries({
        queryKey: ["unit-reference-numbers", variables.unitId],
      });
      queryClient.invalidateQueries({
        queryKey: ["unit-configuration", variables.unitId],
      });
    },
  });
};

export const useUpdateInternalMemoReferencePrefix = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({
      unitId,
      payload,
    }: {
      unitId: string;
      payload: UpdateInternalMemoPrefixPayload;
    }) =>
      unitConfigurationService.updateInternalMemoReferencePrefix(
        unitId,
        payload,
      ),
    onSuccess: (_, variables) => {
      queryClient.invalidateQueries({
        queryKey: ["unit-reference-numbers", variables.unitId],
      });
      queryClient.invalidateQueries({
        queryKey: ["unit-configuration", variables.unitId],
      });
    },
  });
};

export const useUpdateExternalReferencePrefix = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({
      unitId,
      payload,
    }: {
      unitId: string;
      payload: UpdateExternalReferencePrefixPayload;
    }) =>
      unitConfigurationService.updateExternalReferencePrefix(unitId, payload),
    onSuccess: (_, variables) => {
      queryClient.invalidateQueries({
        queryKey: ["unit-reference-numbers", variables.unitId],
      });
      queryClient.invalidateQueries({
        queryKey: ["unit-configuration", variables.unitId],
      });
    },
  });
};

export const useDeleteReferenceNumber = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({
      unitId,
      id,
      payload,
    }: {
      unitId: string;
      id: string;
      payload: DeleteReferenceNumberPayload;
    }) => unitConfigurationService.deleteReferenceNumber(id, payload),
    onSuccess: (_, variables) => {
      queryClient.invalidateQueries({
        queryKey: ["unit-reference-numbers", variables.unitId],
      });
      queryClient.invalidateQueries({
        queryKey: ["unit-configuration", variables.unitId],
      });
    },
  });
};
