import { get, post, put, del } from '../request'
import type { ModelItem, ModelForm, TrainedModel } from '@/types'

/** 模型列表 */
export const getModelList = () => get<ModelItem[]>('/model-manage')

/** 模型详情 */
export const getModel = (id: string | number) => get<ModelItem>(`/model-manage/${id}`)

/** 按名称查模型 */
export const getModelByName = (name: string) => get<ModelItem>(`/model-manage/name/${name}`)

/** 本地模型路径列表 */
export const getLocalModels = () =>
  get<{ models: { path: string; name: string }[] }>('/model-manage/local-models')

/** 已训练模型列表 */
export const getTrainedModels = () =>
  get<{ models: TrainedModel[] }>('/model-manage/trained-models')

/** 创建模型 */
export const createModel = (data: ModelForm) => post('/model-manage', data)

/** 编辑模型 */
export const updateModel = (id: string | number, data: ModelForm) =>
  put(`/model-manage/${id}`, data)

/** 删除模型 */
export const deleteModel = (id: string | number) => del(`/model-manage/${id}`)

/** 删除已训练模型（合并权重） */
export const deleteTrainedModel = (id: string | number, type: 'merged' | 'lora' = 'merged') =>
  del(`/model-manage/trained-models/${id}`, { type })

/** 更新模型用途 */
export const updateModelPurpose = (id: string | number, purpose: string) =>
  put(`/model-manage/${id}/purpose`, { purpose })

/** 合并 LoRA 权重 */
export const mergeModel = (data: {
  model_name: string
  train_method: string
  base_model_path: string
}) => post('/model-manage/merge', data)

/** 导出已训练模型权重 */
export const exportModelUrl = (modelName: string) =>
  `/modelTF/model-manage/trained-models/${encodeURIComponent(modelName)}/export`
