import { apiClient } from './client'

// Autopilot configuration (intelligent mode only)
export interface UnifiedAutopilotConfig {
  symbol: string
  mode?: 'intelligent' // Only intelligent mode supported
  auto_execute?: boolean
  interval?: number
  exchange_id?: number
  timeframe?: string
  paper_trading?: boolean
}





export const autopilotApi = {

  // Intelligent Autopilot APIs
  startIntelligent: async (config: {
    symbol: string
    exchange_id?: number
    timeframe?: string
    interval?: number
    paper_trading?: boolean
  }) => {
    const response = await apiClient.post('/api/autopilot/intelligent/start', config)
    return response.data
  },

  stopIntelligent: async (symbol: string, timeframe: string = '1h') => {
    const response = await apiClient.post(
      `/api/autopilot/intelligent/stop?symbol=${symbol}&timeframe=${timeframe}`
    )
    return response.data
  },

  getIntelligentStatus: async (symbol: string, timeframe: string = '1h'): Promise<{
    symbol: string
    timeframe: string
    running: boolean
    selected_strategy: string | null
    trades_today: number
    max_trades_per_day: number
    min_confidence_threshold: number
    enable_auto_execution: boolean
    last_analysis: any
    model_info: any
  }> => {
    const response = await apiClient.get(
      `/api/autopilot/intelligent/status/${symbol}?timeframe=${timeframe}`
    )
    return response.data
  },

  getIntelligentPerformance: async (strategy_name?: string, days: number = 30) => {
    const params: any = { days }
    if (strategy_name) params.strategy_name = strategy_name
    const response = await apiClient.get('/api/autopilot/intelligent/performance', { params })
    return response.data
  },

  retrainModel: async (force: boolean = false) => {
    const response = await apiClient.post('/api/autopilot/intelligent/retrain', null, {
      params: { force },
    })
    return response.data
  },

  getTrainingStats: async (days: number = 365): Promise<TrainingStats> => {
    const response = await apiClient.get('/api/autopilot/intelligent/training-stats', {
      params: { days }
    })
    return response.data
  },

  getModelInfo: async () => {
    const response = await apiClient.get('/api/autopilot/intelligent/model-info')
    return response.data
  },

  resetModel: async (): Promise<{ status: string; message: string; deleted_count: number }> => {
    const response = await apiClient.post('/api/autopilot/intelligent/reset')
    return response.data
  },

  // Unified Autopilot APIs
  getModes: async (): Promise<any> => {
    const response = await apiClient.get('/api/autopilot/modes')
    return response.data
  },

  startUnified: async (config: UnifiedAutopilotConfig) => {
    const response = await apiClient.post('/api/autopilot/start-unified', config)
    return response.data
  },

  stopUnified: async (symbol: string, mode: 'intelligent' = 'intelligent', timeframe: string = '1h') => {
    const response = await apiClient.post(
      `/api/autopilot/stop-unified?symbol=${symbol}&mode=${mode}&timeframe=${timeframe}`
    )
    return response.data
  },

  getUnifiedStatus: async (
    symbol: string,
    mode: 'intelligent' = 'intelligent',
    timeframe: string = '1h'
  ) => {
    const response = await apiClient.get(
      `/api/autopilot/status-unified/${symbol}?mode=${mode}&timeframe=${timeframe}`
    )
    return response.data
  },

  // Bootstrap Configuration APIs
  getBootstrapConfig: async (): Promise<BootstrapConfig> => {
    const response = await apiClient.get('/api/autopilot/bootstrap-config')
    return response.data
  },

  updateBootstrapConfig: async (config: BootstrapConfig) => {
    const response = await apiClient.put('/api/autopilot/bootstrap-config', config)
    return response.data
  },

  // Multi-Symbol Autopilot APIs
  startMultiSymbol: async (config: MultiSymbolAutopilotConfig): Promise<MultiSymbolResponse> => {
    const response = await apiClient.post('/api/autopilot/multi-symbol/start', config)
    return response.data
  },

  stopMultiSymbol: async (symbols: string[], mode: string = 'intelligent', timeframe: string = '1h'): Promise<MultiSymbolResponse> => {
    const response = await apiClient.post('/api/autopilot/multi-symbol/stop', symbols, {
      params: { mode, timeframe }
    })
    return response.data
  },

  getTaskStatus: async (taskId: string): Promise<any> => {
    const response = await apiClient.get(`/api/autopilot/tasks/${taskId}`)
    return response.data
  },

  getMultiSymbolStatus: async (symbols?: string[], mode: string = 'intelligent', timeframe: string = '1h'): Promise<MultiSymbolStatusResponse> => {
    const response = await apiClient.get('/api/autopilot/multi-symbol/status', {
      params: {
        symbols: symbols?.join(',') || '',
        mode,
        timeframe
      }
    })
    return response.data
  },
}

export interface TrainingStats {
  total_samples: number
  strategy_counts: Record<string, number>
}

export interface BootstrapConfig {
  days: number
  timeframe: string
  min_samples_per_strategy: number
  symbols: string[]
}

export interface MultiSymbolAutopilotConfig {
  symbols: string[]
  mode?: 'intelligent' // Only intelligent mode supported
  auto_execute?: boolean
  timeframe?: string
  exchange_id?: number
  paper_trading?: boolean
  interval?: number
}

export interface MultiSymbolResponse {
  status: string
  mode?: string
  symbols: Array<{
    symbol: string
    status: string
  }>
}

export interface MultiSymbolStatusResponse {
  mode: string
  symbols: Array<{
    symbol: string
    mode: string
    running: boolean
    selected_strategy?: string
    trades_today?: number
    last_analysis?: any
  }>
  total_running: number
}
