import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { CollectionRepository } from "../repositories/ColllectionRepository";
import showToast from "../services/toastService";

export const useCollections = (
  pageSize,
  pageNumber,
  fromDate,
  toDate,
  isPending,
  isActive,
  projectId,
  searchString
) => {
  return useQuery({
    queryKey: [
      "collections",
      pageSize,
      pageNumber,
      fromDate,
      toDate,
      isPending,
      isActive,
      projectId,
      searchString,
    ],

    queryFn: async () => {
      const response = await CollectionRepository.getCollections(
        pageSize,
        pageNumber,
        fromDate,
        toDate,
        isPending,
        isActive,
        projectId,
        searchString
      );
      return response.data;
    },

    keepPreviousData: true,
    staleTime: 1000 * 60 * 1,
  });
};


export const useCollection =(collectionId)=>{
  return useQuery({
    queryKey:["collection",collectionId],
    queryFn:async()=> {
      const resp = await CollectionRepository.getCollection(collectionId);
      return resp.data
    },
    enabled:!!collectionId
  })
}


// =========================Mutation======================

export const useCreateCollection = (onSuccessCallBack) => {
  const clinent = useQueryClient();
  return useMutation({
    mutationFn: async (payload) =>
      await CollectionRepository.createNewCollection(payload),
    onSuccess: (_, variables) => {
      showToast("New Collection created Successfully", "success");
      clinent.invalidateQueries({ queryKey: ["collections"] });
      if (onSuccessCallBack) onSuccessCallBack();
    },
    onError: (error) => {
      showToast(
        error.response.data.message || error.message || "Something Went wrong"
      );
    },
  });
};

export const useMarkedPaymentReceived = (onSuccessCallBack) => {
  const client = useQueryClient();
  return useMutation({
    mutationFn: async (payload) =>
      await CollectionRepository.markPaymentReceived(payload),
    onSuccess: async () => {
      client.invalidateQueries({ queryKey: ["collections"] });
      showToast("Payment Received marked Successfully", "success");
      if (onSuccessCallBack) onSuccessCallBack();
    },
    onError: (error) => {
      showToast(
        error.response.data.message || error.message || "Something Went wrong"
      );
    },
  });
};

export const useAddPayment = (onSuccessCallBack) => {
  const client = useQueryClient();

  return useMutation({
    mutationFn: (payload) => CollectionRepository.makeReceivePayment(payload),
    onSuccess: () => {
      client.invalidateQueries({ queryKey: ["collections"] });
      client.invalidateQueries({ queryKey: ["collection"] });
      showToast("Payment Received marked Successfully", "success");
      if (onSuccessCallBack) onSuccessCallBack();
    },
    onError: (error) => {
      showToast(
        error?.response?.data?.message || error.message || "Something Went wrong"
      );
    },
  });
};

export const useAddComment = (onSuccessCallBack) => {
  const client = useQueryClient();

  return useMutation({
    mutationFn: (payload) => CollectionRepository.addComment(payload),
    onSuccess: () => {
      client.invalidateQueries({ queryKey: ["collections"] });
      client.invalidateQueries({ queryKey: ["collection"] });
      showToast("Comment Successfully", "success");
      if (onSuccessCallBack) onSuccessCallBack();
    },
    onError: (error) => {
      showToast(
        error?.response?.data?.message || error.message || "Something Went wrong"
      );
    },
  });
};

export const useUpdateCollection =(onSuccessCallBack)=>{
   const client = useQueryClient();
  return useMutation({
    mutationFn:async({collectionId,payload})=> await CollectionRepository.updateCollection(collectionId,payload),
     onSuccess: () => {
      client.invalidateQueries({ queryKey: ["collections"] });
      client.invalidateQueries({ queryKey: ["collection"] });
      showToast("Collection Updated Successfully", "success");
      if (onSuccessCallBack) onSuccessCallBack();
    },
    onError: (error) => {
      showToast(
        error?.response?.data?.message || error.message || "Something Went wrong"
      );
    },
  })
}