import React, { createContext, useContext, useState, ReactNode } from 'react';

interface NavigationState {
  symbol?: string;
  koreanName?: string;
  englishName?: string;
  timeInterval?: string;
  /** 알림 팝업 → 알림목록: 차트에 표시할 매수/매도 */
  alertSignal?: 'BUY' | 'SELL';
  /** 알림 발생 시각(ISO). candleTime 우선 전달 */
  alertMarkerTime?: string;
}

interface NavigationContextType {
  currentPage: string;
  navigationState: NavigationState | null;
  navigateToPage: (page: string, state?: NavigationState) => void;
}

const NavigationContext = createContext<NavigationContextType | undefined>(undefined);

export const NavigationProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
  const [currentPage, setCurrentPage] = useState('realtime-chart');
  const [navigationState, setNavigationState] = useState<NavigationState | null>(null);

  const navigateToPage = (page: string, state?: NavigationState) => {
    setCurrentPage(page);
    setNavigationState(state || null);
  };

  return (
    <NavigationContext.Provider value={{ currentPage, navigationState, navigateToPage }}>
      {children}
    </NavigationContext.Provider>
  );
};

export const useNavigation = () => {
  const context = useContext(NavigationContext);
  if (context === undefined) {
    throw new Error('useNavigation must be used within a NavigationProvider');
  }
  return context;
};
