import { apiClient } from './client'

export interface RiskSettings {
  max_drawdown_percent: number
  daily_loss_limit_percent: number
  position_size_percent: number
}

export interface PaperTradingSettings {
  initial_capital: number
  fee_exchange: string
  fee_rates?: {
    maker: number
    taker: number
    minimum?: number
  }
}

export interface FeeExchange {
  name: string
  fees: {
    maker: number
    taker: number
    minimum?: number
  }
}

export interface FeeExchangesResponse {
  exchanges: FeeExchange[]
  current: string
}

export interface LoggingSettings {
  level: string
  dir: string
  retention_days: number
}

export interface GeneralSettings {
  timezone: string
  theme: string
  currency: string
}

export interface ExchangeCreate {
  name: string
  api_key?: string
  api_secret?: string
  sandbox?: boolean
  read_only?: boolean
  enabled?: boolean
}

export interface ExchangeUpdate {
  api_key?: string
  api_secret?: string
  sandbox?: boolean
  read_only?: boolean
  enabled?: boolean
}

export const settingsApi = {
  // Risk Management
  getRiskSettings: async (): Promise<RiskSettings> => {
    const response = await apiClient.get('/api/settings/risk')
    return response.data
  },

  updateRiskSettings: async (settings: RiskSettings) => {
    const response = await apiClient.put('/api/settings/risk', settings)
    return response.data
  },

  // Paper Trading
  getPaperTradingSettings: async (): Promise<PaperTradingSettings> => {
    const response = await apiClient.get('/api/settings/paper-trading')
    return response.data
  },

  updatePaperTradingSettings: async (settings: PaperTradingSettings) => {
    const response = await apiClient.put('/api/settings/paper-trading', settings)
    return response.data
  },

  resetPaperAccount: async () => {
    const response = await apiClient.post('/api/settings/paper-trading/reset')
    return response.data
  },

  getFeeExchanges: async (): Promise<FeeExchangesResponse> => {
    const response = await apiClient.get('/api/settings/paper-trading/fee-exchanges')
    return response.data
  },

  // Logging
  getLoggingSettings: async (): Promise<LoggingSettings> => {
    const response = await apiClient.get('/api/settings/logging')
    return response.data
  },

  updateLoggingSettings: async (settings: LoggingSettings) => {
    const response = await apiClient.put('/api/settings/logging', settings)
    return response.data
  },

  // General Settings
  getGeneralSettings: async (): Promise<GeneralSettings> => {
    const response = await apiClient.get('/api/settings/general')
    return response.data
  },

  updateGeneralSettings: async (settings: GeneralSettings) => {
    const response = await apiClient.put('/api/settings/general', settings)
    return response.data
  },

  // Exchanges
  createExchange: async (exchange: ExchangeCreate) => {
    const response = await apiClient.post('/api/settings/exchanges', exchange)
    return response.data
  },

  updateExchange: async (exchangeId: number, exchange: ExchangeUpdate) => {
    const response = await apiClient.put(`/api/settings/exchanges/${exchangeId}`, exchange)
    return response.data
  },

  deleteExchange: async (exchangeId: number) => {
    const response = await apiClient.delete(`/api/settings/exchanges/${exchangeId}`)
    return response.data
  },

  testExchangeConnection: async (exchangeId: number) => {
    const response = await apiClient.post(`/api/settings/exchanges/${exchangeId}/test`)
    return response.data
  },
}
