import { get, post, put, del } from '../request'
import type { DatasetItem, DatasetVersion, DatasetVersionList } 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/${encodeURIComponent(fileId)}`)

/** 数据集文件版本列表 */
export const getDatasetFileVersions = (fileId: string | number) =>
  get<DatasetVersionList>(`/dataset-manage/versions/${encodeURIComponent(fileId)}`)

/** 读取指定历史版本内容（不改变当前版本） */
export const getDatasetFileVersionContent = (fileId: string | number, versionId: string) =>
  get<{ version: DatasetVersion; content: string }>(
    `/dataset-manage/versions/${encodeURIComponent(fileId)}/${encodeURIComponent(versionId)}`,
  )

/** 保存为新版本，并自动设为当前版本 */
export const createDatasetFileVersion = (
  fileId: string | number,
  content: string,
  options: {
    description?: string
    baseVersionId: string
    expectedCurrentVersionId: string
  },
) =>
  post<{ version: DatasetVersion; content: string }>(
    `/dataset-manage/versions/${encodeURIComponent(fileId)}`,
    {
      content,
      description: options.description || '在线编辑',
      base_version_id: options.baseVersionId,
      expected_current_version_id: options.expectedCurrentVersionId,
    },
  )

/** 切换当前使用的数据集版本 */
export const activateDatasetFileVersion = (
  fileId: string | number,
  versionId: string,
  expectedCurrentVersionId: string,
) =>
  put<{ version: DatasetVersion; content: string }>(
    `/dataset-manage/versions/${encodeURIComponent(fileId)}/active`,
    { version_id: versionId, expected_current_version_id: expectedCurrentVersionId },
  )

/** 删除非当前、非初始的历史版本 */
export const deleteDatasetFileVersion = (
  fileId: string | number,
  versionId: string,
  expectedCurrentVersionId: string,
) =>
  del<DatasetVersionList>(
    `/dataset-manage/versions/${encodeURIComponent(fileId)}/${encodeURIComponent(versionId)}`,
    { expected_current_version_id: expectedCurrentVersionId },
  )

/** 下载文件 URL */
export const downloadFileUrl = (
  datasetId: string | number,
  fileId: string | number,
  versionId?: string,
) => {
  const baseUrl = `/modelTF/dataset-manage/download/${datasetId}/${fileId}`
  return versionId ? `${baseUrl}?version_id=${encodeURIComponent(versionId)}` : baseUrl
}

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