-- ADR-094: Hermes NL Dispatch Audit Log
-- 每次 @mention 觸發 → 記錄派發決策供 P95 latency 監控與幻覺追蹤
-- 2026-04-25 ogt + Claude Sonnet 4.6

CREATE TABLE IF NOT EXISTS hermes_dispatch_log (
    id              BIGSERIAL        PRIMARY KEY,
    created_at      TIMESTAMPTZ      NOT NULL DEFAULT NOW(),
    chat_id         VARCHAR(32)      NOT NULL,
    user_id         BIGINT           NOT NULL,
    username        VARCHAR(100),
    agent_name      VARCHAR(64)      NOT NULL,
    input_preview   VARCHAR(200),    -- 前 200 字，不存完整輸入（隱私）
    latency_ms      INTEGER,
    success         BOOLEAN          NOT NULL DEFAULT TRUE,
    error_type      VARCHAR(64),
    budget_usd      NUMERIC(8, 5)
);

CREATE INDEX IF NOT EXISTS idx_hermes_dispatch_created ON hermes_dispatch_log(created_at DESC);
CREATE INDEX IF NOT EXISTS idx_hermes_dispatch_agent   ON hermes_dispatch_log(agent_name);
CREATE INDEX IF NOT EXISTS idx_hermes_dispatch_user    ON hermes_dispatch_log(user_id);

GRANT SELECT, INSERT ON hermes_dispatch_log TO awoooi;
GRANT USAGE, SELECT ON SEQUENCE hermes_dispatch_log_id_seq TO awoooi;

COMMENT ON TABLE hermes_dispatch_log IS 'ADR-094: Hermes NL 派發審計日誌（P95 latency 監控 + 幻覺追蹤）';
