import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import * as api from "../servicce/Service";
import type { ShipmentResponse, CreateShipper } from "../types/Types";

export const useGetShipment = (page: number = 1) => {
  return useQuery<ShipmentResponse>({
    queryKey: ["shipment", page],
    queryFn: () => api.getShipment(page),
  });
};

export const useCreateShipment = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (data: CreateShipper) => api.createShipment(data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["shipment"] });
    },
  });
};

export const useDeleteShipment = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: api.deleteShipment,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["shipment"] });
    },
  });
};
