import type { MultipartFile } from '@fastify/multipart'
import type { FastifyRequestWithViewer } from '@authentication/auth.guard'

import type { FileId, FileViewModel } from '@types'

export const DEFAULT_MAX_SIZE_BYTES = 30 * 1024 * 1024

export type UploadStorageMode = 'none' | 'local' | 's3'

export type UploadAllowedFileType = 'image' | 'excel' | 'json' | 'csv' | 'pdf' | 'text'

export type UploadGuardOptions = {
  maxFileSizeBytes?: number
  allowedMimeTypes?: string[]
  allowedFileExtensions?: string[]
  allowedFileTypes?: UploadAllowedFileType[]
  storageMode?: UploadStorageMode
}

export type UploadedFileDataPayload = {
  file: FileViewModel
  buffer: Buffer
  filename: string
  mimetype: string
  size: number
  fields: MultipartFile['fields']
}

export type UploadRequest = FastifyRequestWithViewer & {
  uploadedFileData?: UploadedFileDataPayload
}

export type StoredUploadRecord = {
  fileId: FileId
  location: string
  storageMode: UploadStorageMode
}
