import * as MediaLibrary from 'expo-media-library';

export type ExtendedAsset = MediaLibrary.Asset & {
    exif?: Record<string, any>;
};

// 上传任务类型
export type UploadTask = {
  file: File;
  metadata: {
    isCompressed: string;
    type: string;
    isThumbnail?: string;
    [key: string]: any;
  };
};

// 文件元数据信息
interface FileSize {
    value: number;
    unit: string;
}

interface FileMetadata {
    originalName: string;
    type: string;
    isCompressed: string;
    fileType: string;
}

// 后端返回的文件信息
interface FileInfo {
    file_id: number;
    name: string;
    size: FileSize;
    content_type: string; // 这里与 ConfirmUpload 的 content_type 定义不同，需要注意
    upload_time: string;
    storage_medium: string;
    file_path: string; // 这里与 ConfirmUpload 的 file_path 定义不同
    uploader_id: number;
    upload_status: string;
    deletion_status: string;
    metadata: FileMetadata;
}

// 上传队列项 - 作为唯一的类型定义
// 定义 EXIF 数据类型
export type ExifData = {
    GPSLatitude?: number | undefined;
    GPSLongitude?: number | undefined;
    GPSAltitude?: number | undefined;
    DateTimeOriginal?: string | undefined;
    Make?: string | undefined;
    Model?: string | undefined;
    ExposureTime?: number | undefined;
    FNumber?: number | undefined;
    ISOSpeedRatings?: number | undefined;
    FocalLength?: number | undefined;
    [key: string]: any;
};

// 默认的 EXIF 数据结构
export const defaultExifData: ExifData = {
    GPSLatitude: undefined,
    GPSLongitude: undefined,
    GPSAltitude: undefined,
    DateTimeOriginal: undefined,
    Make: undefined,
    Model: undefined,
    ExposureTime: undefined,
    FNumber: undefined,
    ISOSpeedRatings: undefined,
    FocalLength: undefined,
};

// 压缩图片可配置参数
export interface ImagesuploaderProps {
    children?: React.ReactNode;
    style?: import('react-native').StyleProp<import('react-native').ViewStyle>;
    onPickImage?: (file: File, exifData: ExifData) => void;
    compressQuality?: number;
    maxWidth?: number;
    maxHeight?: number;
    preserveExif?: boolean;
    uploadOriginal?: boolean;
    onUploadComplete?: (result: FileUploadItem[]) => void;
    onProgress?: (progress: any) => void; // TODO: Define a proper type for progress
    multipleChoice?: boolean;
    fileType?: any[]; // TODO: Use MediaType from expo-image-picker
    showPreview?: boolean;
}

export interface FileUploadItem {
    id: string;
    uri: string; // 用于本地展示的资源URI
    name: string;
    progress: number;
    status: 'pending' | 'uploading' | 'success' | 'error'; // 统一状态
    error?: string | null;
    previewUrl: string; // 预览URL
    file?: File;
    type: 'image' | 'video';
    thumbnail?: string;  // 缩略图URL
    thumbnailFile?: File; // 缩略图文件对象
    originalFile?: FileInfo // 上传后返回的文件信息
}

// 确认上传返回
export type ConfirmUpload = {
  file_id: string;
  upload_url: string;
  name: string;
  size: number;
  content_type: string;
  file_path: string;
};

// 上传结果
export type UploadResult = {
  originalUrl?: string;
  compressedUrl: string;
  file: File | null;
  exif: any;
  originalFile: ConfirmUpload;
  compressedFile: ConfirmUpload;
  thumbnail: string;
  thumbnailFile: File;
};

// 上传URL响应类型
export type UploadUrlResponse = {
  upload_url: string;
  file_id: string;
};