import axios from 'axios'

export interface ConfigItem {
  id: string
  config_key: string
  config_value: string
  config_type: string
  service_id: string | null
  description: string
  status: string
  created_at: string
  updated_at: string
}

export interface AlgorithmConfig {
  algorithm_id: string
  algorithm_name: string
  version: string
  config: string
}

export interface ComparisonResult {
  success: boolean
  comparison_time: string
  results: Array<{
    algorithm_name: string
    algorithm_id: string
    version: string
    execution_time: number
    success: boolean
    output: any
    error: string | null
  }>
}

export interface ComparisonReport {
  success: boolean
  report_time: string
  summary: string
  performance_analysis: string
  recommendations: string
}

class ConfigService {
  private readonly baseUrl = '/api/config'

  async getConfig(configKey: string): Promise<ConfigItem | null> {
    try {
      const response = await axios.get(`${this.baseUrl}/${configKey}`)
      return response.data
    } catch (error) {
      console.error('获取配置失败:', error)
      throw error
    }
  }

  async setConfig(configKey: string, configData: {
    value: string
    type?: string
    service_id?: string | null
    description?: string
  }): Promise<boolean> {
    try {
      const response = await axios.post(`${this.baseUrl}/${configKey}`, {
        value: configData.value,
        type: configData.type || 'system',
        service_id: configData.service_id || null,
        description: configData.description || ''
      })
      return response.data.message === '设置配置成功'
    } catch (error) {
      console.error('设置配置失败:', error)
      throw error
    }
  }

  async getServiceConfigs(serviceId: string): Promise<ConfigItem[]> {
    try {
      const response = await axios.get(`${this.baseUrl}/service/${serviceId}`)
      return response.data.configs || []
    } catch (error) {
      console.error('获取服务配置失败:', error)
      throw error
    }
  }

  async deleteConfig(configKey: string): Promise<boolean> {
    try {
      const response = await axios.delete(`${this.baseUrl}/${configKey}`)
      return response.data.message === '删除配置成功'
    } catch (error) {
      console.error('删除配置失败:', error)
      throw error
    }
  }

  async getAllConfigs(configType?: string): Promise<ConfigItem[]> {
    try {
      const params: Record<string, any> = {}
      if (configType) {
        params.config_type = configType
      }
      
      const response = await axios.get(`${this.baseUrl}/`, { params })
      return response.data.configs || []
    } catch (error) {
      console.error('获取所有配置失败:', error)
      throw error
    }
  }
}

class ComparisonService {
  private readonly baseUrl = '/api/comparison'

  async compareAlgorithms(inputData: any, algorithmConfigs: AlgorithmConfig[]): Promise<ComparisonResult> {
    try {
      const response = await axios.post(`${this.baseUrl}/compare-algorithms`, {
        input_data: inputData,
        algorithm_configs: algorithmConfigs
      })
      return response.data
    } catch (error) {
      console.error('算法比较失败:', error)
      throw error
    }
  }

  async generateComparisonReport(comparisonResults: ComparisonResult): Promise<ComparisonReport> {
    try {
      const response = await axios.post(`${this.baseUrl}/generate-report`, comparisonResults)
      return response.data
    } catch (error) {
      console.error('生成比较报告失败:', error)
      throw error
    }
  }
}

export const configService = new ConfigService()
export const comparisonService = new ComparisonService()