import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import * as api from "../service/ShipmentMethodService";
import type {
  CreateShipmentMethodType,
  GetRestaurantShipmentMethodsParams,
  GetRestaurantShipmentMethodsResponseType,
} from "../types/Types";

export const useGetRestaurantShipmentMethods = (
  params?: GetRestaurantShipmentMethodsParams
) => {
  return useQuery<GetRestaurantShipmentMethodsResponseType>({
    queryKey: ["restaurant-shipment-methods", params],
    queryFn: () => api.getRestaurantShipmentMethods(params),
  });
};

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

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

export const useGetRestaurantShipmentMethod = (id: string) => {
  return useQuery({
    queryKey: ["restaurant-shipment-method", id],
    queryFn: () => api.getRestaurantShipmentMethod(id),
    enabled: !!id,
  });
};

export const useUpdateRestaurantShipmentMethod = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({
      id,
      params,
    }: {
      id: string;
      params: CreateShipmentMethodType;
    }) => api.updateRestaurantShipmentMethod(id, params),
    onSuccess: () => {
      queryClient.invalidateQueries({
        queryKey: ["restaurant-shipment-methods"],
      });
    },
  });
};
