import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { CreateBankType } from "../types/CardBankTypes";
import * as api from "../service/CardBankService";

export const useCreateBank = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (variables: CreateBankType) => api.createCardBank(variables),
    onSuccess: () => {
      queryClient.refetchQueries({
        queryKey: ["cardBanks"],
      });
    },
  });
};

export const useGetCardBanks = () => {
  return useQuery({
    queryKey: ["cardBanks"],
    queryFn: api.getCardBanks,
  });
};

export const useGetCardBankDetail = (id: string) => {
  return useQuery({
    queryKey: ["cardBank", id],
    queryFn: () => api.getCardBankDetail(id),
    enabled: !!id,
  });
};

export const useUpdateBank = (id: string) => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (variables: CreateBankType) =>
      api.updateCardBank(id, variables),
    onSuccess: () => {
      queryClient.refetchQueries({
        queryKey: ["cardBanks"],
      });
      queryClient.refetchQueries({
        queryKey: ["cardBank", id],
      });
    },
  });
};
