/**
 * KRA - File Upload API Service
 */
import request from '@/utils/request';

export interface FileInfo {
  ID?: number;
  CreatedAt?: string;
  UpdatedAt?: string;
  name: string;
  url: string;
  tag: string;
  key: string;
  categoryId?: number;
}

export interface FileSearchParams {
  page: number;
  pageSize: number;
  name?: string;
  tag?: string;
  categoryId?: number;
  keyword?: string;
  classId?: number;
}

/** 分页获取文件列表 */
export const getFileList = (data: FileSearchParams) => {
  return request.post('/fileUploadAndDownload/getFileList', data);
};

/** 删除文件 */
export const deleteFile = (data: { ID: number }) => {
  return request.post('/fileUploadAndDownload/deleteFile', data);
};

/** 编辑文件名或备注 */
export const editFileName = (data: { ID: number; name: string }) => {
  return request.post('/fileUploadAndDownload/editFileName', data);
};

/** 导入URL */
export const importURL = (data: any) => {
  return request.post('/fileUploadAndDownload/importURL', data);
};

/** 上传文件 */
export const uploadFile = (data: FormData) => {
  return request.post('/fileUploadAndDownload/upload', data, {
    headers: { 'Content-Type': 'multipart/form-data' },
  });
};

/** 查找文件（断点续传用） */
export const findFile = (params: { fileMd5: string }) => {
  return request.get('/fileUploadAndDownload/findFile', { params });
};

/** 断点续传 */
export const breakpointContinue = (data: FormData) => {
  return request.post('/fileUploadAndDownload/breakpointContinue', data, {
    headers: { 'Content-Type': 'multipart/form-data' },
  });
};

/** 断点续传完成 */
export const breakpointContinueFinish = (params: { fileName: string; fileMd5: string }) => {
  return request.post('/fileUploadAndDownload/breakpointContinueFinish', undefined, { params });
};

/** 移除分片 */
export const removeChunk = (data: any, params: { fileName: string; fileMd5: string; filePath: string }) => {
  return request.post('/fileUploadAndDownload/removeChunk', data, { params });
};

export default {
  getFileList,
  deleteFile,
  editFileName,
  importURL,
  uploadFile,
  findFile,
  breakpointContinue,
  breakpointContinueFinish,
  removeChunk,
};
