// @/apis/data/index.ts - 数据管理API
import http from '@/utils/http'
import type {
  FolderInfo,
  FileInfo,
  FolderListParams,
  FileListParams,
  FolderListResponse,
  FileListResponse,
  CreateFolderParams,
  RenameFolderParams,
  DeleteFolderParams,
  UploadFileParams,
  DownloadFileParams,
  DeleteFileParams,
  PreviewFileParams,
  RenameFileParams
} from './type'

const { request, requestRaw } = http

// 导出类型定义
export type {
  FolderInfo,
  FileInfo,
  FolderListParams,
  FileListParams,
  FolderListResponse,
  FileListResponse,
  CreateFolderParams,
  RenameFolderParams,
  DeleteFolderParams,
  UploadFileParams,
  DownloadFileParams,
  DeleteFileParams,
  PreviewFileParams,
  RenameFileParams
}

// 获取文件夹列表（分页）
export function getFolderListApi(params?: FolderListParams) {
  return request<FolderListResponse>({
    url: '/data/folder/list',
    method: 'get',
    params: {
      page: params?.page || 1,
      pageSize: params?.pageSize || 10,
      folderName: params?.folderName
    }
  })
}

// 获取文件列表（分页）
export function getFilesApi(params?: FileListParams) {
  return request<FileListResponse>({
    url: '/data/file/list',
    method: 'get',
    params: {
      page: params?.page || 1,
      pageSize: params?.pageSize || 10,
      folderId: params?.folderId || '0',
      fileName: params?.fileName,
      sortField: params?.sortField,
      sortOrder: params?.sortOrder
    }
  })
}

// 创建文件夹
export function createFolderApi(data: CreateFolderParams) {
  return request({
    url: '/data/folder/creatFolder',
    method: 'post',
    data: {
      name: data.name,
      parentId: data.parentId || '0'
    }
  })
}

// 重命名文件夹
export function updateFolderApi(folderId: string, newName: string) {
  return request({
    url: '/data/folder/rename',
    method: 'put',
    params: {
      folderId: folderId,
      newName: newName
    }
  })
}

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

// 上传文件
export function uploadFileApi(
  file: File,
  folderId: string,
  onUploadProgress?: (progressEvent: any) => void,
  cancelToken?: any
) {
  const formData = new FormData()
  formData.append('file', file)
  
  return requestRaw({
    url: '/data/file/add',
    method: 'post',
    params: {
      folderId: folderId
    },
    data: formData,
    onUploadProgress,
    cancelToken,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  }).then(response => response.data)
    .catch(error => {
      // 确保错误不会抛出，而是返回一个错误对象
      console.error('上传文件API错误:', error)
      return {
        code: 500,
        msg: error.message || '上传失败',
        success: false
      }
    })
}

// 下载文件
export function downloadFileApi(fileId: string) {
  return request({
    url: '/data/file/download',
    method: 'get',
    params: {
      fileId: fileId
    },
    responseType: 'blob'
  })
}

// 删除文件
export function deleteFileApi(fileId: string) {
  return request({
    url: '/data/file/delete',
    method: 'delete',
    params: {
      fileId: fileId
    }
  })
}

// 预览文件
export function previewFileApi(fileId: string) {
  return request({
    url: '/data/file/preview',
    method: 'get',
    params: {
      fileId: fileId
    },
    responseType: 'blob'
  })
}

// 重命名文件
export function renameFileApi(fileId: string, newFileName: string) {
  return request({
    url: '/data/file/rename',
    method: 'put',
    params: {
      fileId: fileId,
      newFileName: newFileName
    }
  })
}

// 重命名文件（兼容旧接口）
export function updateFileNameApi(fileId: string, data: RenameFileParams) {
  return renameFileApi(fileId, data.newFileName)
} 