;; lib/mod/whatif.sx — policy what-if / impact analysis.
;;
;; Before shipping a policy change, a moderation team needs to know which past or
;; pending reports would decide differently. mod/decision-diff compares one
;; report's action under two rule sets; mod/policy-impact runs a whole batch and
;; returns only the reports whose decision flips. Pure SX over decide-report.

(define
  mod/decision-diff
  (fn
    (r reports rules-a rules-b)
    (let
      ((a (get (mod/decide-report r reports rules-a) :action))
        (b (get (mod/decide-report r reports rules-b) :action)))
      {:after b :changed (if (= a b) false true) :report-id (mod/report-id r) :before a})))

(define
  mod/policy-impact
  (fn
    (reports rules-a rules-b)
    (reduce
      (fn
        (acc r)
        (let
          ((d (mod/decision-diff r reports rules-a rules-b)))
          (if (get d :changed) (append acc (list d)) acc)))
      (list)
      reports)))

(define
  mod/impact-count
  (fn
    (reports rules-a rules-b)
    (len (mod/policy-impact reports rules-a rules-b))))

(define
  mod/impact-report
  (fn
    (reports rules-a rules-b)
    (let
      ((changed (mod/policy-impact reports rules-a rules-b)))
      (if
        (empty? changed)
        "No decisions change."
        (mod/join-with
          "\n"
          (map
            (fn
              (d)
              (str
                (get d :report-id)
                ": "
                (get d :before)
                " → "
                (get d :after)))
            changed))))))
