/**
 * 文件管理 API
 */
import { request } from './request'

export interface FileItem {
  id: number
  file_name: string
  original_name: string
  file_path: string
  file_size: number
  file_type: string
  file_ext: string
  uploader_id: number
  uploader_name?: string
  upload_time: string
  thumbnail_path?: string
  share_code?: string
  share_expire_time?: string
  download_count: number
  remark?: string
  download_url?: string
  preview_url?: string
  share_url?: string
}

export interface FileUploadResponse {
  id: number
  file_name: string
  original_name: string
  file_size: number
  file_type: string
  file_path: string
  download_url: string
  preview_url?: string
  message: string
}

export interface FileShareResponse {
  share_code: string
  share_url: string
  expire_time: string
}

export interface FileStatistics {
  total_files: number
  total_size: number
  total_size_human: string
  type_distribution: Record<string, number>
  upload_today: number
  upload_this_week: number
  upload_this_month: number
  top_uploaders: Array<{
    uploader_id: number
    count: number
  }>
}

export interface FileQueryParams {
  keyword?: string
  file_type?: string
  uploader_id?: number
  start_date?: string
  end_date?: string
  page?: number
  page_size?: number
}

/**
 * 上传文件
 */
export function uploadFile(file: File, data?: { remark?: string }) {
  const formData = new FormData()
  formData.append('file', file)
  if (data?.remark) {
    formData.append('remark', data.remark)
  }

  return request.post<FileUploadResponse>('/files/upload', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

/**
 * 获取文件列表
 */
export function getFileList(params?: FileQueryParams) {
  return request.get<FileItem[]>('/files', { params })
}

/**
 * 获取文件详情
 */
export function getFileDetail(id: number) {
  return request.get<FileItem>(`/files/${id}`)
}

/**
 * 下载文件
 */
export function downloadFile(id: number) {
  return request.get(`/files/${id}/download`, {
    responseType: 'blob'
  })
}

/**
 * 预览文件
 */
export function previewFile(id: number) {
  return request.get(`/files/${id}/preview`, {
    responseType: 'blob'
  })
}

/**
 * 更新文件信息
 */
export function updateFile(id: number, data: { remark?: string }) {
  return request.put<FileItem>(`/files/${id}`, data)
}

/**
 * 删除文件
 */
export function deleteFile(id: number) {
  return request.delete(`/files/${id}`)
}

/**
 * 批量删除文件
 */
export function deleteFilesBatch(fileIds: number[]) {
  return request.delete('/files/batch', { data: { file_ids: fileIds } })
}

/**
 * 生成分享链接
 */
export function createShareLink(id: number, expireDays: number = 7) {
  return request.post<FileShareResponse>(`/files/${id}/share`, {
    expire_days: expireDays
  })
}

/**
 * 访问分享文件
 */
export function accessSharedFile(shareCode: string) {
  return request.get(`/files/share/${shareCode}`, {
    responseType: 'blob'
  })
}

/**
 * 获取文件统计
 */
export function getFileStatistics(uploaderId?: number) {
  return request.get<FileStatistics>('/files/statistics', {
    params: uploaderId ? { uploader_id: uploaderId } : undefined
  })
}

/**
 * 初始化分片上传
 */
export function initChunkUpload(data: {
  file_name: string
  file_size: number
  file_type: string
  total_chunks: number
  file_hash?: string
}) {
  return request.post<{ upload_id: string; message: string }>('/files/chunks/init', data)
}

/**
 * 上传分片
 */
export function uploadChunk(uploadId: string, chunkIndex: number, chunk: Blob) {
  const formData = new FormData()
  formData.append('upload_id', uploadId)
  formData.append('chunk_index', chunkIndex.toString())
  formData.append('chunk', chunk)

  return request.post('/files/chunks/upload', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

/**
 * 完成分片上传
 */
export function completeChunkUpload(data: {
  upload_id: string
  file_name: string
  file_hash?: string
}) {
  return request.post<FileUploadResponse>('/files/chunks/complete', data)
}
