import axios from 'axios';
import { API_CONFIG } from './apiConfig';

const API_BASE_URL = API_CONFIG.baseURL;

/**
 * 보조지표 메타데이터 DTO
 */
export interface IndicatorMetadataDto {
  id: number;
  indicatorType: string;
  displayName: string;
  category: string;
  description?: string;
  minValue: number;
  maxValue: number;
  defaultValue: number;
  step: number;
  targetValueRequired: boolean;
  helperText?: string;
  allowedConditions: string;
  displayOrder: number;
  enabled: boolean;
}

/**
 * 활성화된 보조지표 목록 조회 (전략 설정용)
 */
export const getEnabledIndicators = async (): Promise<IndicatorMetadataDto[]> => {
  const response = await axios.get(`${API_BASE_URL}/indicator-metadata`);
  return response.data;
};

/**
 * 카테고리별 보조지표 목록 조회
 */
export const getIndicatorsByCategory = async (category: string): Promise<IndicatorMetadataDto[]> => {
  const response = await axios.get(`${API_BASE_URL}/indicator-metadata/category/${category}`);
  return response.data;
};

/**
 * 카테고리 목록 조회
 */
export const getIndicatorCategories = async (): Promise<string[]> => {
  const response = await axios.get(`${API_BASE_URL}/indicator-metadata/categories`);
  return response.data;
};

/**
 * 모든 보조지표 목록 조회 (관리용)
 */
export const getAllIndicators = async (): Promise<IndicatorMetadataDto[]> => {
  const response = await axios.get(`${API_BASE_URL}/indicator-metadata/all`);
  return response.data;
};

/**
 * 특정 지표의 허용 조건 목록 조회
 */
export const getAllowedConditions = async (indicatorType: string): Promise<string[]> => {
  const response = await axios.get(`${API_BASE_URL}/indicator-metadata/${indicatorType}/conditions`);
  return response.data;
};

/**
 * 보조지표 저장 (관리용)
 */
export const saveIndicator = async (indicator: Partial<IndicatorMetadataDto>): Promise<IndicatorMetadataDto> => {
  const response = await axios.post(`${API_BASE_URL}/indicator-metadata`, indicator);
  return response.data;
};

/**
 * 보조지표 수정 (관리용)
 */
export const updateIndicator = async (id: number, indicator: Partial<IndicatorMetadataDto>): Promise<IndicatorMetadataDto> => {
  const response = await axios.put(`${API_BASE_URL}/indicator-metadata/${id}`, indicator);
  return response.data;
};

/**
 * 보조지표 삭제 (관리용)
 */
export const deleteIndicator = async (id: number): Promise<void> => {
  await axios.delete(`${API_BASE_URL}/indicator-metadata/${id}`);
};

/**
 * 보조지표 활성화/비활성화 토글 (관리용)
 */
export const toggleIndicator = async (id: number, enabled: boolean): Promise<IndicatorMetadataDto> => {
  const response = await axios.patch(`${API_BASE_URL}/indicator-metadata/${id}/toggle`, null, {
    params: { enabled }
  });
  return response.data;
};

/**
 * 카테고리 한글명 매핑
 */
export const getCategoryDisplayName = (category: string): string => {
  const names: Record<string, string> = {
    'MOMENTUM': '모멘텀 지표',
    'TREND': '추세 지표',
    'VOLUME': '거래량 지표',
    'VOLATILITY': '변동성 지표',
    'PSYCHOLOGY': '심리 지표',
  };
  return names[category] || category;
};
