import { get, post, put, del } from '../request'
import type { FineTuneStartPayload, FineTuneTask, TrainingProgress } from '@/types'

/** 训练任务列表 */
export const getFineTuneList = () => get<FineTuneTask[]>('/fine-tune')

/** 训练任务详情 */
export const getFineTune = (id: string | number) => get<FineTuneTask>(`/fine-tune/${id}`)

/** 任务名查重 */
export const checkFineTuneName = (name: string) =>
  get<{ exists: boolean }>('/fine-tune/check-name', { name })

/** 创建训练任务记录（第一步） */
export const createFineTune = (data: Partial<FineTuneTask>) =>
  post<{ id: string | number }>('/fine-tune', data)

/** 启动训练（第二步） */
export const startFineTune = (data: FineTuneStartPayload) => post('/fine-tune/start', data)

/** 更新训练任务 */
export const updateFineTune = (id: string | number, data: Partial<FineTuneTask>) =>
  put(`/fine-tune/${id}`, data)

/** 停止训练任务 */
export const stopFineTune = (id: string | number) => post(`/fine-tune/stop/${id}`)

/** 删除训练任务 */
export const deleteFineTune = (id: string | number) => del(`/fine-tune/${id}`)

/** 获取训练进度 */
export const getFineTuneProgress = (id: string | number) =>
  get<TrainingProgress>(`/fine-tune/progress/${id}`)

/** 启动 TensorBoard */
export const startTensorboard = () => post('/fine-tune/tensorboard/start')

/** 提交 Web 日志 */
export const sendWebLog = (level: string, message: string, page: string) =>
  post('/web-log', { level, message, page, timestamp: new Date().toISOString() })
