import api from './index'

export type LLMProvider = 'openai' | 'claude' | 'ollama' | 'deepseek' | 'custom'
export type LLMType = 'chat' | 'vlm' | 'embedding' | 'rerank'

export interface LLMModelConfig {
  name: string  // 模型名称/别名
  provider: LLMProvider
  model: string
  base_url: string
  api_key: string
  enabled: boolean  // 是否启用
}

export interface LLMConfig {
  chat?: LLMModelConfig[]
  vlm?: LLMModelConfig[]
  embedding?: LLMModelConfig[]
  rerank?: LLMModelConfig[]
}

export interface SchedulerConfig {
  daily_plan_time?: string
  forum_scan_interval_minutes?: number
  todo_ai_generate_time?: string
  enabled?: boolean
}

export interface ProfileUpdate {
  full_name?: string
  password?: string
  current_password?: string
}

export interface SettingsResponse {
  profile: {
    id: string
    email: string
    full_name: string
    created_at: string
  }
  llm_config: LLMConfig
  scheduler_config: SchedulerConfig
}

export const settingsApi = {
  // 获取设置
  get() {
    return api.get<SettingsResponse>('/api/settings')
  },

  // 更新资料
  updateProfile(data: ProfileUpdate) {
    return api.put('/api/settings/profile', data)
  },

  // 更新 LLM 配置
  updateLLM(config: Partial<LLMConfig>) {
    return api.put('/api/settings/llm', config)
  },

  // 测试 LLM 连接
  testLLM(data: { type: LLMType } & Omit<LLMModelConfig, 'name' | 'enabled'>) {
    return api.post('/api/settings/llm/test', data)
  },

  // 更新定时任务配置
  updateScheduler(config: Partial<SchedulerConfig>) {
    return api.put('/api/settings/scheduler', config)
  },
}
