/**
 * 文件管理组件类型定义
 */

import { Ref } from 'vue'

/**
 * 文件项
 */
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
  is_deleted: number
  deleted_at?: string
  deleted_by?: number
  remark?: string
  created_at: string
  updated_at: string
  download_url?: string
  preview_url?: string
  share_url?: string
}

/**
 * 图片项
 */
export interface ImageItem {
  url: string
  name?: 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
}

/**
 * 文件上传组件Props
 */
export interface FileUploadProps {
  action?: string
  showProgress?: boolean
  showImagePreview?: boolean
  drag?: boolean
  multiple?: boolean
  autoUpload?: boolean
  limit?: number
  maxSize?: number
  accept?: string
  data?: Record<string, any>
}

/**
 * 文件上传组件Emits
 */
export interface FileUploadEmits {
  (e: 'update:file-list', files: any[]): void
  (e: 'upload-success', response: FileUploadResponse, file: any): void
  (e: 'upload-error', error: Error, file: any): void
  (e: 'upload-progress', event: any, file: any): void
}

/**
 * 图片预览组件Props
 */
export interface ImagePreviewProps {
  visible: boolean
  images: ImageItem[]
  initialIndex?: number
  showThumbnails?: boolean
}

/**
 * 图片预览组件Emits
 */
export interface ImagePreviewEmits {
  (e: 'update:visible', value: boolean): void
  (e: 'change', index: number): void
}

/**
 * 文件列表组件Props
 */
export interface FileListProps {
  viewMode?: 'table' | 'grid'
  showUpload?: boolean
  selectable?: boolean
}

/**
 * 分片上传初始化参数
 */
export interface ChunkUploadInitParams {
  file_name: string
  file_size: number
  file_type: string
  total_chunks: number
  file_hash?: string
}

/**
 * 分片上传初始化响应
 */
export interface ChunkUploadInitResponse {
  upload_id: string
  message: string
}

/**
 * 分片上传完成参数
 */
export interface ChunkUploadCompleteParams {
  upload_id: string
  file_name: string
  file_hash?: string
}

/**
 * 文件验证选项
 */
export interface FileValidateOptions {
  allowedTypes?: string[]
  maxSize?: number
  maxCount?: number
}

/**
 * 文件验证结果
 */
export interface FileValidateResult {
  valid: boolean
  errors: string[]
}

/**
 * 文件类型枚举
 */
export enum FileType {
  IMAGE = 'image',
  DOCUMENT = 'document',
  ARCHIVE = 'archive',
  VIDEO = 'video',
  AUDIO = 'audio',
  OTHER = 'other'
}

/**
 * 视图模式枚举
 */
export enum ViewMode {
  TABLE = 'table',
  GRID = 'grid'
}

/**
 * 文件状态枚举
 */
export enum FileStatus {
  UPLOADING = 'uploading',
  SUCCESS = 'success',
  ERROR = 'error',
  REMOVED = 'removed'
}

/**
 * 上传任务状态
 */
export interface UploadTask {
  uid: string
  name: string
  size: number
  type: string
  status: FileStatus
  percentage: number
  url?: string
  response?: FileUploadResponse
  error?: Error
}

/**
 * 文件分享创建参数
 */
export interface FileShareCreateParams {
  expire_days: number
}

/**
 * 文件批量删除参数
 */
export interface FileBatchDeleteParams {
  file_ids: number[]
}

/**
 * 文件更新参数
 */
export interface FileUpdateParams {
  remark?: string
}

/**
 * 缩略图生成选项
 */
export interface ThumbnailOptions {
  width?: number
  height?: number
  quality?: number
}

/**
 * 图片压缩选项
 */
export interface ImageCompressOptions {
  quality?: number
  maxWidth?: number
  maxHeight?: number
}

/**
 * 文件服务实例
 */
export interface FileServiceInstance {
  uploadFile(file: File, data?: { remark?: string }): Promise<FileUploadResponse>
  getFileList(params?: FileQueryParams): Promise<FileItem[]>
  getFileDetail(id: number): Promise<FileItem>
  downloadFile(id: number): Promise<Blob>
  previewFile(id: number): Promise<Blob>
  updateFile(id: number, data: FileUpdateParams): Promise<FileItem>
  deleteFile(id: number): Promise<void>
  deleteFilesBatch(fileIds: number[]): Promise<void>
  createShareLink(id: number, expireDays?: number): Promise<FileShareResponse>
  accessSharedFile(shareCode: string): Promise<Blob>
  getFileStatistics(uploaderId?: number): Promise<FileStatistics>
  initChunkUpload(data: ChunkUploadInitParams): Promise<ChunkUploadInitResponse>
  uploadChunk(uploadId: string, chunkIndex: number, chunk: Blob): Promise<void>
  completeChunkUpload(data: ChunkUploadCompleteParams): Promise<FileUploadResponse>
}

/**
 * 文件工具函数集合
 */
export interface FileUtils {
  formatFileSize(bytes: number): string
  formatDateTime(dateString: string, format?: string): string
  getFileExtension(filename: string): string
  getFileNameWithoutExtension(filename: string): string
  isImage(mimeType: string): boolean
  isPDF(mimeType: string): boolean
  isDocument(mimeType: string): boolean
  isArchive(mimeType: string): boolean
  getFileTypeIcon(mimeType: string): string
  downloadFile(url: string, filename?: string): Promise<void>
  previewFile(url: string): void
  validateFileType(file: File, allowedTypes: string[]): boolean
  validateFileSize(file: File, maxSize: number): boolean
  validateFiles(files: File[], options: FileValidateOptions): FileValidateResult
  compressImage(file: File, quality?: number, maxWidth?: number, maxHeight?: number): Promise<Blob>
  createThumbnail(file: File, width?: number, height?: number): Promise<string>
  calculateFileHash(file: File): Promise<string>
  generateUniqueFilename(originalFilename: string): string
}
