import { API_BASE_URL } from './apiConfig';

/**
 * 패턴 검색 요청 타입
 */
export interface PatternSearchRequest {
  patternData?: number[];
  patternType?: string;
  market?: string;
  timeframe?: string;
  minSimilarity?: number;
  maxResults?: number;
  patternLength?: number;
  mirrorSearch?: boolean;
}

/**
 * 패턴 매칭 결과 타입
 */
export interface PatternMatchResult {
  symbol: string;
  name: string;
  similarity: number;
  startTime: string;
  endTime: string;
  chartData: number[];
  timestamps: string[];
  currentPrice: number;
  expectedReturn: number;
  expectedPeriod: string;
  successRate: number;
  direction: 'BULLISH' | 'BEARISH' | 'NEUTRAL';
  isMirrored: boolean;
  dtwDistance: number;
}

/**
 * 캔들 데이터 타입 (캔들 패턴용)
 */
export interface CandlePatternData {
  open: number;
  high: number;
  low: number;
  close: number;
}

/**
 * 패턴 템플릿 타입
 */
export interface PatternTemplate {
  id: string;
  name: string;
  nameEn: string;
  category: 'REVERSAL' | 'CONTINUATION' | 'CANDLE';
  type: string;
  data: number[];
  direction: 'BULLISH' | 'BEARISH' | 'NEUTRAL';
  description: string;
  avgReturn: number;
  avgPeriod: number;
  successRate: number;
  frequency: 'HIGH' | 'MEDIUM' | 'LOW';
  svgPath: string;
  candleData?: CandlePatternData[]; // 캔들 패턴용
}

/**
 * 패턴 검색 응답 타입
 */
export interface PatternSearchResponse {
  success: boolean;
  count: number;
  results: PatternMatchResult[];
  error?: string;
}

/**
 * 패턴 템플릿 응답 타입
 */
export interface PatternTemplatesResponse {
  success: boolean;
  count: number;
  templates: PatternTemplate[];
  error?: string;
}

/**
 * 패턴 통계 응답 타입
 */
export interface PatternStatisticsResponse {
  success: boolean;
  statistics: PatternTemplate;
  error?: string;
}

/**
 * 패턴 검색 API 호출
 */
export const searchPattern = async (request: PatternSearchRequest): Promise<PatternSearchResponse> => {
  try {
    const response = await fetch(`${API_BASE_URL}/pattern/search`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(request),
    });
    
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    
    return await response.json();
  } catch (error) {
    console.error('패턴 검색 API 호출 실패:', error);
    return {
      success: false,
      count: 0,
      results: [],
      error: error instanceof Error ? error.message : '알 수 없는 오류',
    };
  }
};

/**
 * 패턴 템플릿 목록 조회
 */
export const getPatternTemplates = async (category?: string): Promise<PatternTemplatesResponse> => {
  try {
    const url = category 
      ? `${API_BASE_URL}/pattern/templates?category=${category}`
      : `${API_BASE_URL}/pattern/templates`;
    
    const response = await fetch(url, {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
      },
    });
    
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    
    return await response.json();
  } catch (error) {
    console.error('패턴 템플릿 조회 실패:', error);
    return {
      success: false,
      count: 0,
      templates: [],
      error: error instanceof Error ? error.message : '알 수 없는 오류',
    };
  }
};

/**
 * 패턴별 통계 조회
 */
export const getPatternStatistics = async (patternType: string): Promise<PatternStatisticsResponse> => {
  try {
    const response = await fetch(`${API_BASE_URL}/pattern/statistics/${patternType}`, {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
      },
    });
    
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    
    return await response.json();
  } catch (error) {
    console.error('패턴 통계 조회 실패:', error);
    return {
      success: false,
      statistics: {} as PatternTemplate,
      error: error instanceof Error ? error.message : '알 수 없는 오류',
    };
  }
};

/**
 * 패턴 데이터 정규화
 */
export const normalizePattern = async (data: number[]): Promise<{ success: boolean; original: number[]; normalized: number[]; error?: string }> => {
  try {
    const response = await fetch(`${API_BASE_URL}/pattern/normalize`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(data),
    });
    
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    
    return await response.json();
  } catch (error) {
    console.error('패턴 정규화 실패:', error);
    return {
      success: false,
      original: data,
      normalized: [],
      error: error instanceof Error ? error.message : '알 수 없는 오류',
    };
  }
};

/**
 * DTW 거리 계산
 */
export const calculateDTW = async (series1: number[], series2: number[]): Promise<{ success: boolean; distance: number; similarity: number; error?: string }> => {
  try {
    const response = await fetch(`${API_BASE_URL}/pattern/dtw`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ series1, series2 }),
    });
    
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    
    return await response.json();
  } catch (error) {
    console.error('DTW 계산 실패:', error);
    return {
      success: false,
      distance: 0,
      similarity: 0,
      error: error instanceof Error ? error.message : '알 수 없는 오류',
    };
  }
};

/**
 * 캔들 데이터 타입
 */
export interface CandleDataForChart {
  time: string;
  open: number;
  high: number;
  low: number;
  close: number;
  volume?: number;
}

/**
 * 업비트에서 캔들 데이터 조회
 */
export const fetchCandleData = async (
  market: string,
  timeframe: string,
  count: number = 200
): Promise<{ success: boolean; data: CandleDataForChart[]; error?: string }> => {
  try {
    const response = await fetch(`${API_BASE_URL}/pattern/candles?market=${market}&timeframe=${timeframe}&count=${count}`, {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
      },
    });
    
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    
    return await response.json();
  } catch (error) {
    console.error('캔들 데이터 조회 실패:', error);
    return {
      success: false,
      data: [],
      error: error instanceof Error ? error.message : '알 수 없는 오류',
    };
  }
};

/**
 * 카테고리별 한글 이름
 */
export const getCategoryName = (category: string): string => {
  const names: Record<string, string> = {
    REVERSAL: '반전 패턴',
    CONTINUATION: '지속 패턴',
    CANDLE: '캔들 패턴',
  };
  return names[category] || category;
};

/**
 * 방향별 한글 이름
 */
export const getDirectionName = (direction: string): string => {
  const names: Record<string, string> = {
    BULLISH: '상승',
    BEARISH: '하락',
    NEUTRAL: '중립',
  };
  return names[direction] || direction;
};

/**
 * 빈도별 한글 이름
 */
export const getFrequencyName = (frequency: string): string => {
  const names: Record<string, string> = {
    HIGH: '높음',
    MEDIUM: '중간',
    LOW: '낮음',
  };
  return names[frequency] || frequency;
};

/**
 * 기본 패턴 템플릿 (백엔드 연결 실패 시 사용)
 */
export const DEFAULT_PATTERN_TEMPLATES: PatternTemplate[] = [
  // ============ 상승 패턴 (BULLISH) ============
  {
    id: 'UPTREND_CONTINUE',
    name: '상승지속',
    nameEn: 'Uptrend Continuation',
    category: 'CONTINUATION',
    type: 'UPTREND_CONTINUE',
    data: [0.0, 0.15, 0.25, 0.35, 0.5, 0.6, 0.75, 0.85, 1.0],
    direction: 'BULLISH',
    description: '꾸준히 상승하는 추세 지속 패턴',
    avgReturn: 8.5,
    avgPeriod: 5.0,
    successRate: 65.0,
    frequency: 'HIGH',
    svgPath: 'M5 35 L95 5',
  },
  {
    id: 'DOUBLE_BOTTOM',
    name: '이중바닥형',
    nameEn: 'Double Bottom',
    category: 'REVERSAL',
    type: 'DOUBLE_BOTTOM',
    data: [0.8, 0.5, 0.1, 0.0, 0.3, 0.5, 0.2, 0.0, 0.4, 0.7, 1.0],
    direction: 'BULLISH',
    description: 'W자형 바닥을 형성 후 상승하는 반전 패턴',
    avgReturn: 12.5,
    avgPeriod: 6.0,
    successRate: 72.0,
    frequency: 'HIGH',
    svgPath: 'M5 10 L25 35 L45 15 L65 35 L95 5',
  },
  {
    id: 'CORRECTION_UP',
    name: '조정후상승',
    nameEn: 'Correction then Rise',
    category: 'CONTINUATION',
    type: 'CORRECTION_UP',
    data: [0.7, 0.5, 0.3, 0.2, 0.15, 0.25, 0.4, 0.6, 0.8, 1.0],
    direction: 'BULLISH',
    description: '일시적 하락 조정 후 상승하는 패턴',
    avgReturn: 9.0,
    avgPeriod: 4.0,
    successRate: 68.0,
    frequency: 'HIGH',
    svgPath: 'M5 10 L35 30 L95 5',
  },
  {
    id: 'FALL_THEN_RISE',
    name: '하락후상승',
    nameEn: 'Fall then Rise',
    category: 'REVERSAL',
    type: 'FALL_THEN_RISE',
    data: [1.0, 0.8, 0.5, 0.2, 0.0, 0.2, 0.5, 0.75, 0.9],
    direction: 'BULLISH',
    description: 'V자형 반등 패턴',
    avgReturn: 11.0,
    avgPeriod: 3.5,
    successRate: 70.0,
    frequency: 'HIGH',
    svgPath: 'M5 5 L50 35 L95 10',
  },
  {
    id: 'CORRECTION_RERISE',
    name: '조정후재상승',
    nameEn: 'Correction then Rerise',
    category: 'CONTINUATION',
    type: 'CORRECTION_RERISE',
    data: [0.3, 0.5, 0.7, 0.5, 0.35, 0.3, 0.4, 0.6, 0.8, 1.0],
    direction: 'BULLISH',
    description: '상승 후 조정을 거쳐 재상승하는 패턴',
    avgReturn: 10.0,
    avgPeriod: 5.0,
    successRate: 66.0,
    frequency: 'MEDIUM',
    svgPath: 'M5 30 L30 10 L50 25 L95 5',
  },
  {
    id: 'SIDEWAYS_BREAKOUT_UP',
    name: '횡보후상승',
    nameEn: 'Sideways then Breakout Up',
    category: 'CONTINUATION',
    type: 'SIDEWAYS_BREAKOUT_UP',
    data: [0.3, 0.35, 0.25, 0.35, 0.3, 0.35, 0.4, 0.6, 0.8, 1.0],
    direction: 'BULLISH',
    description: '박스권 횡보 후 상승 돌파하는 패턴',
    avgReturn: 9.5,
    avgPeriod: 4.5,
    successRate: 64.0,
    frequency: 'HIGH',
    svgPath: 'M5 20 L20 22 L35 18 L50 20 L65 22 L80 10 L95 5',
  },
  {
    id: 'BOUNCE_ATTEMPT',
    name: '반등시도',
    nameEn: 'Bounce Attempt',
    category: 'REVERSAL',
    type: 'BOUNCE_ATTEMPT',
    data: [0.8, 0.6, 0.4, 0.2, 0.0, 0.15, 0.3, 0.5, 0.7],
    direction: 'BULLISH',
    description: '하락 후 반등을 시도하는 패턴',
    avgReturn: 7.5,
    avgPeriod: 3.0,
    successRate: 58.0,
    frequency: 'MEDIUM',
    svgPath: 'M5 5 L40 35 L95 15',
  },
  {
    id: 'V_BOTTOM',
    name: 'V바닥형',
    nameEn: 'V Bottom',
    category: 'REVERSAL',
    type: 'V_BOTTOM',
    data: [0.9, 0.7, 0.4, 0.1, 0.0, 0.15, 0.4, 0.7, 0.95],
    direction: 'BULLISH',
    description: '급격한 V자형 바닥 반전 패턴',
    avgReturn: 13.0,
    avgPeriod: 2.5,
    successRate: 65.0,
    frequency: 'MEDIUM',
    svgPath: 'M5 5 L50 35 L95 5',
  },
  {
    id: 'INVERSE_HEAD_SHOULDERS',
    name: '역머리어깨형',
    nameEn: 'Inverse Head and Shoulders',
    category: 'REVERSAL',
    type: 'INVERSE_HEAD_SHOULDERS',
    data: [0.6, 0.4, 0.2, 0.35, 0.1, 0.0, 0.1, 0.35, 0.2, 0.5, 0.8, 1.0],
    direction: 'BULLISH',
    description: '역헤드앤숄더 강력한 상승 반전 패턴',
    avgReturn: 14.0,
    avgPeriod: 6.0,
    successRate: 74.0,
    frequency: 'LOW',
    svgPath: 'M5 15 L20 28 L35 18 L50 35 L65 18 L80 28 L95 5',
  },
  {
    id: 'INVERSE_DOUBLE_TOP',
    name: '역쌍봉형',
    nameEn: 'Inverse Double Peak',
    category: 'REVERSAL',
    type: 'INVERSE_DOUBLE_TOP',
    data: [0.7, 0.5, 0.2, 0.0, 0.2, 0.4, 0.15, 0.0, 0.25, 0.5, 0.8, 1.0],
    direction: 'BULLISH',
    description: 'M자를 뒤집은 형태의 상승 반전 패턴',
    avgReturn: 11.5,
    avgPeriod: 5.0,
    successRate: 69.0,
    frequency: 'MEDIUM',
    svgPath: 'M5 10 L25 30 L40 18 L55 32 L95 5',
  },
  {
    id: 'ASCENDING_TRIANGLE',
    name: '강세이등삼각',
    nameEn: 'Ascending Triangle',
    category: 'CONTINUATION',
    type: 'ASCENDING_TRIANGLE',
    data: [0.2, 0.5, 0.3, 0.5, 0.35, 0.5, 0.4, 0.5, 0.45, 0.7, 1.0],
    direction: 'BULLISH',
    description: '저점이 높아지며 저항선 돌파하는 패턴',
    avgReturn: 10.0,
    avgPeriod: 4.0,
    successRate: 70.0,
    frequency: 'MEDIUM',
    svgPath: 'M5 30 L20 12 L35 25 L50 12 L65 20 L80 12 L95 5',
  },
  {
    id: 'RISING_FLAG',
    name: '상승사각깃발',
    nameEn: 'Rising Flag',
    category: 'CONTINUATION',
    type: 'RISING_FLAG',
    data: [0.0, 0.3, 0.5, 0.45, 0.55, 0.5, 0.6, 0.55, 0.65, 0.8, 1.0],
    direction: 'BULLISH',
    description: '상승 후 사각형 조정 후 재상승 패턴',
    avgReturn: 8.5,
    avgPeriod: 3.0,
    successRate: 66.0,
    frequency: 'HIGH',
    svgPath: 'M5 35 L25 15 L40 20 L55 15 L70 20 L95 5',
  },
  {
    id: 'RISING_RECTANGLE',
    name: '상승직사각형',
    nameEn: 'Rising Rectangle',
    category: 'CONTINUATION',
    type: 'RISING_RECTANGLE',
    data: [0.1, 0.35, 0.25, 0.35, 0.25, 0.35, 0.25, 0.5, 0.75, 1.0],
    direction: 'BULLISH',
    description: '박스권 형성 후 상승 돌파 패턴',
    avgReturn: 9.0,
    avgPeriod: 4.0,
    successRate: 64.0,
    frequency: 'MEDIUM',
    svgPath: 'M5 28 L20 18 L35 28 L50 18 L65 28 L80 15 L95 5',
  },
  {
    id: 'SIDEWAYS_RERISE',
    name: '횡보후재상승',
    nameEn: 'Sideways then Rerise',
    category: 'CONTINUATION',
    type: 'SIDEWAYS_RERISE',
    data: [0.4, 0.5, 0.45, 0.5, 0.45, 0.5, 0.55, 0.7, 0.85, 1.0],
    direction: 'BULLISH',
    description: '횡보 구간 후 재상승하는 패턴',
    avgReturn: 8.0,
    avgPeriod: 4.0,
    successRate: 62.0,
    frequency: 'HIGH',
    svgPath: 'M5 20 L25 18 L45 22 L65 18 L85 10 L95 5',
  },
  
  // ============ 하락 패턴 (BEARISH) ============
  {
    id: 'DOWNTREND_CONTINUE',
    name: '하락지속',
    nameEn: 'Downtrend Continuation',
    category: 'CONTINUATION',
    type: 'DOWNTREND_CONTINUE',
    data: [1.0, 0.85, 0.75, 0.65, 0.5, 0.4, 0.25, 0.15, 0.0],
    direction: 'BEARISH',
    description: '꾸준히 하락하는 추세 지속 패턴',
    avgReturn: -8.5,
    avgPeriod: 5.0,
    successRate: 65.0,
    frequency: 'HIGH',
    svgPath: 'M5 5 L95 35',
  },
  {
    id: 'DOUBLE_TOP',
    name: '이중천정형',
    nameEn: 'Double Top',
    category: 'REVERSAL',
    type: 'DOUBLE_TOP',
    data: [0.2, 0.5, 0.9, 1.0, 0.7, 0.5, 0.8, 1.0, 0.6, 0.3, 0.0],
    direction: 'BEARISH',
    description: 'M자형 천정을 형성 후 하락하는 반전 패턴',
    avgReturn: -12.5,
    avgPeriod: 6.0,
    successRate: 72.0,
    frequency: 'HIGH',
    svgPath: 'M5 30 L25 5 L45 25 L65 5 L95 35',
  },
  {
    id: 'V_TOP',
    name: 'V천정형',
    nameEn: 'V Top',
    category: 'REVERSAL',
    type: 'V_TOP',
    data: [0.1, 0.3, 0.6, 0.9, 1.0, 0.85, 0.6, 0.3, 0.05],
    direction: 'BEARISH',
    description: '급격한 역V자형 천정 반전 패턴',
    avgReturn: -13.0,
    avgPeriod: 2.5,
    successRate: 65.0,
    frequency: 'MEDIUM',
    svgPath: 'M5 35 L50 5 L95 35',
  },
  {
    id: 'BOUNCE_FAILURE',
    name: '반등실패',
    nameEn: 'Bounce Failure',
    category: 'CONTINUATION',
    type: 'BOUNCE_FAILURE',
    data: [0.2, 0.4, 0.6, 0.8, 1.0, 0.85, 0.7, 0.5, 0.3, 0.1, 0.0],
    direction: 'BEARISH',
    description: '반등 시도 후 다시 하락하는 패턴',
    avgReturn: -9.0,
    avgPeriod: 4.0,
    successRate: 62.0,
    frequency: 'MEDIUM',
    svgPath: 'M5 35 L40 5 L95 30',
  },
  {
    id: 'SIDEWAYS_BREAKOUT_DOWN',
    name: '횡보후하락',
    nameEn: 'Sideways then Breakout Down',
    category: 'CONTINUATION',
    type: 'SIDEWAYS_BREAKOUT_DOWN',
    data: [0.7, 0.65, 0.75, 0.65, 0.7, 0.65, 0.6, 0.4, 0.2, 0.0],
    direction: 'BEARISH',
    description: '박스권 횡보 후 하락 돌파하는 패턴',
    avgReturn: -9.5,
    avgPeriod: 4.5,
    successRate: 64.0,
    frequency: 'HIGH',
    svgPath: 'M5 20 L20 18 L35 22 L50 20 L65 18 L80 30 L95 35',
  },
  {
    id: 'HEAD_AND_SHOULDERS',
    name: '머리어깨형',
    nameEn: 'Head and Shoulders',
    category: 'REVERSAL',
    type: 'HEAD_AND_SHOULDERS',
    data: [0.4, 0.6, 0.8, 0.65, 0.9, 1.0, 0.9, 0.65, 0.8, 0.5, 0.2, 0.0],
    direction: 'BEARISH',
    description: '헤드앤숄더 강력한 하락 반전 패턴',
    avgReturn: -14.0,
    avgPeriod: 6.0,
    successRate: 74.0,
    frequency: 'LOW',
    svgPath: 'M5 25 L20 12 L35 22 L50 5 L65 22 L80 12 L95 35',
  },
  {
    id: 'DOUBLE_PEAK',
    name: '쌍봉형',
    nameEn: 'Double Peak',
    category: 'REVERSAL',
    type: 'DOUBLE_PEAK',
    data: [0.3, 0.5, 0.8, 1.0, 0.8, 0.6, 0.85, 1.0, 0.75, 0.5, 0.2, 0.0],
    direction: 'BEARISH',
    description: '두 개의 고점 형성 후 하락하는 패턴',
    avgReturn: -11.5,
    avgPeriod: 5.0,
    successRate: 69.0,
    frequency: 'MEDIUM',
    svgPath: 'M5 30 L25 8 L40 22 L55 6 L95 35',
  },
  {
    id: 'DESCENDING_TRIANGLE',
    name: '약세이등삼각',
    nameEn: 'Descending Triangle',
    category: 'CONTINUATION',
    type: 'DESCENDING_TRIANGLE',
    data: [0.8, 0.5, 0.7, 0.5, 0.65, 0.5, 0.6, 0.5, 0.55, 0.3, 0.0],
    direction: 'BEARISH',
    description: '고점이 낮아지며 지지선 붕괴하는 패턴',
    avgReturn: -10.0,
    avgPeriod: 4.0,
    successRate: 70.0,
    frequency: 'MEDIUM',
    svgPath: 'M5 10 L20 28 L35 15 L50 28 L65 20 L80 28 L95 35',
  },
  {
    id: 'EXPANDING_TRIANGLE',
    name: '확장삼각형',
    nameEn: 'Expanding Triangle',
    category: 'REVERSAL',
    type: 'EXPANDING_TRIANGLE',
    data: [0.5, 0.6, 0.4, 0.7, 0.3, 0.8, 0.2, 0.9, 0.1, 0.0],
    direction: 'BEARISH',
    description: '변동폭이 확대되며 방향을 결정하는 패턴',
    avgReturn: -8.0,
    avgPeriod: 5.0,
    successRate: 55.0,
    frequency: 'LOW',
    svgPath: 'M5 20 L20 15 L35 25 L50 10 L65 30 L80 5 L95 35',
  },
  {
    id: 'FALLING_RECTANGLE',
    name: '하락직사각형',
    nameEn: 'Falling Rectangle',
    category: 'CONTINUATION',
    type: 'FALLING_RECTANGLE',
    data: [0.9, 0.65, 0.75, 0.65, 0.75, 0.65, 0.75, 0.5, 0.25, 0.0],
    direction: 'BEARISH',
    description: '박스권 형성 후 하락 돌파 패턴',
    avgReturn: -9.0,
    avgPeriod: 4.0,
    successRate: 64.0,
    frequency: 'MEDIUM',
    svgPath: 'M5 12 L20 22 L35 12 L50 22 L65 12 L80 25 L95 35',
  },
  
  // ============ 캔들스틱 패턴 (CANDLE) ============
  // 상승 반전 캔들 패턴
  {
    id: 'THREE_STARS_SOUTH',
    name: '남쪽삼성형',
    nameEn: 'Three Stars in the South',
    category: 'CANDLE',
    type: 'THREE_STARS_SOUTH',
    data: [0.3, 0.4, 0.5, 0.6, 0.75, 0.9],
    direction: 'BULLISH',
    description: '하락 추세 끝에서 나타나는 세 개의 작아지는 음봉, 강력한 상승 반전 신호',
    avgReturn: 12.0,
    avgPeriod: 3.0,
    successRate: 86.0,
    frequency: 'LOW',
    svgPath: 'M15 35 L15 10 M35 30 L35 15 M55 27 L55 18',
    candleData: [
      { open: 0.8, high: 0.85, low: 0.2, close: 0.3 },
      { open: 0.6, high: 0.65, low: 0.3, close: 0.4 },
      { open: 0.5, high: 0.55, low: 0.4, close: 0.45 },
    ],
  },
  {
    id: 'THREE_LINE_STRIKE_BULLISH',
    name: '삼선반격형(상승)',
    nameEn: 'Three Line Strike Bullish',
    category: 'CANDLE',
    type: 'THREE_LINE_STRIKE_BULLISH',
    data: [0.2, 0.35, 0.5, 0.65, 0.85],
    direction: 'BULLISH',
    description: '세 개의 음봉 후 큰 양봉이 나타나 모두 감싸는 패턴',
    avgReturn: 11.5,
    avgPeriod: 2.5,
    successRate: 84.0,
    frequency: 'LOW',
    svgPath: 'M15 28 L15 35 M30 22 L30 30 M45 16 L45 25 M70 35 L70 8',
    candleData: [
      { open: 0.7, high: 0.75, low: 0.5, close: 0.55 },
      { open: 0.55, high: 0.6, low: 0.35, close: 0.4 },
      { open: 0.4, high: 0.45, low: 0.2, close: 0.25 },
      { open: 0.2, high: 0.85, low: 0.15, close: 0.8 },
    ],
  },
  {
    id: 'THREE_WHITE_SOLDIERS',
    name: '적삼병',
    nameEn: 'Three White Soldiers',
    category: 'CANDLE',
    type: 'THREE_WHITE_SOLDIERS',
    data: [0.2, 0.4, 0.6, 0.75, 0.9],
    direction: 'BULLISH',
    description: '연속으로 상승하는 세 개의 양봉, 강력한 상승 신호',
    avgReturn: 10.5,
    avgPeriod: 3.0,
    successRate: 82.0,
    frequency: 'MEDIUM',
    svgPath: 'M20 35 L20 25 M40 28 L40 18 M60 22 L60 10',
    candleData: [
      { open: 0.2, high: 0.45, low: 0.15, close: 0.4 },
      { open: 0.4, high: 0.65, low: 0.35, close: 0.6 },
      { open: 0.6, high: 0.9, low: 0.55, close: 0.85 },
    ],
  },
  {
    id: 'MORNING_STAR',
    name: '샛별형',
    nameEn: 'Morning Star',
    category: 'CANDLE',
    type: 'MORNING_STAR',
    data: [0.3, 0.2, 0.15, 0.3, 0.7],
    direction: 'BULLISH',
    description: '하락 후 작은 몸통, 그리고 큰 양봉으로 상승 반전',
    avgReturn: 9.5,
    avgPeriod: 2.5,
    successRate: 78.0,
    frequency: 'MEDIUM',
    svgPath: 'M20 15 L20 32 M45 28 L45 25 M70 35 L70 12',
    candleData: [
      { open: 0.7, high: 0.75, low: 0.25, close: 0.3 },
      { open: 0.25, high: 0.3, low: 0.15, close: 0.2 },
      { open: 0.25, high: 0.8, low: 0.2, close: 0.75 },
    ],
  },
  {
    id: 'MORNING_DOJI_STAR',
    name: '샛별십자형',
    nameEn: 'Morning Doji Star',
    category: 'CANDLE',
    type: 'MORNING_DOJI_STAR',
    data: [0.3, 0.15, 0.15, 0.3, 0.7],
    direction: 'BULLISH',
    description: '하락 후 도지, 그리고 큰 양봉으로 상승 반전 (샛별형보다 강력)',
    avgReturn: 9.0,
    avgPeriod: 2.5,
    successRate: 76.0,
    frequency: 'MEDIUM',
    svgPath: 'M20 15 L20 32 M45 20 L45 30 M70 35 L70 12',
    candleData: [
      { open: 0.7, high: 0.75, low: 0.25, close: 0.3 },
      { open: 0.2, high: 0.25, low: 0.1, close: 0.2 },
      { open: 0.25, high: 0.8, low: 0.2, close: 0.75 },
    ],
  },
  {
    id: 'THREE_OUTSIDE_UP',
    name: '상승장악확인형',
    nameEn: 'Three Outside Up',
    category: 'CANDLE',
    type: 'THREE_OUTSIDE_UP',
    data: [0.35, 0.25, 0.5, 0.75],
    direction: 'BULLISH',
    description: '상승장악형 후 양봉으로 확인되는 강력한 상승 신호',
    avgReturn: 8.5,
    avgPeriod: 2.0,
    successRate: 75.0,
    frequency: 'MEDIUM',
    svgPath: 'M20 22 L20 30 M45 35 L45 15 M70 18 L70 8',
    candleData: [
      { open: 0.5, high: 0.55, low: 0.25, close: 0.3 },
      { open: 0.25, high: 0.65, low: 0.2, close: 0.6 },
      { open: 0.6, high: 0.8, low: 0.55, close: 0.75 },
    ],
  },
  {
    id: 'BULLISH_ENGULFING',
    name: '상승장악형',
    nameEn: 'Bullish Engulfing',
    category: 'CANDLE',
    type: 'BULLISH_ENGULFING',
    data: [0.4, 0.25, 0.6, 0.8],
    direction: 'BULLISH',
    description: '음봉을 완전히 감싸는 양봉, 상승 반전 신호',
    avgReturn: 7.5,
    avgPeriod: 2.0,
    successRate: 72.0,
    frequency: 'HIGH',
    svgPath: 'M30 20 L30 30 M60 35 L60 10',
    candleData: [
      { open: 0.6, high: 0.65, low: 0.3, close: 0.35 },
      { open: 0.3, high: 0.85, low: 0.25, close: 0.8 },
    ],
  },
  {
    id: 'HAMMER',
    name: '망치형',
    nameEn: 'Hammer',
    category: 'CANDLE',
    type: 'HAMMER',
    data: [0.3, 0.0, 0.35, 0.4],
    direction: 'BULLISH',
    description: '긴 아래꼬리와 작은 몸통을 가진 상승 반전 캔들',
    avgReturn: 6.5,
    avgPeriod: 1.5,
    successRate: 70.0,
    frequency: 'HIGH',
    svgPath: 'M50 8 L50 35',
    candleData: [
      { open: 0.65, high: 0.75, low: 0.1, close: 0.7 },
    ],
  },
  {
    id: 'INVERTED_HAMMER',
    name: '역망치형',
    nameEn: 'Inverted Hammer',
    category: 'CANDLE',
    type: 'INVERTED_HAMMER',
    data: [0.6, 1.0, 0.65, 0.55],
    direction: 'BULLISH',
    description: '긴 위꼬리와 작은 몸통을 가진 상승 반전 캔들',
    avgReturn: 5.5,
    avgPeriod: 1.5,
    successRate: 65.0,
    frequency: 'MEDIUM',
    svgPath: 'M50 5 L50 32',
    candleData: [
      { open: 0.3, high: 0.85, low: 0.2, close: 0.35 },
    ],
  },
  {
    id: 'DRAGONFLY_DOJI',
    name: '잠자리형',
    nameEn: 'Dragonfly Doji',
    category: 'CANDLE',
    type: 'DRAGONFLY_DOJI',
    data: [0.5, 0.0, 0.5, 0.5],
    direction: 'BULLISH',
    description: '긴 아래꼬리를 가진 도지, 바닥에서 강한 상승 반전 신호',
    avgReturn: 5.0,
    avgPeriod: 1.0,
    successRate: 63.0,
    frequency: 'MEDIUM',
    svgPath: 'M50 10 L50 35',
    candleData: [
      { open: 0.7, high: 0.72, low: 0.1, close: 0.7 },
    ],
  },
  {
    id: 'PIERCING_PATTERN',
    name: '관통형',
    nameEn: 'Piercing Pattern',
    category: 'CANDLE',
    type: 'PIERCING_PATTERN',
    data: [0.4, 0.3, 0.2, 0.55],
    direction: 'BULLISH',
    description: '💉 전일 음봉 50% 이상 회복하는 양봉, 매수 반격 시작 신호',
    avgReturn: 7.0,
    avgPeriod: 2.0,
    successRate: 68.0,
    frequency: 'MEDIUM',
    svgPath: 'M30 12 L30 30 M60 32 L60 15',
    candleData: [
      { open: 0.7, high: 0.75, low: 0.25, close: 0.3 },
      { open: 0.2, high: 0.65, low: 0.15, close: 0.6 },
    ],
  },
  {
    id: 'INSIDE_BAR',
    name: '인사이드바',
    nameEn: 'Inside Bar',
    category: 'CANDLE',
    type: 'INSIDE_BAR',
    data: [0.4, 0.5, 0.45, 0.6],
    direction: 'BULLISH',
    description: '📦 이전 캔들 범위 안에서 형성, 에너지 응축 후 상단 돌파 시 매수',
    avgReturn: 6.5,
    avgPeriod: 2.0,
    successRate: 65.0,
    frequency: 'HIGH',
    svgPath: 'M30 8 L30 32 M55 15 L55 28',
    candleData: [
      { open: 0.35, high: 0.85, low: 0.15, close: 0.7 },
      { open: 0.5, high: 0.65, low: 0.4, close: 0.55 },
    ],
  },
  
  // 하락 반전 캔들 패턴
  {
    id: 'IDENTICAL_THREE_CROWS',
    name: '동일삼병',
    nameEn: 'Identical Three Crows',
    category: 'CANDLE',
    type: 'IDENTICAL_THREE_CROWS',
    data: [0.8, 0.6, 0.4, 0.2],
    direction: 'BEARISH',
    description: '동일한 시가로 시작하는 세 개의 음봉, 강력한 하락 신호',
    avgReturn: -10.5,
    avgPeriod: 3.0,
    successRate: 79.0,
    frequency: 'LOW',
    svgPath: 'M20 10 L20 22 M40 18 L40 28 M60 25 L60 35',
    candleData: [
      { open: 0.8, high: 0.85, low: 0.55, close: 0.6 },
      { open: 0.6, high: 0.62, low: 0.35, close: 0.4 },
      { open: 0.4, high: 0.42, low: 0.15, close: 0.2 },
    ],
  },
  {
    id: 'BEARISH_ENGULFING',
    name: '하락장악형',
    nameEn: 'Bearish Engulfing',
    category: 'CANDLE',
    type: 'BEARISH_ENGULFING',
    data: [0.6, 0.75, 0.4, 0.2],
    direction: 'BEARISH',
    description: '양봉을 완전히 감싸는 음봉, 하락 반전 신호',
    avgReturn: -9.5,
    avgPeriod: 2.0,
    successRate: 79.0,
    frequency: 'HIGH',
    svgPath: 'M30 25 L30 15 M60 10 L60 35',
    candleData: [
      { open: 0.4, high: 0.7, low: 0.35, close: 0.65 },
      { open: 0.7, high: 0.75, low: 0.15, close: 0.2 },
    ],
  },
  {
    id: 'THREE_BLACK_CROWS',
    name: '흑삼병',
    nameEn: 'Three Black Crows',
    category: 'CANDLE',
    type: 'THREE_BLACK_CROWS',
    data: [0.8, 0.6, 0.4, 0.25, 0.1],
    direction: 'BEARISH',
    description: '연속으로 하락하는 세 개의 음봉, 강력한 하락 신호',
    avgReturn: -10.0,
    avgPeriod: 3.0,
    successRate: 78.0,
    frequency: 'MEDIUM',
    svgPath: 'M20 10 L20 20 M40 17 L40 27 M60 24 L60 35',
    candleData: [
      { open: 0.8, high: 0.85, low: 0.55, close: 0.6 },
      { open: 0.6, high: 0.65, low: 0.35, close: 0.4 },
      { open: 0.4, high: 0.45, low: 0.1, close: 0.15 },
    ],
  },
  {
    id: 'EVENING_STAR',
    name: '석별형',
    nameEn: 'Evening Star',
    category: 'CANDLE',
    type: 'EVENING_STAR',
    data: [0.7, 0.8, 0.85, 0.7, 0.3],
    direction: 'BEARISH',
    description: '상승 후 작은 몸통, 그리고 큰 음봉으로 하락 반전',
    avgReturn: -8.5,
    avgPeriod: 2.5,
    successRate: 72.0,
    frequency: 'MEDIUM',
    svgPath: 'M20 30 L20 12 M45 15 L45 18 M70 10 L70 32',
    candleData: [
      { open: 0.3, high: 0.75, low: 0.25, close: 0.7 },
      { open: 0.75, high: 0.9, low: 0.7, close: 0.8 },
      { open: 0.75, high: 0.8, low: 0.2, close: 0.25 },
    ],
  },
  {
    id: 'THREE_OUTSIDE_DOWN',
    name: '하락장악확인형',
    nameEn: 'Three Outside Down',
    category: 'CANDLE',
    type: 'THREE_OUTSIDE_DOWN',
    data: [0.65, 0.75, 0.5, 0.25],
    direction: 'BEARISH',
    description: '하락장악형 후 음봉으로 확인되는 강력한 하락 신호',
    avgReturn: -8.0,
    avgPeriod: 2.0,
    successRate: 71.0,
    frequency: 'MEDIUM',
    svgPath: 'M20 23 L20 15 M45 10 L45 30 M70 27 L70 35',
    candleData: [
      { open: 0.5, high: 0.75, low: 0.45, close: 0.7 },
      { open: 0.75, high: 0.8, low: 0.35, close: 0.4 },
      { open: 0.4, high: 0.45, low: 0.2, close: 0.25 },
    ],
  },
  {
    id: 'SHOOTING_STAR',
    name: '유성형',
    nameEn: 'Shooting Star',
    category: 'CANDLE',
    type: 'SHOOTING_STAR',
    data: [0.7, 1.0, 0.65, 0.6],
    direction: 'BEARISH',
    description: '긴 위꼬리와 작은 몸통을 가진 하락 반전 캔들',
    avgReturn: -6.5,
    avgPeriod: 1.5,
    successRate: 68.0,
    frequency: 'HIGH',
    svgPath: 'M50 5 L50 32',
    candleData: [
      { open: 0.35, high: 0.9, low: 0.25, close: 0.3 },
    ],
  },
  {
    id: 'HANGING_MAN',
    name: '교수형',
    nameEn: 'Hanging Man',
    category: 'CANDLE',
    type: 'HANGING_MAN',
    data: [0.7, 0.0, 0.65, 0.6],
    direction: 'BEARISH',
    description: '상승 추세 끝에서 나타나는 긴 아래꼬리, 하락 반전 신호',
    avgReturn: -5.5,
    avgPeriod: 1.5,
    successRate: 65.0,
    frequency: 'MEDIUM',
    svgPath: 'M50 8 L50 35',
    candleData: [
      { open: 0.7, high: 0.75, low: 0.1, close: 0.65 },
    ],
  },
  {
    id: 'GRAVESTONE_DOJI',
    name: '비석형',
    nameEn: 'Gravestone Doji',
    category: 'CANDLE',
    type: 'GRAVESTONE_DOJI',
    data: [0.5, 1.0, 0.5, 0.5],
    direction: 'BEARISH',
    description: '긴 위꼬리를 가진 도지, 천정에서 강한 하락 반전 신호',
    avgReturn: -5.0,
    avgPeriod: 1.0,
    successRate: 62.0,
    frequency: 'MEDIUM',
    svgPath: 'M50 5 L50 30',
    candleData: [
      { open: 0.3, high: 0.9, low: 0.28, close: 0.3 },
    ],
  },
  {
    id: 'DARK_CLOUD_COVER',
    name: '먹구름형',
    nameEn: 'Dark Cloud Cover',
    category: 'CANDLE',
    type: 'DARK_CLOUD_COVER',
    data: [0.5, 0.75, 0.55, 0.35],
    direction: 'BEARISH',
    description: '양봉 후 고가 갱신 음봉이 양봉 중간 이하로 마감',
    avgReturn: -6.0,
    avgPeriod: 2.0,
    successRate: 60.0,
    frequency: 'MEDIUM',
    svgPath: 'M30 30 L30 15 M60 10 L60 28',
    candleData: [
      { open: 0.3, high: 0.7, low: 0.25, close: 0.65 },
      { open: 0.75, high: 0.8, low: 0.3, close: 0.35 },
    ],
  },
  
  // 중립 캔들 패턴
  {
    id: 'DOJI',
    name: '도지',
    nameEn: 'Doji',
    category: 'CANDLE',
    type: 'DOJI',
    data: [0.5, 0.2, 0.8, 0.5],
    direction: 'NEUTRAL',
    description: '시가와 종가가 같은 불확실성을 나타내는 캔들',
    avgReturn: 3.0,
    avgPeriod: 1.0,
    successRate: 50.0,
    frequency: 'HIGH',
    svgPath: 'M50 10 L50 30',
    candleData: [
      { open: 0.5, high: 0.8, low: 0.2, close: 0.5 },
    ],
  },
  {
    id: 'SPINNING_TOP',
    name: '팽이형',
    nameEn: 'Spinning Top',
    category: 'CANDLE',
    type: 'SPINNING_TOP',
    data: [0.45, 0.2, 0.8, 0.55],
    direction: 'NEUTRAL',
    description: '작은 몸통과 긴 꼬리를 가진 불확실성 캔들',
    avgReturn: 2.0,
    avgPeriod: 1.0,
    successRate: 50.0,
    frequency: 'HIGH',
    svgPath: 'M50 10 L50 30',
    candleData: [
      { open: 0.45, high: 0.8, low: 0.2, close: 0.55 },
    ],
  },
];

