// 2026-04-26 P2.5 by Claude — AIOps Timeline
// ============================================================
// AIOps Timeline 型別定義
// 告警→決策→執行→驗證鏈路資料結構
// ============================================================

export type StageStatus = 'success' | 'running' | 'failed' | 'skipped' | 'pending'

export type StageType =
  | 'alert'
  | 'diagnose'
  | 'decide'
  | 'execute'
  | 'verify'
  | 'learn'

// ----- Alert Stage -----
export interface AlertStageData {
  alert_name: string
  severity: 'P0' | 'P1' | 'P2' | 'P3'
  rule_matched: string
  labels: Record<string, string>
  annotations: Record<string, string>
  raw_value?: string
  threshold?: string
}

// ----- Diagnose Stage -----
export interface EvidenceDimension {
  name: string
  key: string
  value: string | number | null
  status: 'ok' | 'anomaly' | 'unknown'
  detail?: string
}

export interface DiagnoseStageData {
  investigator: string
  mcp_tools_used: string[]
  dimensions: EvidenceDimension[]
  summary: string
  root_cause_hypothesis: string
}

// ----- Decide Stage -----
export interface DecideStageData {
  engine: string
  fusion_method: string
  confidence: number
  threshold_used: number
  auto_execute: boolean
  decision: string
  reasoning: string
  playbook_id: string
  playbook_name: string
  alternate_decisions?: Array<{ decision: string; confidence: number }>
}

// ----- Execute Stage -----
export interface ExecuteStageData {
  command: string
  target: string
  executor: string
  duration_ms: number
  stdout?: string
  stderr?: string
  exit_code: number
}

// ----- Verify Stage -----
export interface VerifyStageData {
  verifier: string
  outcome: 'SUCCESS' | 'PARTIAL' | 'FAILED' | 'TIMEOUT'
  checks_passed: number
  checks_total: number
  trust_delta: number
  notes?: string
}

// ----- Learn Stage -----
export interface LearnStageData {
  playbook_id: string
  trust_before: number
  trust_after: number
  km_entry_id?: string
  learning_summary: string
}

export type StageData =
  | AlertStageData
  | DiagnoseStageData
  | DecideStageData
  | ExecuteStageData
  | VerifyStageData
  | LearnStageData

export interface TimelineStageEntry {
  stage: StageType
  status: StageStatus
  timestamp: string       // ISO 8601
  duration_ms?: number
  data: StageData
}

export interface TimelineIncident {
  incident_id: string
  title: string
  severity: 'P0' | 'P1' | 'P2' | 'P3'
  started_at: string
  resolved_at?: string
  stages: TimelineStageEntry[]
}

export interface TimelineFilterState {
  incident_id: string
  time_range: '1h' | '6h' | '24h' | '7d' | 'custom'
  status_filter: 'all' | 'success' | 'failed' | 'running'
}
