/** Settings API client */

import { BaseApiClient } from './base'

const API_BASE = '/api/v1'

// --- Response types ---

export interface ILlmProviderResponse {
  name: string
  type: string
  api_key: string
  base_url: string
  models: Record<string, unknown>
  max_tokens: number
  timeout: number
}

export interface ILlmConfigResponse {
  providers: ILlmProviderResponse[]
  model_aliases: Record<string, string>
  fallbacks: Record<string, string[]>
}

export interface ISkillsConfigResponse {
  paths: string[]
  auto_discover: boolean
}

export interface IKbConfigResponse {
  memory: Record<string, unknown>
}

export interface IGeneralConfigResponse {
  host: string
  port: number
  workers: number
  log_level: string
  log_format: string
  rate_limit: number
  cors_origins: string[]
}

// --- Request types ---

export interface ILlmProviderUpdate {
  name: string
  type?: string
  api_key?: string | null
  base_url?: string
  models?: Record<string, unknown> | null
  max_tokens?: number
  timeout?: number
}

export interface ILlmConfigUpdate {
  providers?: ILlmProviderUpdate[] | null
  model_aliases?: Record<string, string> | null
  fallbacks?: Record<string, string[]> | null
}

export interface ISkillsConfigUpdate {
  paths?: string[] | null
  auto_discover?: boolean | null
}

export interface IKbConfigUpdate {
  memory: Record<string, unknown>
}

export interface IGeneralConfigUpdate {
  host?: string | null
  port?: number | null
  workers?: number | null
  log_level?: string | null
  log_format?: string | null
  rate_limit?: number | null
  cors_origins?: string[] | null
}

class SettingsApiClient extends BaseApiClient {
  constructor(baseUrl: string = API_BASE) {
    super(baseUrl)
  }

  // --- LLM Settings ---

  async getLlmConfig(): Promise<ILlmConfigResponse> {
    return this.request<ILlmConfigResponse>('/settings/llm')
  }

  async updateLlmConfig(data: ILlmConfigUpdate): Promise<ILlmConfigResponse> {
    return this.request<ILlmConfigResponse>('/settings/llm', {
      method: 'PUT',
      body: JSON.stringify(data),
    })
  }

  // --- Skills Settings ---

  async getSkillsConfig(): Promise<ISkillsConfigResponse> {
    return this.request<ISkillsConfigResponse>('/settings/skills')
  }

  async updateSkillsConfig(data: ISkillsConfigUpdate): Promise<ISkillsConfigResponse> {
    return this.request<ISkillsConfigResponse>('/settings/skills', {
      method: 'PUT',
      body: JSON.stringify(data),
    })
  }

  // --- Knowledge Base Settings ---

  async getKbConfig(): Promise<IKbConfigResponse> {
    return this.request<IKbConfigResponse>('/settings/kb')
  }

  async updateKbConfig(data: IKbConfigUpdate): Promise<IKbConfigResponse> {
    return this.request<IKbConfigResponse>('/settings/kb', {
      method: 'PUT',
      body: JSON.stringify(data),
    })
  }

  // --- General Settings ---

  async getGeneralConfig(): Promise<IGeneralConfigResponse> {
    return this.request<IGeneralConfigResponse>('/settings/general')
  }

  async updateGeneralConfig(data: IGeneralConfigUpdate): Promise<IGeneralConfigResponse> {
    return this.request<IGeneralConfigResponse>('/settings/general', {
      method: 'PUT',
      body: JSON.stringify(data),
    })
  }
}

export const settingsApi = new SettingsApiClient()
