(ns iol-ion.tx
  (:require [datomic.api :as dc]
            [iol-ion.utils])
  (:import [java.util UUID]))

(def random-tempid iol-ion.utils/random-tempid)
(defn composite-tempid [& pieces]
  (format "hashed-%d" (.hashCode (set pieces))))

(def by iol-ion.utils/by)
(def pull-many iol-ion.utils/pull-many)
(def remove-nils iol-ion.utils/remove-nils)


;; TODO expected-deposit ledger entry 
#_(defmethod entity-change->ledger :expected-deposit
    [db [type id]]
    (let [{:expected-deposit/keys [total client date]} (d/pull db '[:expected-deposit/total :expected-deposit/client :expected-deposit/date] id)]
      #:journal-entry
       {:source          "expected-deposit"
        :original-entity id

        :client     client
        :date       date
        :amount     total
        :vendor     :vendor/ccp-square
        :line-items [#:journal-entry-line
                      {:credit   total
                       :location "A"
                       :account  :account/receipts-split}
                     #:journal-entry-line
                      {:debit    total
                       :location "A"
                       :account  :account/ccp}]}))




(defn regenerate-literals []
  (require 'com.github.ivarref.gen-fn)
  (spit
   "resources/functions.edn"
   (let [datomic-fn @(resolve 'com.github.ivarref.gen-fn/datomic-fn)]
     [(datomic-fn :pay #'iol-ion.tx.pay/pay)
      (datomic-fn :plus #'iol-ion.tx.plus/plus)
      (datomic-fn :propose-invoice #'iol-ion.tx.propose-invoice/propose-invoice)
      (datomic-fn :reset-rels #'iol-ion.tx.reset-rels/reset-rels)
      (datomic-fn :reset-scalars #'iol-ion.tx.reset-scalars/reset-scalars)
      (datomic-fn :upsert-entity #'iol-ion.tx.upsert-entity/upsert-entity)
      (datomic-fn :upsert-invoice #'iol-ion.tx.upsert-invoice/upsert-invoice)
      (datomic-fn :upsert-ledger #'iol-ion.tx.upsert-ledger/upsert-ledger)
      (datomic-fn :upsert-transaction #'iol-ion.tx.upsert-transaction/upsert-transaction)
      (datomic-fn :upsert-sales-summary #'iol-ion.tx.upsert-sales-summary-ledger/upsert-sales-summary)])))

(comment
  (regenerate-literals)
  (auto-ap.datomic/install-functions))
