/**
 * 全局业务类型定义
 */

/** 通用分页/列表响应暂用数组，后端未分页 */

// ============ 模型管理 ============
export type ModelType = 'LLM' | 'CV' | 'NLP' | 'Embedding' | 'Other'
export type ModelPurpose = 'training' | 'inference' | 'evaluation'
export type ModelSource = 'local' | 'api' | 'online'

export interface TrainMethod {
  name: string
}

/** 数据库配置模型 */
export interface ModelItem {
  id: number | string
  name: string
  type: ModelType | string
  purpose: ModelPurpose | string
  model_source: ModelSource | string
  description?: string
  path?: string
  api_url?: string
  api_key?: string
  online_model_name?: string
  create_time?: string
}

/** 已训练模型 */
export interface TrainedModel {
  id: number | string
  name: string
  train_methods?: TrainMethod[]
  base_model_path?: string
  create_time?: string
  merged?: boolean
  merging?: boolean
  merged_path?: string
}

/** 创建/编辑模型请求体 */
export interface ModelForm {
  id?: number | string
  name: string
  type: ModelType | string
  purpose: ModelPurpose | string
  model_source: ModelSource | string
  description?: string
  path?: string
  api_url?: string
  api_key?: string
  online_model_name?: string
}

// ============ 数据集 ============
export type DatasetType = 'train' | 'test' | 'eval' | 'val' | 'other'
export type DatasetStorage = 'local' | 'cloud' | 'minio'
export type DatasetSource = 'upload' | 'task'

export interface DatasetFile {
  id?: string
  name: string
  size?: string | number
  [k: string]: any
}

export interface DatasetItem {
  id: number | string
  name: string
  type: DatasetType | string
  storage_type: DatasetStorage | string
  source?: DatasetSource
  task_id?: string | number
  size?: string | number
  count?: number
  description?: string
  create_time?: string
  files?: DatasetFile[]
}

// ============ 训练任务 ============
export type TrainType = 'SFT' | 'DPO' | 'CPT'
export type TrainMethodType = 'lora' | 'full'
export type TaskStatus = 'pending' | 'running' | 'completed' | 'failed' | string

export interface FineTuneTask {
  id: number | string
  name: string
  description?: string
  status: TaskStatus
  train_type: TrainType | string
  train_method?: TrainMethodType | string
  template?: string
  base_model: number | string
  train_dataset_id?: number | string
  auto_merge?: boolean
  output_model_name?: string
  gpus?: number[]
  batch_size?: number
  learning_rate?: number
  n_epochs?: number
  save_steps?: number
  lr_scheduler_type?: string
  max_length?: number
  warmup_ratio?: number
  weight_decay?: number
  lora_alpha?: number
  lora_dropout?: number
  lora_rank?: number
  progress?: number
  process_id?: number
  train_duration?: string
  create_time?: string
}

export interface TrainingProgress {
  status?: string
  progress?: number
  step?: string
  speed?: string
  eta?: string
}

// ============ 模型推理 / 对比 (model-compare) ============
export interface LoadedModel {
  model_id?: number | string
  model_name?: string
  status?: string
  pid?: number
  port?: number
}

export interface CompareTask {
  id: number | string
  model_name?: string
  name?: string
  description?: string
  status?: string
  models?: CompareModelRef[] | string
  load_status?: string | { loaded_models: LoadedModel[] }
  create_time?: string
}

export interface CompareModelRef {
  model_id: number | string
  model_name: string
  model_path: string
  gpu_id: number
  source?: string
  port?: number
}

// ============ 模型评测 ============
export type EvalType = 'custom' | 'baseline'
export type DimensionType = 'classification' | 'metric' | 'text_similarity'
export type EvalErrorType = '混淆' | '不完整' | '格式偏差' | '幻觉' | '其他'

export interface EvalTask {
  id: number | string
  eval_task_name?: string
  model_name?: string
  model_id?: number | string
  dataset?: string
  metric?: string
  score?: number
  status?: string
  create_time?: string
}

/** 单个测试样本的评测结果 */
export interface EvalSampleResult {
  id: number | string
  index: number
  input: string
  reference_answer?: string
  model_output: string
  score: number | null
  max_score?: number
  passed?: boolean
  status: 'completed' | 'pending' | 'failed'
  judgement?: '正确' | '部分正确' | '错误'
  evaluation_reason?: string
  error_type?: EvalErrorType
  dimension_scores?: Array<{
    name: string
    score: number
    max_score: number
  }>
}

/** 评测任务详情，包含逐样本结果和综合评价 */
export interface EvalTaskDetail extends EvalTask {
  evaluator_model?: string
  sample_count: number
  completed_count: number
  passed_count?: number
  completed_time?: string
  overall_score: number
  overall_score_max: number
  overall_evaluation: string
  improvement_suggestions: string[]
  dimension_summary: Array<{
    name: string
    score: number
    max_score: number
    pass_rate: number
  }>
  samples: EvalSampleResult[]
}

export interface Dimension {
  id: number | string
  name: string
  type: DimensionType | string
  description?: string
  eval_model?: string
  eval_method?: string | string[]
  eval_prompt?: string
  is_active?: boolean
  is_default?: boolean
  bleu_n?: number
  output_precision?: number
  score_min?: number
  score_max?: number
  pass_threshold?: number
  create_time?: string
}

// ============ 系统监控 ============
export interface GpuInfo {
  name: string
  gpu_percent: number
  memory_used_gb: number
  memory_total_gb: number
  temperature: number
  power_w: number
  fan_speed?: number
  clock_mhz?: number
  driver_version?: string
}

export interface SystemInfo {
  cpu?: {
    percent: number
    cores: number
    percents?: number[]
  }
  memory?: {
    used_gb: number
    total_gb: number
    percent: number
    available_gb: number
    cached_gb: number
  }
  disk?: {
    used_gb: number
    total_gb: number
    percent: number
  }
  gpu?: GpuInfo[]
  network?: {
    download_mb?: number
    upload_mb?: number
  }
  system?: {
    uptime_seconds?: number
    process_count?: number
    os?: string
  }
}

export interface HealthMetrics {
  cpu_percent?: number
  memory_percent?: number
  disk_percent?: number
}

// ============ 日志 ============
export interface LogFile {
  file: string
  name: string
  size: string
  pid?: number
  date?: string
}

export interface LogContent {
  content: string
  file: string
  size: string
}

export interface TrainingLogFile extends LogFile {
  pid?: number
}

// ============ 其他工具 ============
export interface CustomTool {
  id: string
  name: string
  description: string
  url: string
  icon: string
}
