/**
 * File Storage Types for LocalGreenChain
 * Agent 3: File Upload & Storage System
 */

export type StorageProvider = 's3' | 'r2' | 'minio' | 'local';

export type FileCategory = 'plant-photo' | 'certificate' | 'document' | 'report' | 'avatar';

export type ImageSize = 'thumbnail' | 'small' | 'medium' | 'large' | 'original';

export interface StorageConfig {
  provider: StorageProvider;
  bucket: string;
  region?: string;
  endpoint?: string;
  accessKeyId?: string;
  secretAccessKey?: string;
  publicUrl?: string;
  localPath?: string;
}

export interface FileMetadata {
  id: string;
  filename: string;
  originalName: string;
  mimeType: string;
  size: number;
  category: FileCategory;
  uploadedBy?: string;
  plantId?: string;
  farmId?: string;
  url: string;
  thumbnailUrl?: string;
  urls?: Record<ImageSize, string>;
  width?: number;
  height?: number;
  exifData?: ExifData;
  createdAt: Date;
  updatedAt: Date;
  deletedAt?: Date;
}

export interface ExifData {
  make?: string;
  model?: string;
  dateTaken?: Date;
  gpsLatitude?: number;
  gpsLongitude?: number;
  orientation?: number;
}

export interface UploadOptions {
  category: FileCategory;
  plantId?: string;
  farmId?: string;
  userId?: string;
  generateThumbnails?: boolean;
  maxSizeBytes?: number;
  allowedMimeTypes?: string[];
}

export interface UploadResult {
  success: boolean;
  file?: FileMetadata;
  error?: string;
  presignedUrl?: string;
}

export interface PresignedUrlRequest {
  filename: string;
  contentType: string;
  category: FileCategory;
  expiresIn?: number;
}

export interface PresignedUrlResponse {
  uploadUrl: string;
  fileKey: string;
  publicUrl: string;
  expiresAt: Date;
}

export interface ImageProcessingOptions {
  generateThumbnails?: boolean;
  convertToWebP?: boolean;
  extractExif?: boolean;
  maxWidth?: number;
  maxHeight?: number;
  quality?: number;
}

export interface ThumbnailConfig {
  size: ImageSize;
  width: number;
  height: number;
  fit: 'cover' | 'contain' | 'fill' | 'inside' | 'outside';
}

export const THUMBNAIL_CONFIGS: Record<ImageSize, ThumbnailConfig> = {
  thumbnail: { size: 'thumbnail', width: 150, height: 150, fit: 'cover' },
  small: { size: 'small', width: 300, height: 300, fit: 'inside' },
  medium: { size: 'medium', width: 600, height: 600, fit: 'inside' },
  large: { size: 'large', width: 1200, height: 1200, fit: 'inside' },
  original: { size: 'original', width: 0, height: 0, fit: 'inside' },
};

export const ALLOWED_IMAGE_TYPES = [
  'image/jpeg',
  'image/png',
  'image/gif',
  'image/webp',
  'image/heic',
  'image/heif',
];

export const ALLOWED_DOCUMENT_TYPES = [
  'application/pdf',
  'application/msword',
  'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
];

export const DEFAULT_MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB
export const DEFAULT_MAX_IMAGE_SIZE = 5 * 1024 * 1024; // 5MB

export interface StorageProviderInterface {
  upload(key: string, buffer: Buffer, contentType: string): Promise<string>;
  delete(key: string): Promise<boolean>;
  getSignedUrl(key: string, expiresIn?: number): Promise<string>;
  getPresignedUploadUrl(key: string, contentType: string, expiresIn?: number): Promise<string>;
  exists(key: string): Promise<boolean>;
  getPublicUrl(key: string): string;
}
