;; next/genesis/projections/actor-state.sx
;;
;; Per-actor live state: publicKeys (with history per design §9.6),
;; profile fields (preferredUsername, summary, ...), follower/
;; following counts. Powers the actor doc endpoint and the
;; time-aware signature verification in envelope:verify_signature/2.

(DefineProjection
  :name "actor-state"
  :doc "Actor-id -> {publicKeys, profile, followers, following}.\n        Updated by Create{Person|Service|Group}, Update (key\n        rotation, profile edits), Move (federation migration)."
  :initial-state {}
  :fold (fn
    (state act)
    (let
      ((aid (-> act :actor)) (t (-> act :type)))
      (cond
        (= t "Create")
        (assoc state aid (or (-> act :object) {}))
        (= t "Update")
        (assoc
          state
          aid
          (merge
            (or (get state aid) {})
            (or (-> act :patch) {})))
        :else state))))
