/**
 * Market Data 공통 타입 정의
 * Market Data System (Python Broadcast Server)과 연동되는 타입
 */

export interface Candle {
  timestamp: number;
  open: number;
  high: number;
  low: number;
  close: number;
  volume: number;
  is_closed?: boolean;
  trade_count?: number;
}

/**
 * 보조지표 인터페이스 (16개 지표)
 * - 기존 6개: RSI, EMA, MACD, Bollinger Bands, Stochastic, ADX
 * - 추가 10개: CCI, OBV, 심리선, 신심리도, 투자심리도, Williams %R, BWI, Volume OSC, VR, Momentum, TRIX, Disparity
 */
export interface Indicators {
  // 기존 6개 보조지표
  rsi_14?: number;
  ema_9?: number;
  ema_21?: number;
  macd?: number;
  macd_signal?: number;
  macd_histogram?: number;
  bb_upper?: number;
  bb_middle?: number;
  bb_lower?: number;
  stoch_k?: number;
  stoch_d?: number;
  adx?: number;
  plus_di?: number;
  minus_di?: number;
  
  // 추가 10개 보조지표
  cci?: number;
  cci_signal?: number;
  obv?: number;
  obv_signal?: number;
  psychological_line?: number;
  new_psy?: number;
  invest_psy?: number;
  williams_r?: number;
  bwi?: number;
  volume_osc?: number;
  vr?: number;
  vr2?: number;
  momentum?: number;
  trix?: number;
  trix_signal?: number;
  disparity_5?: number;
  disparity_10?: number;
  disparity_20?: number;
  disparity_60?: number;
}

/**
 * 보조지표 메타데이터
 */
export interface IndicatorMetadata {
  key: keyof Indicators;
  name: string;
  description: string;
  category: 'trend' | 'momentum' | 'volatility' | 'volume' | 'psychological';
  defaultPeriod?: number;
  range?: { min: number; max: number };
}

/**
 * 보조지표 카테고리별 분류
 */
export const INDICATOR_CATEGORIES: Record<string, IndicatorMetadata[]> = {
  trend: [
    { key: 'ema_9', name: 'EMA(9)', description: '9일 지수이동평균', category: 'trend', defaultPeriod: 9 },
    { key: 'ema_21', name: 'EMA(21)', description: '21일 지수이동평균', category: 'trend', defaultPeriod: 21 },
    { key: 'bb_upper', name: '볼린저 상단', description: '볼린저밴드 상단선', category: 'trend', defaultPeriod: 20 },
    { key: 'bb_middle', name: '볼린저 중간', description: '볼린저밴드 중간선', category: 'trend', defaultPeriod: 20 },
    { key: 'bb_lower', name: '볼린저 하단', description: '볼린저밴드 하단선', category: 'trend', defaultPeriod: 20 },
  ],
  momentum: [
    { key: 'rsi_14', name: 'RSI(14)', description: '상대강도지수', category: 'momentum', defaultPeriod: 14, range: { min: 0, max: 100 } },
    { key: 'macd', name: 'MACD', description: 'MACD 라인', category: 'momentum' },
    { key: 'macd_signal', name: 'MACD Signal', description: 'MACD 신호선', category: 'momentum' },
    { key: 'macd_histogram', name: 'MACD Histogram', description: 'MACD 히스토그램', category: 'momentum' },
    { key: 'stoch_k', name: 'Stochastic %K', description: '스토캐스틱 %K', category: 'momentum', range: { min: 0, max: 100 } },
    { key: 'stoch_d', name: 'Stochastic %D', description: '스토캐스틱 %D', category: 'momentum', range: { min: 0, max: 100 } },
    { key: 'adx', name: 'ADX', description: '평균방향성지수', category: 'momentum', defaultPeriod: 14 },
    { key: 'plus_di', name: '+DI', description: '양의 방향성 지표', category: 'momentum' },
    { key: 'minus_di', name: '-DI', description: '음의 방향성 지표', category: 'momentum' },
    { key: 'cci', name: 'CCI', description: '상품채널지수', category: 'momentum', defaultPeriod: 13 },
    { key: 'cci_signal', name: 'CCI Signal', description: 'CCI 신호선', category: 'momentum' },
    { key: 'williams_r', name: 'Williams %R', description: '윌리엄스 %R', category: 'momentum', defaultPeriod: 14, range: { min: -100, max: 0 } },
    { key: 'momentum', name: 'Momentum', description: '모멘텀', category: 'momentum', defaultPeriod: 10 },
    { key: 'trix', name: 'TRIX', description: '삼중 지수이동평균', category: 'momentum', defaultPeriod: 14 },
    { key: 'trix_signal', name: 'TRIX Signal', description: 'TRIX 신호선', category: 'momentum' },
  ],
  volatility: [
    { key: 'bwi', name: 'BWI', description: '볼린저밴드 폭 지수', category: 'volatility', defaultPeriod: 20 },
    { key: 'disparity_5', name: '이격도(5)', description: '5일 이격도', category: 'volatility', defaultPeriod: 5 },
    { key: 'disparity_10', name: '이격도(10)', description: '10일 이격도', category: 'volatility', defaultPeriod: 10 },
    { key: 'disparity_20', name: '이격도(20)', description: '20일 이격도', category: 'volatility', defaultPeriod: 20 },
    { key: 'disparity_60', name: '이격도(60)', description: '60일 이격도', category: 'volatility', defaultPeriod: 60 },
  ],
  volume: [
    { key: 'obv', name: 'OBV', description: '거래량 균형', category: 'volume' },
    { key: 'obv_signal', name: 'OBV Signal', description: 'OBV 신호선', category: 'volume' },
    { key: 'volume_osc', name: 'Volume OSC', description: '거래량 오실레이터', category: 'volume' },
    { key: 'vr', name: 'VR', description: '거래량 비율', category: 'volume', defaultPeriod: 26 },
    { key: 'vr2', name: 'VR2', description: '거래량 비율 2', category: 'volume', defaultPeriod: 10 },
  ],
  psychological: [
    { key: 'psychological_line', name: '심리선', description: '심리선', category: 'psychological', defaultPeriod: 12, range: { min: 0, max: 100 } },
    { key: 'new_psy', name: '신심리도', description: '신심리도', category: 'psychological', defaultPeriod: 12, range: { min: 0, max: 100 } },
    { key: 'invest_psy', name: '투자심리도', description: '투자심리도', category: 'psychological', defaultPeriod: 12, range: { min: 0, max: 100 } },
  ],
};

/**
 * 모든 보조지표 목록
 */
export const ALL_INDICATORS: IndicatorMetadata[] = Object.values(INDICATOR_CATEGORIES).flat();

/**
 * 보조지표 키로 메타데이터 조회
 */
export const getIndicatorMetadata = (key: keyof Indicators): IndicatorMetadata | undefined => {
  return ALL_INDICATORS.find((indicator) => indicator.key === key);
};

/**
 * 보조지표 표시 이름 조회
 */
export const getIndicatorName = (key: keyof Indicators): string => {
  const metadata = getIndicatorMetadata(key);
  return metadata?.name || key;
};

/**
 * 보조지표 설명 조회
 */
export const getIndicatorDescription = (key: keyof Indicators): string => {
  const metadata = getIndicatorMetadata(key);
  return metadata?.description || '';
};
