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

/** 数据集列表 */
export const getDatasetList = () => get<DatasetItem[]>('/dataset-manage')

/** 数据集详情 */
export const getDataset = (id: string | number) => get<DatasetItem>(`/dataset-manage/${id}`)

/** 创建数据集 */
export const createDataset = (data: Partial<DatasetItem>) =>
  post<{ id: string | number }>('/dataset-manage', data)

/** 更新数据集 */
export const updateDataset = (id: string | number, data: Partial<DatasetItem>) =>
  put(`/dataset-manage/${id}`, data)

/** 删除数据集 */
export const deleteDataset = (id: string | number) => del(`/dataset-manage/${id}`)

/** 上传数据集文件（multipart，字段名 files） */
export const uploadDatasetFiles = (datasetId: string | number, files: File[]) => {
  const formData = new FormData()
  files.forEach((f) => formData.append('files', f))
  return post(`/dataset-manage/upload/${datasetId}`, formData, {
    headers: { 'Content-Type': 'multipart/form-data' },
  })
}

/** 预览数据集文件内容 */
export const previewDatasetFile = (fileId: string | number) =>
  get<{ content: string }>(`/dataset-manage/preview/${fileId}`)

/** 下载文件 URL */
export const downloadFileUrl = (datasetId: string | number, fileId: string | number) =>
  `/api/dataset-manage/download/${datasetId}/${fileId}`

/** 打包下载数据集 URL */
export const downloadDatasetUrl = (datasetId: string | number) =>
  `/api/dataset-manage/download/${datasetId}`
