export interface ResultDetail {
  username: string;
  url: string;
  status: 'success' | 'error';
  message: string;
  errorDetails?: string;
}

export interface Subset {
  id: number;
  title: string;
  members: Record<string, 'RW' | 'R'>;
}

export interface ProjectDetails {
  projectId: number;
  name?: string;
  lang?: string; // Language code for API operations
  members: Record<string, string>;
  subsets: Subset[];
}

export interface GeIdResult {
  geIdAndLang: string;
  completionTime: Date;
  details: ResultDetail[];
}

export interface Submission {
  id: string; // submission_id from backend
  username: string; // joined usernames string (\n separated)
  geIdAndLang: string; // raw ge input
  timestamp: Date;
  results?: GeIdResult[];
}

export interface FileSystemItem {
  id: string;
  name: string;
  type: 'file' | 'folder';
  size?: string;
  modified: string;
  path?: string; // NAS path for download
}

export interface FileProgress {
  name: string;
  status: 'pending' | 'downloading' | 'completed' | 'failed';
  is_folder: boolean;
  size: number;
  progress?: number;
  downloaded?: number;
  total?: number;
}

export interface ProgressData {
  current_file?: string;
  current_file_index?: number;
  total_files?: number;
  current_file_progress?: number;
  current_file_downloaded?: number;
  current_file_total?: number;
  files_status?: FileProgress[];
}

export interface DownloadQueueItem {
    key: string;
    name: string;
    status: 'waiting' | 'processing' | 'done' | 'error' | 'pending';
    queuePosition?: number;
    geId?: string; // GE ID for tracking multiple concurrent jobs
    jobId?: string; // Job ID from backend for cancel operation
    mongoDbPath?: string; // MongoDB path to display (for mode 'api')
    sharingUrl?: string; // Sharing link URL (for mode 'sharing')
    destinationPath?: string; // NAS destination path
    progressData?: ProgressData; // Progress tracking for multi-file downloads
    mode?: 'api' | 'sharing'; // Download mode
    created_at?: string; // ISO timestamp for sorting (from first file in batch)
}

export interface DownloadedFile {
  name?: string;  // Legacy field
  file_name?: string;  // Actual field from backend
  local_path?: string;  // Full path with .zip for folders
  status?: 'success' | 'error';
  success?: boolean;  // Backend uses 'success' boolean
  message?: string;
  error_message?: string | null;  // Backend field
  is_directory?: boolean;
  file_size?: number;  // File size in bytes
}

export interface DownloadHistoryEntry {
  id: string;
  geIdAndLang: string;
  timestamp: string;
  files: DownloadedFile[];
  destinationPath: string;
  mongoDbPath?: string;
  totalFiles: number;
  successCount: number;
}

// ==================== FILE-CENTRIC DOWNLOAD TYPES ====================

/**
 * Single file download record (matches database schema).
 * Each row in 'downloads' table = one file being downloaded.
 */
export interface FileDownload {
  id: number;  // BIGSERIAL
  batch_id: string;  // Groups files downloaded together
  
  // File info
  ge_id: string;
  lang: string;
  file_name: string;
  file_path: string;
  
  // Mode & status
  mode: 'api' | 'sharing';
  status: 'pending' | 'downloading' | 'completed' | 'failed' | 'cancelled';
  
  // Paths
  source_path?: string;
  destination_path?: string;
  
  // Progress
  file_size?: number;
  downloaded_size: number;
  progress_percent: number;
  
  // Timestamps
  created_at: string;
  started_at?: string;
  completed_at?: string;
  
  // Error handling
  error_message?: string;
  retry_count: number;
  
  // Metadata
  sharing_id?: string;
  mongodb_path?: string;
}

/**
 * Grouped view of file downloads for UI display.
 * Groups files by batch_id for better UX.
 */
export interface DownloadBatch {
  batch_id: string;
  ge_id: string;
  lang: string;
  mode: 'api' | 'sharing';
  
  // Batch stats
  total_files: number;
  completed_files: number;
  failed_files: number;
  total_size: number;
  downloaded_size: number;
  
  // Batch status (derived)
  status: 'downloading' | 'completed' | 'partial_failed' | 'failed';
  
  // Timestamps
  created_at: string;
  started_at?: string;
  completed_at?: string;
  duration_seconds?: number;
  
  // Individual files
  files: FileDownload[];
}

// Legacy types - keep for backward compatibility during migration
export interface DownloadJob {
  id: string;
  ge_id: string;
  lang: string;
  status: 'pending' | 'processing' | 'completed' | 'failed' | 'cancelled';
  mode: 'api' | 'sharing';
  files: any[];
  destination_path: string;
  mongodb_path?: string;
  sharing_id?: string;
  queue_position?: number;
  error_message?: string;
  created_at: string;
  started_at?: string;
  completed_at?: string;
  progress_data?: ProgressData;
}
