-- Phase 7 migration: decision provenance.
-- Matches PROJECT.md Appendix A. Phase 8 reuses this same table with
-- simulation_id-tagged rows for what-if outputs; no schema change there.

CREATE TABLE IF NOT EXISTS decision_log (
    id                  INTEGER PRIMARY KEY AUTOINCREMENT,
    timestamp           INTEGER NOT NULL,
    decision_type       TEXT NOT NULL,        -- "delete_dead_swarms" | "tighten_filter" | "relax_filter" | "tracker_attention" | ...
    subject_type        TEXT NOT NULL,        -- "torrent" | "filter" | "tracker" | "disk" | "system"
    subject_id          TEXT NOT NULL,
    recommendation      TEXT NOT NULL,        -- one-line operator-facing
    confidence          REAL,                 -- 0..1
    provenance_json     TEXT NOT NULL,        -- full causal chain
    alternatives_json   TEXT,                 -- considered-but-rejected
    operator_action     TEXT,                 -- "acknowledged" | "applied" | "dismissed"
    operator_action_at  INTEGER
);
CREATE INDEX IF NOT EXISTS idx_decision_log_time ON decision_log(timestamp DESC);
CREATE INDEX IF NOT EXISTS idx_decision_log_subject ON decision_log(subject_type, subject_id);
CREATE INDEX IF NOT EXISTS idx_decision_log_action ON decision_log(operator_action) WHERE operator_action IS NULL;
