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"] });
    },
  });
};

export const useGetShipmentById = (id: string) => {
  return useQuery({
    queryKey: ["shipment", id],
    queryFn: () => api.getShipmentById(id),
  });
};

export const useUpdateShipment = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({ id, params }: { id: string; params: CreateShipper }) =>
      api.updateShipment(id, params),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["shipment"] });
    },
  });
};

export const useRemoveShipmentFromAdmin = () => {
  return useMutation({
    mutationFn: api.removeShipmentFromAdmin,
  });
};

export const useAddShipmentToAdmin = () => {
  return useMutation({
    mutationFn: api.addShipmentToAdmin,
  });
};

export const useGetAdminShop = () => {
  return useQuery({
    queryKey: ["admin-shop"],
    queryFn: () => api.getAdminShop(),
  });
};
