import { useQuery, useMutation } from "@tanstack/react-query";
import { apiClient } from "@/api/client";
import type { BillingCadence } from "@/api/types";

export function useNetworkBilling(networkId: string | undefined) {
  return useQuery({
    queryKey: ["network-billing", networkId],
    queryFn: () => apiClient.getNetworkBilling(networkId!),
    enabled: !!networkId,
    // Refetch on window focus so the UI catches up after the user returns
    // from Stripe Checkout (webhook may land a second or two later).
    // FIX: doesn't work with electron
    refetchOnWindowFocus: true,
    refetchInterval: 10000
  });
}

export function useCreateCheckoutSession(networkId: string) {
  return useMutation({
    mutationFn: (cadence: BillingCadence) =>
      apiClient.createCheckoutSession(networkId, cadence),
  });
}

export function useCreatePortalSession(networkId: string) {
  return useMutation({
    mutationFn: () => apiClient.createPortalSession(networkId),
  });
}
