import { useMutation, useQuery } from "@tanstack/react-query";
import * as api from "../service/Service";
import { useQueryClient } from "@tanstack/react-query";
import { UpdateAddressType } from "../types/Types";

export const useGetAddress = (
  search: string,
  status: string,
  page: number = 1,
  limit: number = 10
) => {
  return useQuery({
    queryKey: ["address", search, status, page, limit],
    queryFn: () => api.getAddress(search, status, page, limit),
  });
};

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

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

export const useUpdateAddress = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({ id, params }: { id: string; params: UpdateAddressType }) =>
      api.updateAddress(id, params),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["address"] });
    },
  });
};

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