/**
 * 模型配置状态管理
 */
import { defineStore } from 'pinia'
import { ref } from 'vue'
import api from '@/api'

export interface ModelConfig {
  id: string
  name: string
  provider: string
  model_name: string
  base_url?: string
  user_id: string
  created_at: string
  updated_at: string
}

export const useModelConfigStore = defineStore('modelConfig', () => {
  const modelConfigs = ref<ModelConfig[]>([])
  const currentModelConfig = ref<ModelConfig | null>(null)
  const loading = ref(false)

  // 获取模型配置列表（支持筛选）
  const fetchModelConfigs = async (options?: {
    provider?: string
    skip?: number
    limit?: number
  }) => {
    loading.value = true
    try {
      const params: any = {}
      if (options?.provider) params.provider = options.provider
      if (options?.skip !== undefined) params.skip = options.skip
      if (options?.limit !== undefined) params.limit = options.limit
      
      const response = await api.get('/api/v1/model-configs', { params })
      modelConfigs.value = response.data
      return response.data
    } finally {
      loading.value = false
    }
  }

  // 创建模型配置
  const createModelConfig = async (configData: {
    name: string
    provider: string
    model_name: string
    api_key: string
    base_url?: string
  }) => {
    loading.value = true
    try {
      const response = await api.post('/api/v1/model-configs', configData)
      modelConfigs.value.push(response.data)
      return response.data
    } finally {
      loading.value = false
    }
  }

  // 获取模型配置详情
  const fetchModelConfig = async (id: string) => {
    loading.value = true
    try {
      const response = await api.get(`/api/v1/model-configs/${id}`)
      currentModelConfig.value = response.data
      return response.data
    } finally {
      loading.value = false
    }
  }

  // 更新模型配置
  const updateModelConfig = async (id: string, configData: Partial<ModelConfig & { api_key?: string }>) => {
    loading.value = true
    try {
      const response = await api.put(`/api/v1/model-configs/${id}`, configData)
      const index = modelConfigs.value.findIndex(c => c.id === id)
      if (index !== -1) {
        modelConfigs.value[index] = response.data
      }
      if (currentModelConfig.value?.id === id) {
        currentModelConfig.value = response.data
      }
      return response.data
    } finally {
      loading.value = false
    }
  }

  // 删除模型配置
  const deleteModelConfig = async (id: string) => {
    loading.value = true
    try {
      await api.delete(`/api/v1/model-configs/${id}`)
      modelConfigs.value = modelConfigs.value.filter(c => c.id !== id)
      if (currentModelConfig.value?.id === id) {
        currentModelConfig.value = null
      }
    } finally {
      loading.value = false
    }
  }

  // 测试模型配置
  const testModelConfig = async (id: string) => {
    try {
      const response = await api.post(`/api/v1/model-configs/${id}/test`)
      return response.data
    } catch (error: any) {
      throw error
    }
  }

  // 设置当前模型配置
  const setCurrentModelConfig = (config: ModelConfig | null) => {
    currentModelConfig.value = config
  }

  return {
    modelConfigs,
    currentModelConfig,
    loading,
    fetchModelConfigs,
    createModelConfig,
    fetchModelConfig,
    updateModelConfig,
    deleteModelConfig,
    testModelConfig,
    setCurrentModelConfig
  }
})
