import http from '@/utils/http'

const { request } = http

// 文件信息
export interface KnowledgeFile {
  id: string
  name: string
  size: string
  type: string
  uploadTime: string
}

// 文件夹信息
export interface KnowledgeFolder {
  id: string
  name: string
  children?: KnowledgeFolder[]
}

// 获取文件夹树
export function getFolderTreeApi() {
  return request<KnowledgeFolder[]>({
    url: '/knowledge/folders',
    method: 'get',
  })
}

// 获取文件夹列表
export function getFolderListApi(params?: { page?: number; pageSize?: number; folderName?: string }) {
  return request<{ list: KnowledgeFolder[]; total: number }>({
    url: '/knowledge/folder-list',
    method: 'get',
    params,
  })
}

// 获取文件列表（按文件夹）
export function getFilesApi(folderId: string) {
  return request<KnowledgeFile[]>({
    url: '/knowledge/files',
    method: 'get',
    params: { folderId },
  })
}



// 创建文件夹
export function createFolderApi(data: { name: string; parentId?: string }) {
  return request({
    url: '/knowledge/create-folder',
    method: 'post',
    data,
  })
}

// 更新文件夹
export function updateFolderApi(folderId: string, data: { name: string }) {
  return request({
    url: `/knowledge/update-folder/${folderId}`,
    method: 'put',
    data,
  })
}

// 删除文件夹
export function deleteFolderApi(folderId: string) {
  return request({
    url: `/knowledge/delete-folder/${folderId}`,
    method: 'delete',
  })
}

// 删除文件
export function deleteFileApi(fileId: string) {
  return request({
    url: `/knowledge/delete-file/${fileId}`,
    method: 'delete',
  })
}

// 下载文件
export function downloadFileApi(fileId: string) {
  return request<Blob>({
    url: `/knowledge/download/${fileId}`,
    method: 'get',
    responseType: 'blob',
  })
}

// 上传文件
export function uploadFileApi(data: FormData) {
  return request({
    url: '/knowledge/upload',
    method: 'post',
    data,
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  })
}

// 更新文件名
export function updateFileNameApi(fileId: string, newName: string) {
  return request({
    url: `/knowledge/update-file-name/${fileId}`,
    method: 'put',
    data: { name: newName },
  })
}

// 预览文件
export function previewFileApi(fileId: string) {
  return request({
    url: `/knowledge/preview/${fileId}`,
    method: 'get',
  })
}
