/**
 * Training Metrics Types & Utilities
 *
 * Shared types for training dashboard components.
 */

/** Training data point for charts */
export interface TrainingDataPoint {
  epoch: number
  step: number
  loss: number
  val_loss?: number
  learning_rate: number
  timestamp: number
}

/** Training job status */
export interface TrainingStatus {
  job_id: string
  status: 'queued' | 'running' | 'completed' | 'failed' | 'cancelled'
  progress: number // 0-100
  current_epoch: number
  total_epochs: number
  current_step: number
  total_steps: number
  elapsed_time_ms: number
  estimated_remaining_ms: number
  metrics: {
    loss: number
    val_loss?: number
    accuracy?: number
    learning_rate: number
  }
  history: TrainingDataPoint[]
  error?: string
}

export interface TrainingMetricsProps {
  /** API base URL */
  apiBase: string
  /** Job ID to track (null for simulation mode) */
  jobId?: string | null
  /** Simulate training progress for demo */
  simulateMode?: boolean
  /** Custom class names */
  className?: string
  /** Callback when training completes */
  onComplete?: (status: TrainingStatus) => void
  /** Callback on error */
  onError?: (error: string) => void
}

/**
 * Format time duration from milliseconds to human-readable string
 */
export function formatDuration(ms: number): string {
  if (ms < 1000) return `${ms}ms`
  const seconds = Math.floor(ms / 1000)
  if (seconds < 60) return `${seconds}s`
  const minutes = Math.floor(seconds / 60)
  const remainingSeconds = seconds % 60
  if (minutes < 60) return `${minutes}m ${remainingSeconds}s`
  const hours = Math.floor(minutes / 60)
  const remainingMinutes = minutes % 60
  return `${hours}h ${remainingMinutes}m`
}
