import request from "@/utils/request";

/**
 * 获取用户分类
 * @returns {Promise}
 */
export function getUserCate() {
  return request({
    url: `/backend/usercate`,
    method: "get",
  });
}

/**
 * 获取所有文件
 * @returns {Promise}
 */
export function getAllFiles() {
  return request({
    url: "/backend/allfiles",
    method: "get",  
  });
}

/**
 * 新建文件分组
 * @param {Object} data 文件分组数据
 * @returns {Promise}
 */
export function createFileCate(data) {
  return request({
    url: "/backend/createfilecate",
    method: "post",
    data,
  });
}

/**
 * 重命名文件分组
 * @param {number|string} id 文件分组ID
 * @param {Object} data 文件分组数据
 * @returns {Promise}
 */
export function renameFileCate(id, data) {
  return request({
    url: `/backend/renamefilecate/${id}`,
    method: "post",
    data,
  });
}

/**
 * 删除文件分组
 * @param {number|string} id 文件分组ID
 * @returns {Promise}
 */
export function deleteFileCate(id) {
  return request({
    url: `/backend/deletefilecate/${id}`,
    method: "delete",
  });
}

/**
 * 根据分类ID获取文件
 * @param {number|string} id 分类ID
 * @param {number} page 页码，默认1
 * @param {number} pageSize 每页数量，默认24
 * @param {string} keyword 搜索关键词，可选
 * @returns {Promise}
 */
export function getCateFiles(id, page = 1, pageSize = 24, keyword = "") {
  const params = {
    page,
    pageSize,
  };
  if (keyword) {
    params.keyword = keyword;
  }
  return request({
    url: `/backend/catefiles/${id}`,
    method: "get",
    params,
  });
}

/**
 * 根据ID获取文件
 * @param {number|string} id 文件ID
 * @returns {Promise}
 */
export function getFileById(id) {
  return request({
    url: `/backend/catefiles`,
    method: "get",
  });
}

/**
 * 上传文件
 * @param {FormData} formData 文件数据
 * @param {Object} options 额外选项
 * @param {string} [options.cate]
 * @returns {Promise}
 */
export function uploadFile(formData, options = {}) {
  if (options.cate) {
    formData.append('cate', options.cate);
  }

  return request({
    url: "/backend/uploadfile",
    method: "post",
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data"
    }
  });
}

/**
 * 更新文件信息
 * @param {number|string} id 文件ID
 * @param {Object} fileData 更新的数据
 * @returns {Promise}
 */
export function updateFile(id, fileData) {
  return request({
    url: `/backend/updatefile/${id}`,
    method: "post",
    data: fileData,
  });
}

/**
 * 删除文件
 * @param {number|string} id 文件ID
 * @returns {Promise}
 */
export function deleteFile(id) {
  return request({
    url: `/backend/deletefile/${id}`,
    method: "delete",
  });
}

/**
 * 删除文件
 * @param {number|string} id 文件ID
 * @returns {Promise}
 */
export function deleteFilePermanently(id) {
  return request({
    url: `/backend/deleteFilePermanently/${id}`,
    method: "delete",
  });
}

/**
 * 移动文件
 * @param {number|string} id 文件ID
 * @param {Object} fileData 更新的数据
 * @returns {Promise}
 */
export function moveFile(id, cate) {
  return request({
    url: `/backend/movefile/${id}`,
    method: "get",
    params: { cate },
  });
}

/**
 * 批量删除文件
 * @param {Array} ids 文件ID数组
 * @returns {Promise}
 */
export function batchDeleteFiles(ids) {
  return request({
    url: "/backend/batchDeleteFiles",
    method: "post",
    data: { ids },
  });
}

/**
 * 批量彻底删除文件
 * @param {Array} ids 文件ID数组
 * @returns {Promise}
 */
export function batchDeleteFilesPermanently(ids) {
  return request({
    url: "/backend/batchDeleteFilesPermanently",
    method: "post",
    data: { ids },
  });
}

/**
 * 批量移动文件
 * @param {Array} ids 文件ID数组
 * @param {number} cate 目标分类ID
 * @returns {Promise}
 */
export function batchMoveFiles(ids, cate) {
  return request({
    url: "/backend/batchMoveFiles",
    method: "post",
    data: { ids, cate },
  });
}