import { useMutation } from '@tanstack/react-query';

import { useApiClient } from '@/lib/apiClient';

interface CommercialRightsPurchaseParams {
  clipId: string;
}

export const useCommercialRightsPurchase = () => {
  const apiClient = useApiClient();

  return useMutation({
    mutationFn: async ({ clipId }: CommercialRightsPurchaseParams) => {
      const { data } = await apiClient.POST(
        '/api/billing/purchase-commercial-rights/',
        {
          body: {
            clip_id: clipId,
          },
        }
      );

      return data;
    },
    onSuccess: (data) => {
      if (data?.checkout_url) {
        window.location.href = data.checkout_url;
      }
    },
    onError: (error) => {
      console.error('Commercial rights purchase failed:', error);
    },
  });
};
