import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { QUERY_KEYS } from "@/utils/query-key";
import toast from "react-hot-toast";
import { getWalletStatus, updateWalletStatus, creditWallet } from "./_requests";
import { UpdateWalletStatusRequest } from "./_models";
import { WalletCreditRequest } from "./_credit-models";

export const useWalletStatus = () => {
  return useQuery({
    queryKey: [QUERY_KEYS.GET_WALLET_STATUS],
    queryFn: getWalletStatus,
  });
};

export const useUpdateWalletStatus = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (payload: UpdateWalletStatusRequest) => updateWalletStatus(payload),
    onSuccess: () => {
      queryClient.invalidateQueries({
        queryKey: [QUERY_KEYS.GET_WALLET_STATUS],
      });
      toast.success("وضعیت کیف پول با موفقیت به‌روزرسانی شد");
    },
    onError: (error: any) => {
      toast.error(error?.message || "خطا در به‌روزرسانی وضعیت کیف پول");
    },
  });
};

export const useWalletCredit = () => {
  return useMutation({
    mutationKey: [QUERY_KEYS.WALLET_CREDIT],
    mutationFn: (payload: WalletCreditRequest) => creditWallet(payload),
    onSuccess: (data) => {
      toast.success(data.message || "کیف پول با موفقیت شارژ شد");
    },
    onError: (error: any) => {
      console.error("Error crediting wallet:", error);
      toast.error(error?.message || "خطا در شارژ کیف پول");
    },
  });
};







