import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import {
  CreatePrefixPayload,
  CreateSuffixPayload,
  prefixSuffixService,
} from "@/user-management/services/api/prefixSuffixService";

export const usePrefixesList = (
  unitId: string,
  recordTypeKey: string,
  skip = 0,
  take = 10
) =>
  useQuery({
    queryKey: ["prefixes", unitId, recordTypeKey, { cc: false }],
    queryFn: () => prefixSuffixService.getPrefix(unitId, recordTypeKey, skip, take),
    enabled: !!unitId && !!recordTypeKey,
  });

export const useCCPrefixesList = (
  unitId: string,
  recordTypeKey: string,
  skip = 0,
  take = 10
) =>
  useQuery({
    queryKey: ["prefixes", unitId, recordTypeKey, { cc: true }],
    queryFn: () =>
      prefixSuffixService.getPrefixCC(unitId, recordTypeKey, skip, take),
    enabled: !!unitId && !!recordTypeKey,
  });

export const usePositionPrefixesList = (
  unitId: string,
  recordTypeKey: string,
  skip = 0,
  take = 10
) =>
  useQuery({
    queryKey: ["prefixes-by-position", unitId, recordTypeKey],
    queryFn: () =>
      prefixSuffixService.getPrefixByPosition(unitId, recordTypeKey, skip, take),
    enabled: !!unitId && !!recordTypeKey,
  });

export const usePrefixDetail = (id: string) =>
  useQuery({
    queryKey: ["prefix", id],
    queryFn: () => prefixSuffixService.getPrefixById(id),
    enabled: !!id,
  });

export const useCreatePrefix = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (payload: CreatePrefixPayload) =>
      prefixSuffixService.createPrefix(payload),
    onSuccess: (_, payload) => {
      queryClient.invalidateQueries({
        queryKey: [
          "prefixes",
          payload.unitId,
          payload.recordTypeKey,
          { cc: payload.isForCC },
        ],
      });
    },
  });
};

export const useUpdatePrefix = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({ id, payload }: { id: string; payload: CreatePrefixPayload }) =>
      prefixSuffixService.editPrefix(id, payload),
    onSuccess: (_, variables) => {
      queryClient.invalidateQueries({
        queryKey: [
          "prefixes",
          variables.payload.unitId,
          variables.payload.recordTypeKey,
          { cc: variables.payload.isForCC },
        ],
      });
      queryClient.invalidateQueries({ queryKey: ["prefix", variables.id] });
    },
  });
};

export const useDeletePrefix = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({
      id,
    }: {
      id: string;
      unitId: string;
      recordTypeKey: string;
      isForCC: boolean;
    }) => prefixSuffixService.deletePrefix(id),
    onSuccess: (_, variables) => {
      queryClient.invalidateQueries({
        queryKey: [
          "prefixes",
          variables.unitId,
          variables.recordTypeKey,
          { cc: variables.isForCC },
        ],
      });
      queryClient.invalidateQueries({ queryKey: ["prefix", variables.id] });
    },
  });
};

export const useSuffixesList = (
  unitId: string,
  recordTypeKey: string,
  skip = 0,
  take = 10
) =>
  useQuery({
    queryKey: ["suffixes", unitId, recordTypeKey, { cc: false }],
    queryFn: () => prefixSuffixService.getSuffix(unitId, recordTypeKey, skip, take),
    enabled: !!unitId && !!recordTypeKey,
  });

export const useCCSuffixesList = (
  unitId: string,
  recordTypeKey: string,
  skip = 0,
  take = 10
) =>
  useQuery({
    queryKey: ["suffixes", unitId, recordTypeKey, { cc: true }],
    queryFn: () =>
      prefixSuffixService.getSuffixCC(unitId, recordTypeKey, skip, take),
    enabled: !!unitId && !!recordTypeKey,
  });

export const useSuffixDetail = (id: string) =>
  useQuery({
    queryKey: ["suffix", id],
    queryFn: () => prefixSuffixService.getSuffixById(id),
    enabled: !!id,
  });

export const useCreateSuffix = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (payload: CreateSuffixPayload) =>
      prefixSuffixService.createSuffix(payload),
    onSuccess: (_, payload) => {
      queryClient.invalidateQueries({
        queryKey: [
          "suffixes",
          payload.unitId,
          payload.recordTypeKey,
          { cc: payload.isForCC },
        ],
      });
    },
  });
};

export const useUpdateSuffix = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({ id, payload }: { id: string; payload: CreateSuffixPayload }) =>
      prefixSuffixService.editSuffix(id, payload),
    onSuccess: (_, variables) => {
      queryClient.invalidateQueries({
        queryKey: [
          "suffixes",
          variables.payload.unitId,
          variables.payload.recordTypeKey,
          { cc: variables.payload.isForCC },
        ],
      });
      queryClient.invalidateQueries({ queryKey: ["suffix", variables.id] });
    },
  });
};

export const useDeleteSuffix = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({
      id,
    }: {
      id: string;
      unitId: string;
      recordTypeKey: string;
      isForCC: boolean;
    }) => prefixSuffixService.deleteSuffix(id),
    onSuccess: (_, variables) => {
      queryClient.invalidateQueries({
        queryKey: [
          "suffixes",
          variables.unitId,
          variables.recordTypeKey,
          { cc: variables.isForCC },
        ],
      });
      queryClient.invalidateQueries({ queryKey: ["suffix", variables.id] });
    },
  });
};
