import { useEffect } from 'react';
import toast from 'react-hot-toast';
import { ticketsApi } from '@/lib/api';
import type { BookingStep } from '../_types';

/**
 * Watch for Lightning payment confirmation while on the paying step.
 * SSE gives instant updates; a 3s poll runs in parallel as a safety net so a
 * buffered/stuck stream (e.g. a proxy that doesn't flush SSE) can't strand the UI.
 */
export function useLightningWatcher(
  step: BookingStep,
  ticketId: string | undefined,
  locale: string,
  setPaymentPending: (value: boolean) => void,
  setStep: (value: BookingStep) => void
) {
  useEffect(() => {
    if (step !== 'paying' || !ticketId) return;

    let settled = false;
    let pollTimer: ReturnType<typeof setTimeout> | null = null;

    const confirmPaid = () => {
      if (settled) return;
      settled = true;
      toast.success(locale === 'es' ? '¡Pago confirmado!' : 'Payment confirmed!');
      setPaymentPending(false);
      setStep('success');
    };

    const expire = () => {
      if (settled) return;
      settled = true;
      toast.error(locale === 'es' ? 'La factura ha expirado' : 'Invoice has expired');
      setPaymentPending(false);
    };

    // Always same-origin so the streaming proxy route handler is used (it
    // bypasses the rewrite, which buffers SSE).
    const eventSource = new EventSource(`/api/lnbits/stream/${ticketId}`);

    eventSource.addEventListener('payment', (event) => {
      try {
        const data = JSON.parse((event as MessageEvent).data);
        if (data.type === 'paid' || data.type === 'already_paid') {
          confirmPaid();
        } else if (data.type === 'expired') {
          expire();
        }
      } catch (e) {
        console.error('Error parsing payment event:', e);
      }
    });

    eventSource.onerror = () => {
      // SSE failed or was closed; the poll below remains the source of truth.
      eventSource.close();
    };

    const poll = async () => {
      try {
        const status = await ticketsApi.checkPaymentStatus(ticketId);
        if (status.isPaid) {
          confirmPaid();
          return;
        }
      } catch (error) {
        console.error('Error checking payment status:', error);
      }
      if (!settled) {
        pollTimer = setTimeout(poll, 3000);
      }
    };
    pollTimer = setTimeout(poll, 3000);

    return () => {
      settled = true;
      eventSource.close();
      if (pollTimer) clearTimeout(pollTimer);
    };
  }, [step, ticketId, locale]);
}
