// =============================================================================
// Core Types for K-Convert
// =============================================================================

// -----------------------------------------------------------------------------
// Format Definitions
// -----------------------------------------------------------------------------

// Image formats
export type ImageFormat = 
  | 'jpeg' | 'png' | 'webp' | 'gif' | 'bmp' | 'tiff' | 'heic' | 'ico'
  | 'avif' | 'jxl' | 'svg' | 'psd' | 'raw' | 'tga';

// Audio formats
export type AudioFormat = 
  | 'mp3' | 'wav' | 'ogg' | 'm4a' | 'flac'
  | 'aac' | 'aiff' | 'alac' | 'wma' | 'opus' | 'm4r' | 'amr';

export type SupportedFormat = ImageFormat | AudioFormat;

export type FormatCategory = 'image' | 'audio';

export interface FormatMetadata {
  extension: string;
  mimeType: string;
  displayName: string;
  category: FormatCategory;
}

export const FORMAT_METADATA: Record<SupportedFormat, FormatMetadata> = {
  // Images - Common
  jpeg: { extension: 'jpg', mimeType: 'image/jpeg', displayName: 'JPEG', category: 'image' },
  png: { extension: 'png', mimeType: 'image/png', displayName: 'PNG', category: 'image' },
  webp: { extension: 'webp', mimeType: 'image/webp', displayName: 'WebP', category: 'image' },
  gif: { extension: 'gif', mimeType: 'image/gif', displayName: 'GIF', category: 'image' },
  bmp: { extension: 'bmp', mimeType: 'image/bmp', displayName: 'BMP', category: 'image' },
  tiff: { extension: 'tiff', mimeType: 'image/tiff', displayName: 'TIFF', category: 'image' },
  heic: { extension: 'heic', mimeType: 'image/heic', displayName: 'HEIC', category: 'image' },
  ico: { extension: 'ico', mimeType: 'image/x-icon', displayName: 'ICO', category: 'image' },
  // Images - New formats
  avif: { extension: 'avif', mimeType: 'image/avif', displayName: 'AVIF', category: 'image' },
  jxl: { extension: 'jxl', mimeType: 'image/jxl', displayName: 'JPEG XL', category: 'image' },
  svg: { extension: 'svg', mimeType: 'image/svg+xml', displayName: 'SVG', category: 'image' },
  psd: { extension: 'psd', mimeType: 'image/vnd.adobe.photoshop', displayName: 'PSD', category: 'image' },
  raw: { extension: 'raw', mimeType: 'image/x-raw', displayName: 'RAW', category: 'image' },
  tga: { extension: 'tga', mimeType: 'image/x-tga', displayName: 'TGA', category: 'image' },
  // Audio - Common
  mp3: { extension: 'mp3', mimeType: 'audio/mpeg', displayName: 'MP3', category: 'audio' },
  wav: { extension: 'wav', mimeType: 'audio/wav', displayName: 'WAV', category: 'audio' },
  ogg: { extension: 'ogg', mimeType: 'audio/ogg', displayName: 'OGG Vorbis', category: 'audio' },
  m4a: { extension: 'm4a', mimeType: 'audio/mp4', displayName: 'M4A', category: 'audio' },
  flac: { extension: 'flac', mimeType: 'audio/flac', displayName: 'FLAC', category: 'audio' },
  // Audio - New formats
  aac: { extension: 'aac', mimeType: 'audio/aac', displayName: 'AAC', category: 'audio' },
  aiff: { extension: 'aiff', mimeType: 'audio/aiff', displayName: 'AIFF', category: 'audio' },
  alac: { extension: 'm4a', mimeType: 'audio/x-m4a', displayName: 'ALAC', category: 'audio' },
  wma: { extension: 'wma', mimeType: 'audio/x-ms-wma', displayName: 'WMA', category: 'audio' },
  opus: { extension: 'opus', mimeType: 'audio/opus', displayName: 'Opus', category: 'audio' },
  m4r: { extension: 'm4r', mimeType: 'audio/x-m4r', displayName: 'M4R (Ringtone)', category: 'audio' },
  amr: { extension: 'amr', mimeType: 'audio/amr', displayName: 'AMR', category: 'audio' },
};

// MIME type to format reverse lookup
export const MIME_TO_FORMAT: Record<string, SupportedFormat> = {
  // Images
  'image/jpeg': 'jpeg',
  'image/png': 'png',
  'image/webp': 'webp',
  'image/gif': 'gif',
  'image/bmp': 'bmp',
  'image/tiff': 'tiff',
  'image/heic': 'heic',
  'image/heif': 'heic',
  'image/x-icon': 'ico',
  'image/vnd.microsoft.icon': 'ico',
  'image/avif': 'avif',
  'image/jxl': 'jxl',
  'image/svg+xml': 'svg',
  'image/vnd.adobe.photoshop': 'psd',
  'image/x-raw': 'raw',
  'image/x-tga': 'tga',
  'image/x-targa': 'tga',
  // Audio
  'audio/mpeg': 'mp3',
  'audio/mp3': 'mp3',
  'audio/wav': 'wav',
  'audio/wave': 'wav',
  'audio/x-wav': 'wav',
  'audio/ogg': 'ogg',
  'audio/mp4': 'm4a',
  'audio/x-m4a': 'm4a',
  'audio/flac': 'flac',
  'audio/x-flac': 'flac',
  'audio/aac': 'aac',
  'audio/aiff': 'aiff',
  'audio/x-aiff': 'aiff',
  'audio/x-ms-wma': 'wma',
  'audio/opus': 'opus',
  'audio/x-m4r': 'm4r',
  'audio/amr': 'amr',
};

// -----------------------------------------------------------------------------
// Conversion Types
// -----------------------------------------------------------------------------

export type ConversionStatus = 'pending' | 'processing' | 'complete' | 'error';

export interface ConversionOptions {
  quality?: number; // 0-100 for lossy formats
  bitrate?: number; // kbps for audio
}

export interface ConversionRequest {
  file: File;
  targetFormat: SupportedFormat;
  options?: ConversionOptions;
}

export interface QueueItem {
  id: string;
  file: File;
  inputFormat: SupportedFormat;
  targetFormat: SupportedFormat;
  status: ConversionStatus;
  progress: number; // 0-100
  error?: string;
  result?: Blob;
  options?: ConversionOptions;
}

// -----------------------------------------------------------------------------
// Adapter Interface
// -----------------------------------------------------------------------------

export interface IFileConverter {
  /** Unique identifier for this adapter */
  readonly name: string;

  /** Returns true if this converter handles the input→output conversion */
  supports(inputFormat: SupportedFormat, outputFormat: SupportedFormat): boolean;

  /** Perform the conversion, optionally reporting progress */
  convert(
    request: ConversionRequest,
    onProgress?: (progress: number) => void
  ): Promise<Blob>;
}