import { useMutation, useQuery } from "@tanstack/react-query";
import { AxiosError } from "axios";
import toast from "react-hot-toast";
import { paymentService } from "../services/payment.service";

export const useCreateCheckoutSession = () =>
  useMutation({
    mutationFn: (invoiceId: string) =>
      paymentService.createCheckoutSession(invoiceId),
    onSuccess: ({ checkoutUrl }) => {
      window.location.href = checkoutUrl;
    },
    onError: (err: AxiosError<{ error?: string }>) => {
      toast.error(err.response?.data?.error || "Payment initiation failed");
    },
  });

export const useClientTransactions = (params: { page: number; limit: number; status?: string }) =>
  useQuery({
    queryKey: ["client-transactions", params],
    queryFn: () => paymentService.getClientTransactions(params),
  });
