;; 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.

(def c auto-ap.datomic/conn)

(defn check-fn [query checker]
  (fn [db]
    (assert (checker (d/q query db))
            (str "query failed!" query))))

(defn check-transaction [fs tx]

  (println "checking before...")
  (doseq [f fs]
    (f (d/db c)))

  (println "checing after...")
  (let [updated (:db-after (d/with (d/db c)
                                   tx))]
    (doseq [f fs]
      (f updated)))
  (println "success!"))




(let [invoices-without-clients (check-fn '[:find ?i
                                           :in $
                                           :where
                                           [?i :invoice/invoice-number]
                                           (not [?i :invoice/client ])]
                                         #(= (count %) 0))
      transactions-without-clients (check-fn '[:find ?t
                                               :in $
                                               :where
                                               [?t :transaction/amount]
                                               (not [?t :transaction/client ])]
                                             #(= (count %) 0))
      account-overide-without-clients (check-fn '[:find ?t
                                                  :in $
                                                  :where
                                                  [?t :account-client-override/name]
                                                  (not [?t :account-client-override/client ])]
                                                #(= (count %) 0))
      vendor-schedule-payment-dom (check-fn '[:find ?t
                                                  :in $
                                                  :where
                                                  [?t :vendor-schedule-payment-dom/dom]
                                                  (not [?t :vendor-schedule-payment-dom/client ])]
                                            #(= (count %) 0))
      payments-without-clients (check-fn '[:find ?t
                                              :in $
                                              :where
                                              [?t :payment/date]
                                              (not [?t :payment/client ])]
                                         #(= (count %) 0))
      vendor-usage-without-client (check-fn '[:find ?t
                                           :in $
                                           :where
                                           [?t :vendor-usage/vendor]
                                           (not [?t :vendor-usage/client ])]
                                         #(= (count %) 0))
      journal-entries-without-client (check-fn '[:find ?t
                                                 :in $
                                                 :where
                                                 [?t :journal-entry/date]
                                                 (not [?t :journal-entry/client ])]
                                               #(= (count %) 0))
      userless-users (check-fn '[:find ?t
                                 :in $
                                 :where
                                 [?t :user/role :user-role/user]
                                 (not [?t :user/clients ])]
                               #(= (count %) 0))
      invoice-payments-without-invoices (check-fn '[:find ?t
                                                    :in $
                                                    :where
                                                    [?t :invoice-payment/amount]
                                                    (not [?t :invoice-payment/invoice ])]
                                                  #(= (count %) 0))
      banks-without-types (check-fn '[:find ?ba
                                      :in $
                                      :where [?ba :bank-account/name]
                                      (not [?ba :bank-account/type])]
                                    #(= (count %) 0))

      transaction fix-transaction
      ]

  (check-transaction [invoices-without-clients transactions-without-clients
                      account-overide-without-clients vendor-schedule-payment-dom
                      payments-without-clients vendor-usage-without-client
                      journal-entries-without-client userless-users
                      invoice-payments-without-invoices banks-without-types]
                     transaction)
  )

(def fix-transaction
  (d/transact c (into [{:db/id "datomic.tx"
                        :db/doc "Fixing issue with a transaction that was adjusting bank accounts"}]
                      (->> 
                       (d/pull (d/db c) '[*] 17592250556002) 
                       :transaction/accounts
                       (group-by #(dissoc % :db/id))
                       (vals)
                       (mapcat rest)
                       (map :db/id)
                       (map (fn [to-delete]
                              [:db/retractEntity to-delete]))
                       ))))







(comment
  (defn delete-client []
    (->> [[:db/retractEntity [:client/code "NGGL"]]]
           (into (map (fn [[i]]
                        [:db/retractEntity i])
                      (d/q '[:find ?i
                             :in $
                             :where (or [?i :invoice/client [:client/code "NGGL"]]
                                        [?i :vendor-usage/client [:client/code "NGGL"]]
                                        [?i :transaction/client [:client/code "NGGL"]]
                                        [?i :payment/client [:client/code "NGGL"]]
                                        [?i :transaction-rule/client [:client/code "NGGL"]]
                                        [?i :journal-entry/client [:client/code "NGGL"]]
                                        [?i :user/clients [:client/code "NGGL"]])]
                           (d/db c))))
           (into (map (fn [[i]]
                        [:db/retractEntity i])
                      (d/q '[:find ?ip
                             :in $
                             :where [?i :invoice/client [:client/code "NGGL"]]
                             [?ip :invoice-payment/invoice ?i]]
                           (d/db c)))))))
