;; This buffer is for Clojure experiments and evaluation.

;; Press C-j to evaluate the last expression.

;; You can also press C-u C-j to evaluate the expression and pretty-print its result.


(d/q '[:find ?tx
       :in $ [?tx ..]
       :where [_ :transaction/amount _ false ?tx]]
     (dc/history ()))


(user/init-repl)


(def attrs-used  #{:transaction/bank-account
                    :transaction/date
                    :journal-entry-line/credit
                    :transaction/check-number
                    :transaction/matched-rule
                    :journal-entry/alternate-description
                    :payment/status                   :db/txInstant                :transaction/payment
                    :journal-entry-line/account       :transaction/client          :journal-entry/date
                    :journal-entry/original-entity    :journal-entry/client
                    :journal-entry/line-items         :journal-entry/source        :transaction/status
                    :transaction-account/account      :audit/batch
                    :transaction-account/location     :transaction/id              :transaction/location
                    :transaction/description-original :journal-entry-line/debit
                    :transaction/approval-status      :import-batch/entry
                    :transaction-account/amount       :transaction/amount          :transaction/accounts
                    :transaction/description-simple   :transaction/raw-id
                    :journal-entry/cleared            :journal-entry/amount        :journal-entry/vendor
                    :audit/user                       :journal-entry-line/location :transaction/vendor})

(def attrs-to-undo (set (filter (comp (complement #{"audit" "journal-entry" "journal-entry-line" "db"}) namespace) attrs-used)))

(def potential-reverts 
  (let [db (d/db auto-ap.datomic/conn)
        a  (:db/id (d/pull db [:db/id] :audit/user))]
    (for [n     (reverse (d/tx-range (d/log auto-ap.datomic/conn)
                                     #inst "2022-09-02T14:08:00-07:00"
                                     #inst "2022-09-02T14:28:00-07:00"))
          :when (seq (filter #(and (= a (:a %))
                                   (= "admin-Julia Tarabbia" (:v %)))
                             (:data n)))
          datum (:data n)
          :let  [attr (d/ident db (:a datum))]
          :when (attrs-to-undo attr)]
      (if (:added datum)
        [:db/retract (:e datum) attr (:v datum)]
        [:db/add (:e datum) attr (:v datum)]))))

(doseq [[n p]    (map vector (range) (partition-all 50 (partition-by second potential-reverts)))
        :let [transaction (into [] (mapcat identity) p)]]
  (println "batch " n)
  @(d/transact
     auto-ap.datomic/conn
     (conj transaction
                     {:db/id      "datomic.tx"
                      :audit/user (str "Bryce fixup")
                      :db/doc     "Fixing a mistake by undoing julia's changes"})))

(count potential-reverts)

(take 50 potential-reverts)

(spit-csv [:e :a :v :added] potential-reverts)

