/**
 * TVAI (Topaz Video AI) 相关类型定义
 */

export interface TvaiTask {
  id: string;
  task_type: string;
  input_path: string;
  output_path: string;
  status: TvaiTaskStatus;
  progress: number;
  error_message?: string;
  created_at: string;
  started_at?: string;
  completed_at?: string;
  processing_time?: number; // milliseconds
}

export enum TvaiTaskStatus {
  Pending = "Pending",
  Processing = "Processing",
  Completed = "Completed",
  Failed = "Failed",
  Cancelled = "Cancelled",
}

export interface VideoInfo {
  width: number;
  height: number;
  fps: number;
  duration: number; // in seconds
  codec: string;
  bitrate?: number;
  file_size: number;
}

export interface ImageInfo {
  width: number;
  height: number;
  format: string;
  color_depth: number;
  file_size: number;
}

export interface GpuInfo {
  available: boolean;
  cuda_available: boolean;
  opencl_available: boolean;
  device_name?: string;
  memory_gb?: number;
}

export interface FfmpegInfo {
  system_available: boolean;
  topaz_available: boolean;
  system_version?: string;
  topaz_version?: string;
  system_path?: string;
  topaz_path?: string;
}

export type UpscaleModel = 
  | "aaa-9" | "ahq-12" | "alq-13" | "alqs-2" | "amq-13" | "amqs-2"
  | "ghq-5" | "iris-2" | "iris-3" | "nyx-3" | "prob-4" | "thf-4"
  | "thd-3" | "thm-2" | "rhea-1" | "rxl-1";

export type InterpolationModel = 
  | "apo-8" | "apf-1" | "chr-2" | "chf-3";

export type QualityPreset =
  | "fast" | "balanced" | "high_quality" | "maximum";

export type ImageFormat = 
  | "png" | "jpg" | "jpeg" | "tiff" | "bmp";

export interface TvaiConfig {
  topaz_path?: string;
  use_gpu?: boolean;
  temp_dir?: string;
}

// 音频处理模式
export type AudioMode =
  | { type: 'keep' }
  | { type: 'remove' }
  | { type: 'reencode'; codec: string; bitrate: number };

export interface VideoUpscaleParams {
  // 基础参数
  scale_factor: number;
  model: UpscaleModel;
  compression: number;
  blend: number;
  quality_preset: QualityPreset;

  // 高级TVAI参数
  preblur?: number;        // 预模糊 (0-100)
  noise?: number;          // 噪点减少 (0-100)
  details?: number;        // 细节恢复 (0-100)
  halo?: number;           // 光晕减少 (0-100)
  blur?: number;           // 模糊减少 (0-100)
  estimate?: number;       // 估算质量 (1-20)
  device?: number;         // 设备ID (-2=auto, -1=CPU, 0+=GPU)
  vram?: number;           // VRAM使用 (0-1)
  instances?: number;      // 实例数量 (1-4)

  // 输出尺寸控制
  target_width?: number;   // 目标宽度
  target_height?: number;  // 目标高度
  maintain_aspect?: boolean; // 保持宽高比
  pad_color?: string;      // 填充颜色

  // 编码参数
  codec?: string;          // 编码器
  profile?: string;        // 编码配置文件
  pixel_format?: string;   // 像素格式
  gop_size?: number;       // GOP大小
  preset?: string;         // 编码预设
  tune?: string;           // 调优选项
  rate_control?: string;   // 码率控制模式
  quality_param?: number;  // 质量参数 (CRF/QP)
  lookahead?: number;      // 前瞻帧数
  spatial_aq?: boolean;    // 空间自适应量化
  aq_strength?: number;    // AQ强度
  bitrate?: number;        // 目标码率 (0=VBR)
  buffer_frames?: number;  // 缓冲帧数

  // 音频处理
  audio_mode?: AudioMode;  // 音频处理模式

  // 元数据
  preserve_metadata?: boolean; // 保留元数据
  custom_metadata?: string;    // 自定义元数据
}

export interface ImageUpscaleParams {
  scale_factor: number;
  model: UpscaleModel;
  compression: number;
  blend: number;
  output_format: ImageFormat;
}

export interface InterpolationParams {
  input_fps: number;
  multiplier: number;
  model: InterpolationModel;
  target_fps?: number;
}

/**
 * TVAI 服务接口
 */
export interface TvaiService {
  // 系统检测
  detectTopazInstallation(): Promise<string | null>;
  detectGpuSupport(): Promise<GpuInfo>;
  detectFfmpeg(): Promise<FfmpegInfo>;
  
  // 配置
  initializeTvaiConfig(config: TvaiConfig): Promise<void>;
  
  // 信息获取
  getVideoInfo(videoPath: string): Promise<VideoInfo>;
  getImageInfo(imagePath: string): Promise<ImageInfo>;
  estimateProcessingTime(
    inputPath: string,
    scaleFactor?: number
  ): Promise<number>;
  
  // 快速处理
  quickUpscaleVideo(
    inputPath: string,
    outputPath: string,
    scaleFactor: number,
    model?: UpscaleModel
  ): Promise<string>;
  
  quickUpscaleImage(
    inputPath: string,
    outputPath: string,
    scaleFactor: number
  ): Promise<string>;

  // 快速插帧
  quickInterpolateVideo(
    inputPath: string,
    outputPath: string,
    multiplier: number,
    inputFps: number
  ): Promise<string>;
  
  // 自动增强
  autoEnhanceVideo(
    inputPath: string, 
    outputPath: string
  ): Promise<string>;
  
  autoEnhanceImage(
    inputPath: string, 
    outputPath: string
  ): Promise<string>;
  
  // 高级处理
  upscaleVideoAdvanced(
    inputPath: string,
    outputPath: string,
    scaleFactor: number,
    model: UpscaleModel,
    compression: number,
    blend: number,
    qualityPreset: QualityPreset
  ): Promise<string>;
  
  upscaleImageAdvanced(
    inputPath: string,
    outputPath: string,
    scaleFactor: number,
    model: UpscaleModel,
    compression: number,
    blend: number,
    outputFormat: ImageFormat
  ): Promise<string>;
  
  // 任务管理
  getTvaiTaskStatus(taskId: string): Promise<TvaiTask | null>;
  getAllTvaiTasks(): Promise<TvaiTask[]>;
  cancelTvaiTask(taskId: string): Promise<void>;
  cleanupCompletedTvaiTasks(): Promise<number>;
}

/**
 * TVAI 事件类型
 */
export interface TvaiEvents {
  'tvai_task_created': string; // task_id
  'tvai_task_updated': string; // task_id
}

/**
 * 预设配置
 */
export const TVAI_PRESETS = {
  // 视频放大模型预设
  VIDEO_UPSCALE_MODELS: {
    OLD_VIDEO: { model: "thf-4" as UpscaleModel, compression: 0.3, blend: 0.2 },
    GAME_CONTENT: { model: "ghq-5" as UpscaleModel, compression: 0.0, blend: 0.0 },
    ANIMATION: { model: "iris-3" as UpscaleModel, compression: -0.1, blend: 0.1 },
    PORTRAIT: { model: "nyx-3" as UpscaleModel, compression: -0.2, blend: 0.1 },
    GENERAL: { model: "iris-3" as UpscaleModel, compression: 0.0, blend: 0.0 },
  },
  
  // 图片放大模型预设
  IMAGE_UPSCALE_MODELS: {
    PHOTO: { model: "iris-3" as UpscaleModel, compression: 0.0, blend: 0.1 },
    ARTWORK: { model: "thf-4" as UpscaleModel, compression: -0.1, blend: 0.0 },
    SCREENSHOT: { model: "ghq-5" as UpscaleModel, compression: 0.0, blend: 0.0 },
    PORTRAIT: { model: "nyx-3" as UpscaleModel, compression: -0.2, blend: 0.1 },
    GENERAL: { model: "iris-3" as UpscaleModel, compression: 0.0, blend: 0.0 },
  },
  
  // 插帧模型预设
  INTERPOLATION_MODELS: {
    SLOW_MOTION: { model: "apo-8" as InterpolationModel },
    ANIMATION: { model: "chr-2" as InterpolationModel },
    GENERAL: { model: "apo-8" as InterpolationModel },
  },
  
  // 质量预设
  QUALITY_PRESETS: {
    FAST: "fast" as QualityPreset,
    BALANCED: "balanced" as QualityPreset,
    HIGH_QUALITY: "high_quality" as QualityPreset,
    MAXIMUM: "maximum" as QualityPreset,
  },
  
  // 输出格式
  OUTPUT_FORMATS: {
    PNG: "png" as ImageFormat,
    JPG: "jpg" as ImageFormat,
    TIFF: "tiff" as ImageFormat,
    BMP: "bmp" as ImageFormat,
  },
} as const;

/**
 * 常用的放大倍数
 */
export const SCALE_FACTORS = [1.5, 2.0, 2.5, 3.0, 4.0] as const;

/**
 * 常用的插帧倍数
 */
export const INTERPOLATION_MULTIPLIERS = [1.5, 2.0, 2.5, 3.0, 4.0, 8.0] as const;
