-- AIOps Phase 1 / Phase 2 / Phase 6 — 補齊缺失 DB 表
-- ADR-081 (P1 EvidenceSnapshot) + ADR-082 (P2 AgentSession) + ADR-087 (P6 GovernanceEvent)
-- 2026-04-15 ogt + Claude Sonnet 4.6（亞太）: 補齊三張缺失表，全開 P1-P6 必需

-- ============================================================================
-- 1. incident_evidence — ADR-081 Phase 1 EvidenceSnapshot 持久化
-- ============================================================================
CREATE TABLE IF NOT EXISTS incident_evidence (
    id                      VARCHAR(36)     PRIMARY KEY,
    incident_id             VARCHAR(30)     NOT NULL,
    matched_playbook_id     VARCHAR(36),
    schema_version          VARCHAR(10)     NOT NULL DEFAULT 'v1',

    -- 8D 感官數據
    k8s_state               JSONB,
    recent_logs             TEXT,
    metrics_snapshot        JSONB,
    recent_deployments      JSONB,
    business_metrics        JSONB,
    historical_context      TEXT,
    peer_health             JSONB,
    dependency_topology     JSONB,
    anomaly_context         JSONB,

    -- 感官品質指標
    mcp_health              JSONB           NOT NULL DEFAULT '{}',
    collection_duration_ms  INTEGER,
    sensors_attempted       INTEGER         NOT NULL DEFAULT 0,
    sensors_succeeded       INTEGER         NOT NULL DEFAULT 0,

    -- LLM 輸入摘要
    evidence_summary        TEXT,

    -- 執行前後 State
    pre_execution_state     JSONB,
    post_execution_state    JSONB,
    verification_result     VARCHAR(20),

    -- 時間戳
    collected_at            TIMESTAMPTZ     NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS ix_incident_evidence_incident_id    ON incident_evidence (incident_id);
CREATE INDEX IF NOT EXISTS ix_incident_evidence_collected_at   ON incident_evidence (collected_at);
CREATE INDEX IF NOT EXISTS ix_incident_evidence_playbook_id    ON incident_evidence (matched_playbook_id);


-- ============================================================================
-- 2. agent_sessions — ADR-082 Phase 2 多 Agent 辯證 Immutable Event Log
-- ============================================================================
CREATE TABLE IF NOT EXISTS agent_sessions (
    id          VARCHAR(36)     PRIMARY KEY,
    session_id  VARCHAR(36)     NOT NULL,
    incident_id VARCHAR(50)     NOT NULL,
    agent_role  VARCHAR(20)     NOT NULL,
    input_hash  VARCHAR(16)     NOT NULL DEFAULT '',
    output_json JSONB           NOT NULL DEFAULT '{}',
    latency_ms  INTEGER         NOT NULL DEFAULT 0,
    vote        VARCHAR(20)     NOT NULL DEFAULT 'abstain',
    degraded    BOOLEAN         NOT NULL DEFAULT FALSE,
    created_at  TIMESTAMPTZ     NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS ix_agent_sessions_session_id    ON agent_sessions (session_id);
CREATE INDEX IF NOT EXISTS ix_agent_sessions_incident_id   ON agent_sessions (incident_id);
CREATE INDEX IF NOT EXISTS ix_agent_sessions_created_at    ON agent_sessions (created_at);
CREATE INDEX IF NOT EXISTS ix_agent_sessions_session_role  ON agent_sessions (session_id, agent_role);


-- ============================================================================
-- 3. ai_governance_events — ADR-087 Phase 6 自我治理事件（不可變）
-- ============================================================================
CREATE TABLE IF NOT EXISTS ai_governance_events (
    id           VARCHAR(36)  PRIMARY KEY,
    event_type   VARCHAR(40)  NOT NULL,
    triggered_at TIMESTAMPTZ  NOT NULL DEFAULT NOW(),
    details      JSONB        NOT NULL DEFAULT '{}',
    resolved     BOOLEAN      NOT NULL DEFAULT FALSE,
    resolved_at  TIMESTAMPTZ,
    resolved_by  VARCHAR(100)
);

CREATE INDEX IF NOT EXISTS ix_ai_governance_events_event_type    ON ai_governance_events (event_type);
CREATE INDEX IF NOT EXISTS ix_ai_governance_events_triggered_at  ON ai_governance_events (triggered_at);
CREATE INDEX IF NOT EXISTS ix_ai_governance_events_resolved      ON ai_governance_events (resolved);
