import axios from 'axios';

import { getApiBaseUrl } from './apiConfig';
const API_BASE_URL = getApiBaseUrl().replace('/api', ''); // /api 제거

export interface MAPriceType {
  value: string;
  koreanName: string;
  description: string;
}

export interface IndicatorCalculationSetting {
  id: number;
  indicatorType: string;
  indicatorName: string;
  priceType: string;
  priceTypeName: string;
  priceTypeDescription: string;
  description: string;
  enabled: boolean;
  createdAt: string;
  updatedAt: string;
}

/**
 * 모든 보조지표 계산 설정 조회
 */
export const getAllSettings = async (): Promise<IndicatorCalculationSetting[]> => {
  const response = await axios.get(`${API_BASE_URL}/api/indicator-calculation-settings`);
  return response.data;
};

/**
 * 특정 지표의 계산 설정 조회
 */
export const getSetting = async (indicatorType: string): Promise<IndicatorCalculationSetting> => {
  const response = await axios.get(`${API_BASE_URL}/api/indicator-calculation-settings/${indicatorType}`);
  return response.data;
};

/**
 * 보조지표 계산 설정 수정
 */
export const updateSetting = async (
  id: number,
  data: Partial<IndicatorCalculationSetting>
): Promise<IndicatorCalculationSetting> => {
  const response = await axios.put(`${API_BASE_URL}/api/indicator-calculation-settings/${id}`, data);
  return response.data;
};

/**
 * 사용 가능한 가격 타입 목록 조회
 */
export const getPriceTypes = async (): Promise<MAPriceType[]> => {
  const response = await axios.get(`${API_BASE_URL}/api/indicator-calculation-settings/price-types`);
  return response.data;
};

/**
 * 설정 초기화 (기본값으로 복원)
 */
export const resetToDefaults = async (): Promise<string> => {
  const response = await axios.post(`${API_BASE_URL}/api/indicator-calculation-settings/reset`);
  return response.data;
};

/**
 * 권장 설정 적용 (골든/데드크로스 감지 최적화)
 */
export const applyRecommended = async (): Promise<string> => {
  const response = await axios.post(`${API_BASE_URL}/api/indicator-calculation-settings/apply-recommended`);
  return response.data;
};
