import * as api from "../service/PaymentMethodService";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import type {
  CreateRestaurantPaymentMethodType,
  GetRestaurantPaymentMethodsParams,
} from "../types/Types";

export const useGetRestaurantPaymentMethods = (
  params?: GetRestaurantPaymentMethodsParams
) => {
  return useQuery({
    queryKey: ["restaurant-payment-methods", params],
    queryFn: () => api.getRestaurantPaymentMethods(params),
  });
};

export const useCreateRestaurantPaymentMethod = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: api.createRestaurantPaymentMethod,
    onSuccess: () => {
      queryClient.invalidateQueries({
        queryKey: ["restaurant-payment-methods"],
      });
    },
  });
};

export const useDeleteRestaurantPaymentMethod = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: api.deleteRestaurantPaymentMethod,
    onSuccess: () => {
      queryClient.invalidateQueries({
        queryKey: ["restaurant-payment-methods"],
      });
    },
  });
};

export const useGetRestaurantPaymentMethod = (id: string) => {
  return useQuery({
    queryKey: ["restaurant-payment-method", id],
    queryFn: () => api.getRestaurantPaymentMethod(id),
  });
};

export const useUpdateRestaurantPaymentMethod = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({
      id,
      params,
    }: {
      id: string;
      params: CreateRestaurantPaymentMethodType;
    }) => api.updateRestaurantPaymentMethod(id, params),
    onSuccess: () => {
      queryClient.invalidateQueries({
        queryKey: ["restaurant-payment-methods"],
      });
    },
  });
};
