import { useEffect } from 'react';
import { loadVirtualTargets, loadVirtualSession } from '@frontend/utils/virtualTradingStorage';
import { useLiveStrategyMarkers } from '@frontend/hooks/useLiveStrategyMarkers';
import { useTradeNotification } from '../contexts/TradeNotificationContext';

/** 가상매매 대상 종목 STOMP 시그널 → 알림 Context */
export default function LiveSignalBridge() {
  const { addNotification, refreshHistory } = useTradeNotification();
  const targets = loadVirtualTargets();
  const session = loadVirtualSession();
  const markets = targets.map(t => t.market);

  useLiveStrategyMarkers({
    markets,
    activeMarket: markets[0] ?? 'KRW-BTC',
    enabled: session.running && markets.length > 0,
    onMarkersChange: () => {},
    onSignal: marker => {
      addNotification({
        market: marker.market,
        signalType: marker.signal,
        price: marker.price,
        candleTime: marker.time,
        candleType: marker.candleType ?? '1m',
      });
      window.setTimeout(() => { void refreshHistory(); }, 800);
    },
  });

  useEffect(() => {
    if (!session.running) return;
    const id = window.setInterval(() => { void refreshHistory(); }, 20000);
    return () => window.clearInterval(id);
  }, [session.running, refreshHistory]);

  return null;
}
