// 2026-04-26 P2.5 by Claude — AIOps Timeline
// ============================================================
// Sample incidents — 3 筆範例 incident，完整 6 階段 + metadata
// NEXT_PUBLIC_AIOPS_TIMELINE_MOCK=true 時使用
// ============================================================

import type { TimelineIncident } from './types'

export const SAMPLE_TIMELINE_INCIDENTS: TimelineIncident[] = [
  {
    incident_id: 'INC-2026-0425-001',
    title: 'API Server CPU 100% — k8s-188',
    severity: 'P0',
    started_at: '2026-04-25T14:22:03+08:00',
    resolved_at: '2026-04-25T14:22:55+08:00',
    stages: [
      {
        stage: 'alert',
        status: 'success',
        timestamp: '2026-04-25T14:22:03+08:00',
        duration_ms: 0,
        data: {
          alert_name: 'HighCPUUsage',
          severity: 'P0',
          rule_matched: 'cpu-burst-rule-v3',
          labels: {
            host: 'k8s-188',
            service: 'awoooi-api',
            namespace: 'production',
          },
          annotations: {
            summary: 'CPU 使用率超過 98% 持續 5 分鐘',
            runbook_url: 'https://wiki.internal/runbooks/high-cpu',
          },
          raw_value: '100%',
          threshold: '95%',
        },
      },
      {
        stage: 'diagnose',
        status: 'success',
        timestamp: '2026-04-25T14:22:04+08:00',
        duration_ms: 8240,
        data: {
          investigator: 'PreDecisionInvestigator v2.1',
          mcp_tools_used: [
            'kubectl_top_pods',
            'kubectl_describe_pod',
            'prometheus_query_range',
            'loki_logs_tail',
            'pg_active_queries',
          ],
          dimensions: [
            { name: 'CPU 使用率', key: 'cpu_usage', value: '100%', status: 'anomaly', detail: 'P99 持續 5m 超限' },
            { name: '記憶體', key: 'memory', value: '68%', status: 'ok', detail: '正常範圍內' },
            { name: 'Pod 狀態', key: 'pod_status', value: 'Running', status: 'ok', detail: '2/2 Ready' },
            { name: '請求速率', key: 'req_rate', value: '4200/s', status: 'anomaly', detail: '正常值 800/s，異常 5×' },
            { name: '錯誤率', key: 'error_rate', value: '0.2%', status: 'ok', detail: '< 1% SLO 達標' },
            { name: '最近部署', key: 'last_deploy', value: '4h ago', status: 'ok', detail: 'v2.8.1 穩定' },
            { name: 'DB 查詢', key: 'db_queries', value: 12.4, status: 'anomaly', detail: '平均 12.4ms（基準 2ms）' },
            { name: 'GC 壓力', key: 'gc_pressure', value: '45ms/s', status: 'anomaly', detail: 'Full GC 觸發率異常' },
          ],
          summary: '請求速率異常飆升（5×正常值），觸發 CPU 飽和與 GC 壓力',
          root_cause_hypothesis: '流量突增導致 CPU 飽和，推測為外部爬蟲或流量攻擊，Pod 重啟可短暫緩解',
        },
      },
      {
        stage: 'decide',
        status: 'success',
        timestamp: '2026-04-25T14:22:12+08:00',
        duration_ms: 1340,
        data: {
          engine: 'OpenClaw v3.2',
          fusion_method: 'Weighted Confidence Fusion III',
          confidence: 0.87,
          threshold_used: 0.75,
          auto_execute: true,
          decision: 'kubectl rollout restart deployment/awoooi-api',
          reasoning: '8D 證據確認為暫態流量突增導致的 CPU 飽和；重啟可清除 GC 積壓，同時觸發 HPA 橫向擴展。信心度 0.87 > 自動執行門檻 0.75。',
          playbook_id: 'PB-K8S-001',
          playbook_name: 'K8s Deployment Restart — CPU High',
          alternate_decisions: [
            { decision: 'kubectl scale deployment/awoooi-api --replicas=4', confidence: 0.61 },
            { decision: 'human_approval_required', confidence: 0.13 },
          ],
        },
      },
      {
        stage: 'execute',
        status: 'success',
        timestamp: '2026-04-25T14:22:13+08:00',
        duration_ms: 12400,
        data: {
          command: 'kubectl rollout restart deployment/awoooi-api -n production',
          target: 'deployment/awoooi-api @ k8s-188',
          executor: 'AutoExecutor v2.0',
          duration_ms: 12400,
          stdout: 'deployment.apps/awoooi-api restarted\nWaiting for rollout to finish: 0 of 2 updated replicas are available...\nWaiting for rollout to finish: 1 of 2 updated replicas are available...\ndeployment "awoooi-api" successfully rolled out',
          exit_code: 0,
        },
      },
      {
        stage: 'verify',
        status: 'success',
        timestamp: '2026-04-25T14:22:26+08:00',
        duration_ms: 29000,
        data: {
          verifier: 'PostExecutionVerifier v1.4',
          outcome: 'SUCCESS',
          checks_passed: 5,
          checks_total: 5,
          trust_delta: 0.05,
          notes: 'CPU 回落至 42%，所有 Pod Ready，SLO 指標正常',
        },
      },
      {
        stage: 'learn',
        status: 'success',
        timestamp: '2026-04-25T14:22:55+08:00',
        duration_ms: 210,
        data: {
          playbook_id: 'PB-K8S-001',
          trust_before: 0.50,
          trust_after: 0.55,
          km_entry_id: 'KM-20260425-0312',
          learning_summary: 'Playbook PB-K8S-001 信任度 +0.05（0.50 → 0.55）。成功處置 CPU 突增事件，記錄至知識庫。',
        },
      },
    ],
  },
  {
    incident_id: 'INC-2026-0424-007',
    title: 'PostgreSQL 備份失敗 — host-110',
    severity: 'P1',
    started_at: '2026-04-24T03:15:00+08:00',
    resolved_at: '2026-04-24T03:21:44+08:00',
    stages: [
      {
        stage: 'alert',
        status: 'success',
        timestamp: '2026-04-24T03:15:00+08:00',
        duration_ms: 0,
        data: {
          alert_name: 'HostBackupFailed',
          severity: 'P1',
          rule_matched: 'backup-age-rule-v2',
          labels: { host: 'host-110', job: 'pg-backup-cron' },
          annotations: { summary: '備份任務超過 25 小時未成功' },
          raw_value: '26h ago',
          threshold: '25h',
        },
      },
      {
        stage: 'diagnose',
        status: 'success',
        timestamp: '2026-04-24T03:15:01+08:00',
        duration_ms: 6800,
        data: {
          investigator: 'PreDecisionInvestigator v2.1',
          mcp_tools_used: [
            'ssh_exec',
            'pg_backup_status',
            'disk_usage_check',
            'cron_log_tail',
          ],
          dimensions: [
            { name: '備份年齡', key: 'backup_age', value: '26h', status: 'anomaly', detail: '超過 25h 門檻' },
            { name: '磁碟空間', key: 'disk_free', value: '2.1GB', status: 'anomaly', detail: '備份目錄空間不足' },
            { name: 'PG 狀態', key: 'pg_status', value: 'running', status: 'ok', detail: 'PostgreSQL 正常運行' },
            { name: 'Cron 日誌', key: 'cron_log', value: 'FAILED', status: 'anomaly', detail: 'No space left on device' },
            { name: '上次備份', key: 'last_backup', value: null, status: 'unknown', detail: '無法取得' },
            { name: '網路', key: 'network', value: 'ok', status: 'ok', detail: 'Remote host 可達' },
            { name: '備份腳本', key: 'script_status', value: 'exist', status: 'ok', detail: '/opt/backup/pg_backup.sh 存在' },
            { name: '排程狀態', key: 'cron_enabled', value: 'enabled', status: 'ok', detail: 'Cron job 已啟用' },
          ],
          summary: '備份目錄磁碟空間不足（僅剩 2.1GB），導致備份任務失敗',
          root_cause_hypothesis: '磁碟空間耗盡，需清理舊備份後重新執行',
        },
      },
      {
        stage: 'decide',
        status: 'success',
        timestamp: '2026-04-24T03:15:08+08:00',
        duration_ms: 890,
        data: {
          engine: 'OpenClaw v3.2',
          fusion_method: 'Weighted Confidence Fusion III',
          confidence: 0.91,
          threshold_used: 0.75,
          auto_execute: true,
          decision: 'cleanup_old_backups && trigger_pg_backup',
          reasoning: '磁碟空間不足是明確根因，清理 7 天前備份後重新執行可解決。信心度 0.91 高於門檻。',
          playbook_id: 'PB-BACKUP-001',
          playbook_name: 'Backup Disk Cleanup & Retry',
          alternate_decisions: [
            { decision: 'human_approval_required', confidence: 0.09 },
          ],
        },
      },
      {
        stage: 'execute',
        status: 'success',
        timestamp: '2026-04-24T03:15:09+08:00',
        duration_ms: 394000,
        data: {
          command: 'find /backup -name "*.sql.gz" -mtime +7 -delete && /opt/backup/pg_backup.sh',
          target: 'host-110 via SSH',
          executor: 'AutoExecutor v2.0',
          duration_ms: 394000,
          stdout: 'Cleaned 8 files (12.3GB freed)\nStarting PostgreSQL backup...\nBackup completed: /backup/pg_2026-04-24_031509.sql.gz (3.2GB)',
          exit_code: 0,
        },
      },
      {
        stage: 'verify',
        status: 'success',
        timestamp: '2026-04-24T03:21:32+08:00',
        duration_ms: 12000,
        data: {
          verifier: 'PostExecutionVerifier v1.4',
          outcome: 'SUCCESS',
          checks_passed: 4,
          checks_total: 4,
          trust_delta: 0.05,
          notes: '備份檔案存在且大小合理，磁碟空間恢復正常（14.4GB 可用）',
        },
      },
      {
        stage: 'learn',
        status: 'success',
        timestamp: '2026-04-24T03:21:44+08:00',
        duration_ms: 180,
        data: {
          playbook_id: 'PB-BACKUP-001',
          trust_before: 0.65,
          trust_after: 0.70,
          km_entry_id: 'KM-20260424-0089',
          learning_summary: 'Playbook PB-BACKUP-001 信任度 +0.05（0.65 → 0.70）。建議增加磁碟空間預警規則。',
        },
      },
    ],
  },
  {
    incident_id: 'INC-2026-0423-015',
    title: 'Cadvisor OOM — monitoring namespace',
    severity: 'P2',
    started_at: '2026-04-23T09:41:22+08:00',
    stages: [
      {
        stage: 'alert',
        status: 'success',
        timestamp: '2026-04-23T09:41:22+08:00',
        duration_ms: 0,
        data: {
          alert_name: 'ContainerOOMKilled',
          severity: 'P2',
          rule_matched: 'oom-kill-detection-v1',
          labels: {
            container: 'cadvisor',
            namespace: 'monitoring',
            node: 'k8s-188',
          },
          annotations: { summary: 'cadvisor 被 OOM Killer 終止' },
          raw_value: 'OOMKilled',
        },
      },
      {
        stage: 'diagnose',
        status: 'success',
        timestamp: '2026-04-23T09:41:23+08:00',
        duration_ms: 5400,
        data: {
          investigator: 'PreDecisionInvestigator v2.1',
          mcp_tools_used: [
            'kubectl_describe_pod',
            'kubectl_top_pods',
            'prometheus_memory_usage',
          ],
          dimensions: [
            { name: 'OOM 事件', key: 'oom_event', value: 'OOMKilled', status: 'anomaly', detail: 'cadvisor 觸發 OOM' },
            { name: 'Memory limit', key: 'mem_limit', value: '256Mi', status: 'anomaly', detail: '限制過低' },
            { name: 'Memory 實際', key: 'mem_actual', value: '260Mi+', status: 'anomaly', detail: '超過 limit' },
            { name: 'CPU', key: 'cpu', value: '288%', status: 'anomaly', detail: '已持續異常 13 天（監控盲區）' },
            { name: 'Pod 重啟', key: 'restart_count', value: 47, status: 'anomaly', detail: '47 次重啟' },
            { name: '影響範圍', key: 'blast_radius', value: '監控層', status: 'anomaly', detail: '指標採集中斷' },
            { name: '其他 Pod', key: 'other_pods', value: '正常', status: 'ok', detail: '其餘 monitoring pod 正常' },
            { name: '節點資源', key: 'node_resources', value: '充裕', status: 'ok', detail: '節點整體資源正常' },
          ],
          summary: 'cadvisor memory limit 設定過低（256Mi），實際需求超過限制導致 OOM',
          root_cause_hypothesis: '提高 cadvisor memory limit 至 512Mi 並重啟',
        },
      },
      {
        stage: 'decide',
        status: 'success',
        timestamp: '2026-04-23T09:41:29+08:00',
        duration_ms: 1200,
        data: {
          engine: 'OpenClaw v3.2',
          fusion_method: 'Weighted Confidence Fusion III',
          confidence: 0.78,
          threshold_used: 0.75,
          auto_execute: true,
          decision: 'kubectl patch daemonset cadvisor -p memory_limit=512Mi',
          reasoning: '根因明確（memory limit 過低），提升 limit 後重啟即可。影響範圍僅監控層，無業務風險。',
          playbook_id: 'PB-K8S-OOM-001',
          playbook_name: 'K8s OOM — Increase Memory Limit',
          alternate_decisions: [
            { decision: 'kubectl delete pod cadvisor (restart only)', confidence: 0.20 },
            { decision: 'human_approval_required', confidence: 0.02 },
          ],
        },
      },
      {
        stage: 'execute',
        status: 'success',
        timestamp: '2026-04-23T09:41:30+08:00',
        duration_ms: 8300,
        data: {
          command: 'kubectl patch daemonset cadvisor -n monitoring --type merge -p \'{"spec":{"template":{"spec":{"containers":[{"name":"cadvisor","resources":{"limits":{"memory":"512Mi"}}}]}}}}\'',
          target: 'daemonset/cadvisor @ monitoring',
          executor: 'AutoExecutor v2.0',
          duration_ms: 8300,
          stdout: 'daemonset.apps/cadvisor patched\ncadvisor-xk2p9 restarted',
          exit_code: 0,
        },
      },
      {
        stage: 'verify',
        status: 'success',
        timestamp: '2026-04-23T09:41:39+08:00',
        duration_ms: 45000,
        data: {
          verifier: 'PostExecutionVerifier v1.4',
          outcome: 'SUCCESS',
          checks_passed: 3,
          checks_total: 4,
          trust_delta: 0.03,
          notes: 'cadvisor Running，OOM 事件消除。CPU 仍 220%（長期問題，已記錄待 P2.6 處理）',
        },
      },
      {
        stage: 'learn',
        status: 'success',
        timestamp: '2026-04-23T09:42:24+08:00',
        duration_ms: 160,
        data: {
          playbook_id: 'PB-K8S-OOM-001',
          trust_before: 0.40,
          trust_after: 0.43,
          km_entry_id: 'KM-20260423-0156',
          learning_summary: 'Playbook PB-K8S-OOM-001 信任度 +0.03（0.40 → 0.43）。cadvisor CPU 問題已記錄為待處理項目。',
        },
      },
    ],
  },
]
