import apiClient from './client'

// 预设接口定义
export interface Preset {
  id: number
  userId: number
  name: string
  description: string
  isPublic: boolean
  isDefault: boolean
  temperature: number
  topP: number
  topK: number
  frequencyPenalty: number
  presencePenalty: number
  maxTokens: number
  repetitionPenalty: number
  minP: number
  topA: number
  systemPrompt: string
  stopSequences: string[]
  extensions: Record<string, any>
  useCount: number
  createdAt: string
  updatedAt: string
}

// 创建预设请求
export interface CreatePresetRequest {
  name: string
  description?: string
  isPublic?: boolean
  temperature?: number
  topP?: number
  topK?: number
  frequencyPenalty?: number
  presencePenalty?: number
  maxTokens?: number
  repetitionPenalty?: number
  minP?: number
  topA?: number
  systemPrompt?: string
  stopSequences?: string[]
  extensions?: Record<string, any>
}

// 预设列表响应
export interface PresetListResponse {
  list: Preset[]
  total: number
  page: number
  pageSize: number
}

// 预设 API
export const presetApi = {
  // 创建预设
  createPreset: (data: CreatePresetRequest) => {
    return apiClient.post<Preset>('/app/preset', data)
  },

  // 获取预设列表
  getPresetList: (params: { page?: number; pageSize?: number; keyword?: string; isPublic?: boolean }) => {
    return apiClient.get<PresetListResponse>('/app/preset', { params })
  },

  // 根据ID获取预设
  getPresetById: (id: number) => {
    return apiClient.get<Preset>(`/app/preset/${id}`)
  },

  // 更新预设
  updatePreset: (id: number, data: Partial<CreatePresetRequest>) => {
    return apiClient.put<Preset>(`/app/preset/${id}`, data)
  },

  // 删除预设
  deletePreset: (id: number) => {
    return apiClient.delete(`/app/preset/${id}`)
  },

  // 设置默认预设
  setDefaultPreset: (id: number) => {
    return apiClient.post(`/app/preset/${id}/default`)
  },

  // 导入预设
  importPreset: (file: File) => {
    const formData = new FormData()
    formData.append('file', file)
    return apiClient.post<Preset>('/app/preset/import', formData, {
      headers: { 'Content-Type': 'multipart/form-data' }
    })
  },

  // 导出预设
  exportPreset: (id: number) => {
    return apiClient.get(`/app/preset/${id}/export`, {
      responseType: 'blob'
    })
  }
}
