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

(user/init-repl)

(d/q '[:find ?c
       :in $
       :where
       [_ :expected-deposit/charges ?c]
       [_ :sales-order/charges ?c]
       ]
     (d/db auto-ap.datomic/conn))

(take 4 (d/q '[:find (pull ?ed [* {:expected-deposit/charges [*] :expected-deposit/client [:client/code]}])
              :in $
              :where
               [?ed :expected-deposit/sales-date ?d]
               [(>= ?d #inst "2022-06-24")]
               [?ed :expected-deposit/charges ?c]
              ]
            (d/db auto-ap.datomic/conn)))

(println "test")

(let [query-id "54b9bf6b-99d3-408f-b696-96f255f0f073"
      obj                (s3/get-object :bucket-name (:data-bucket env)
                         :key (str "queries/" query-id))
      query-string       (str (slurp  (:object-content obj)))]
  (log/info obj)
  (println query-string))

(do (time (execute-query {} {:query-id "54b9bf6b-99d3-408f-b696-96f255f0f073"}))
    (println "done"))

(execute-query {} {:query-id "54b9bf6b-99d3-408f-b696-96f255f0f073"})


(d/q '[:find  ?d4 ?n ?n2 (sum ?total) (sum ?tax) (sum ?discount)
      :with ?s ?li
      :in $ 
      :where 
      [?s :sales-order/client [:client/code "NGVZ"]]
      [?s :sales-order/date ?d]
      [(ground (clj-time.coerce/to-date (clj-time.core/minus (auto-ap.time/local-now) (clj-time.core/days 30)))) ?min-d]
      [(>= ?d ?min-d)]
      [?s :sales-order/line-items ?li]
      [?li :order-line-item/category ?n]
      [(get-else $ ?li :order-line-item/item-name "") ?n2]
      [?li :order-line-item/total ?total]
      [?li :order-line-item/tax ?tax]
      [?li :order-line-item/discount ?discount]
      [(clj-time.coerce/to-date-time ?d) ?d2]
      [(auto-ap.time/localize ?d2) ?d3]
      [(auto-ap.time/unparse-local ?d3 auto-ap.time/normal-date) ?d4]
      ]
     (d/db auto-ap.datomic/conn))

(d/q '[:find ?d4 ?type ?p2 (sum ?total) (sum ?tip)
       :with ?charge
       :in $
       :where
       [?c :client/code "NGOP"]
       [?s :sales-order/client ?c]
       [?s :sales-order/date ?date]
       [(ground (clj-time.coerce/to-date (clj-time.core/minus (auto-ap.time/local-now) (clj-time.core/days 3)))) ?min-d]
       [(>= ?date ?min-d)]
       [?s :sales-order/charges ?charge]
       [?charge :charge/type-name ?type]
       [?charge :charge/total ?total]
       [?charge :charge/tip ?tip]
       (or-join [?charge ?p2]

                (and [_ :expected-deposit/charges ?charge ]
                     [(ground "settlement") ?p2])
                (and 
                  [(get-else $ ?charge :charge/processor :na) ?ccp]
                  [(get-else $ ?ccp :db/ident :na) ?p]
                  [(name ?p) ?p2]))
       
       [(clj-time.coerce/to-date-time ?date) ?d2]
       [(auto-ap.time/localize ?d2) ?d3]
       [(auto-ap.time/unparse-local ?d3 auto-ap.time/normal-date) ?d4]]
     (d/db auto-ap.datomic/conn))


(d/q '[:find (pull ?c [*])
       :in $ ?ed
       :where [?ed :expected-deposit/charges ?c]
       #_[_ :sales-order/charges ?c]]
     (d/db auto-ap.datomic/conn)
     17592266347939)

(auto-ap.square.core/get-payment (first (auto-ap.square.core/get-square-clients "NGOP"))
                                 "lWSahgO1aZMK6tb5Jv3t3SgzvaB")

(auto-ap.square.core/get-order
  (first (auto-ap.square.core/get-square-clients "NGOP"))
  (first (filter :square-location/client-location (:client/square-locations (auto-ap.square.core/get-square-clients "NGOP"))))
  "h0tVO9MK5Eh7Wpj3TEHVjX6eV")

