-- sql/migrations/staging/002_issue_user_explanation.sql
-- Add columns for the on-demand "Explain this to me" feature and for
-- per-finding evidence quoting.
--
-- user_explanation: plain-English explanation generated on demand by
--   the advanced model; cached so clicking the button again doesn't
--   burn another AI call.
-- user_explanation_at / _model: audit fields (when + which model
--   produced the current explanation).
-- evidence: verbatim log quote supporting the finding (~200 chars).
--   Populated by the review processor; stored so the UI can show
--   "because we saw: ..." without needing to re-open the raw log.

ALTER TABLE whp.ai_monitor_issues
    ADD COLUMN IF NOT EXISTS user_explanation      TEXT NULL,
    ADD COLUMN IF NOT EXISTS user_explanation_at   DATETIME NULL,
    ADD COLUMN IF NOT EXISTS user_explanation_model VARCHAR(128) NULL,
    ADD COLUMN IF NOT EXISTS evidence              TEXT NULL;

-- Daily quota on the "Explain this to me" feature for non-root users.
-- Only fresh AI calls count toward the quota — cache hits (same issue
-- explained again within the same day) are free. Root is never limited.
-- user_explanation_at on ai_monitor_issues is the timestamp we query to
-- count against this quota (see customer API explain_issue action).
ALTER TABLE whp.ai_monitor_subscriptions
    ADD COLUMN IF NOT EXISTS daily_explain_limit INT NOT NULL DEFAULT 10
        COMMENT 'Max "Explain this" AI calls per user per 24h (0 disables)';
