/**
 * @description 文件相关API
 * @author yslg
 * @since 2025-10-16
 */

import { api } from '@/apis/index';
import type { SysFile, FileUploadParam, BatchFileUploadParam, ResultDomain } from '@/types';

/**
 * 文件API服务
 */
export const fileApi = {
  /**
   * 上传文件
   * @param param 文件上传参数
   * @returns Promise<ResultDomain<SysFile>>
   */
  async uploadFile(param: FileUploadParam): Promise<ResultDomain<SysFile>> {
    const formData = new FormData();
    formData.append('file', param.file);
    if (param.module) {
      formData.append('module', param.module);
    }
    if (param.businessId) {
      formData.append('businessId', param.businessId);
    }
    if (param.uploader) {
      formData.append('uploader', param.uploader);
    }
    const response = await api.upload<SysFile>('/file/upload', formData);
    return response.data;
  },

  /**
   * 批量上传文件
   * @param param 批量文件上传参数
   * @returns Promise<ResultDomain<SysFile>>
   */
  async batchUploadFiles(param: BatchFileUploadParam): Promise<ResultDomain<SysFile>> {
    const formData = new FormData();
    param.files.forEach(file => {
      formData.append('files', file);
    });
    if (param.module) {
      formData.append('module', param.module);
    }
    if (param.businessId) {
      formData.append('businessId', param.businessId);
    }
    if (param.uploader) {
      formData.append('uploader', param.uploader);
    }
    const response = await api.upload<SysFile>('/file/batch-upload', formData);
    return response.data;
  },

  /**
   * 下载文件
   * @param fileId 文件ID
   * @param filename 保存的文件名（可选）
   * @returns Promise<void>
   */
  async downloadFile(fileId: string, filename?: string): Promise<void> {
    return api.download(`/file/download/${fileId}`, filename);
  },

  /**
   * 删除文件（逻辑删除）
   * @param fileId 文件ID
   * @returns Promise<ResultDomain<SysFile>>
   */
  async deleteFile(fileId: string): Promise<ResultDomain<SysFile>> {
    const response = await api.delete<SysFile>(`/file/${fileId}`);
    return response.data;
  },

  /**
   * 物理删除文件
   * @param fileId 文件ID
   * @returns Promise<ResultDomain<SysFile>>
   */
  async deleteFilePhysically(fileId: string): Promise<ResultDomain<SysFile>> {
    const response = await api.delete<SysFile>(`/file/physical/${fileId}`);
    return response.data;
  },

  /**
   * 批量删除文件
   * @param fileIds 文件ID数组
   * @returns Promise<ResultDomain<SysFile>>
   */
  async batchDeleteFiles(fileIds: string[]): Promise<ResultDomain<SysFile>> {
    const response = await api.delete<SysFile>('/file/batch', fileIds);
    return response.data;
  },

  /**
   * 根据文件ID查询文件信息
   * @param fileId 文件ID
   * @returns Promise<ResultDomain<SysFile>>
   */
  async getFileById(fileId: string): Promise<ResultDomain<SysFile>> {
    const response = await api.get<SysFile>(`/file/${fileId}`);
    return response.data;
  },

  /**
   * 根据业务ID查询文件列表
   * @param module 所属模块
   * @param businessId 业务ID
   * @returns Promise<ResultDomain<SysFile>>
   */
  async getFilesByBusinessId(module: string, businessId: string): Promise<ResultDomain<SysFile>> {
    const response = await api.get<SysFile>(`/file/business/${module}/${businessId}`);
    return response.data;
  },

  /**
   * 根据上传者查询文件列表
   * @param uploader 上传者用户ID
   * @returns Promise<ResultDomain<SysFile>>
   */
  async getFilesByUploader(uploader: string): Promise<ResultDomain<SysFile>> {
    const response = await api.get<SysFile>(`/file/uploader/${uploader}`);
    return response.data;
  },

  /**
   * 获取文件访问URL
   * @param fileId 文件ID
   * @returns Promise<ResultDomain<string>>
   */
  async getFileUrl(fileId: string): Promise<ResultDomain<string>> {
    const response = await api.get<string>(`/file/url/${fileId}`);
    return response.data;
  }
};

