(ns fix-pending-transactions)


(def potential-matches
 (d/query {:query {:find ['(pull ?e1 [:db/id :transaction/date])
                          '(pull ?e2 [:db/id :transaction/date])]
                   :in ['$]
                   :where ['[?e1 :transaction/status "PENDING"]
                           '[?e1 :transaction/description-original ?e1-d]
                           '[?e1 :transaction/amount ?e1-a]
                           #_'[?e1 :transaction/date ?e1-dt]
                           
                           '[?e2 :transaction/description-original ?e1-d]
                           '[?e2 :transaction/amount ?e1-a]
                           #_'[?e2 :transaction/date e1-dt]
                           '[?e2 :transaction/status "POSTED"]]}
           :args [(d/db (d/connect uri))]})
 )

(def all-pending
  (d/query {:query {:find ['?e1]
                    :in ['$]
                    :where ['[?e1 :transaction/status "PENDING"]]}
            :args [(d/db (d/connect uri))]}))

(def checkless
  (d/query {:query {:find ['?e1]
                    :in ['$]
                    :where ['[?e1 :transaction/status "PENDING"]
                            '(not [?e1 :transaction/payment])
                            ]}
            :args [(d/db (d/connect uri))]}))

(def checked
  (d/query {:query {:find ['?e1 '(pull ?z [:payment/amount :payment/type :payment/status {:payment/client [:client/code]}])]
                    :in ['$]
                    :where ['[?e1 :transaction/status "PENDING"]
                            '[?e1 :transaction/payment ?z]
                            ]}
            :args [(d/db (d/connect uri))]}))

(count checkless)
(count all-pending)
(count checked)
(take 4 checked)

(def match-lookup (->> potential-matches
                       (reduce
                        (fn [acc x]
                          (into acc (map
                                     #(vector (:db/id %) x)
                                     x)))
                        {})))

(take 2 potential-matches)

(take 3 match-lookup)

(->> checked
     (map first)
     (map #(if (get match-lookup %)
             (d/pull (d/db (d/connect uri)) '[*] (:db/id (second (get match-lookup %)))) 
             )))

(defn revert-transactions [ids]
  (->> 
   (d/query {:query {:find ['(pull ?id [:db/id {:journal-entry/_original-entity [:db/id]
                                                :transaction/payment [:db/id]} ])]
                     :in ['$ '[?id ...]]
                     :where ['[?id :transaction/status]]}
             :args [(d/db (d/connect uri)) ids]})
   (mapcat (fn [[transaction]]
             (let [ledger-id (:db/id (first (:journal-entry/_original-entity transaction)))
                   payment-id (:db/id (:transaction/payment transaction))]
               (cond-> [[:db/retractEntity (:db/id transaction)]]
                 ledger-id (conj [:db/retractEntity ledger-id])
                 payment-id (conj {:db/id payment-id
                                   :payment/status :payment-status/pending})))))
   (into [])))

#_(doseq [result (partition-all 200 (revert-transactions (map first checked)))]
  (println "proccesed 1")
  @(d/transact (d/connect uri) result ))

(map second checked)


(re-find #"(?i)check(card|[^0-9]+([0-9]*))" "Check-1205")
