(ns auto-ap.datomic.expected-deposit
  (:require [auto-ap.datomic :refer [add-sorter-fields apply-pagination apply-sort-3 merge-query conn]]
            [auto-ap.graphql.utils :refer [limited-clients]]
            [auto-ap.utils :refer [dollars=]]
            [clj-time.coerce :as c]
            [datomic.api :as d]
            [clojure.tools.logging :as log]))


(defn <-datomic [result]
  (-> result
       (update :expected-deposit/date c/from-date)))

(def default-read '[*
                   {:expected-deposit/client [:client/name :db/id :client/code]}])

(defn raw-graphql-ids [db args]
  (let [query (cond-> {:query {:find []
                               :in ['$]
                               :where []}
                       :args [db]}
                (:sort args) (add-sorter-fields {"client" ['[?e :expected-deposit/client ?c]
                                                           '[?c :client/name ?sort-client]]
                                                 "location" ['[?e :expected-deposit/location ?sort-location]]
                                                 "date" ['[?e :expected-deposit/date ?sort-date]]
                                                 "total" ['[?e :expected-deposit/total ?sort-total]]
                                                 "fee" ['[?e :expected-deposit/fee ?sort-fee]]}
                                                  args)

                (limited-clients (:id args))
                (merge-query {:query {:in ['[?xx ...]]
                                      :where ['[?e :expected-deposit/client ?xx]]}
                              :args [(set (map :db/id (limited-clients (:id args))))]})

                (:exact-match-id args)
                (merge-query {:query {:in ['?e]
                                      :where []}
                              :args [(:exact-match-id args)]})

                (:client-id args)
                (merge-query {:query {:in ['?client-id]
                                      :where ['[?e :expected-deposit/client ?client-id]]}
                              :args [(:client-id args)]})
                (:client-code args)
                (merge-query {:query {:in ['?client-code]
                                      :where ['[?e :expected-deposit/client ?client-id]
                                              '[?client-id :client/code ?client-code]]}
                              :args [(:client-code args)]})

                (:total-gte args) 
                (merge-query {:query {:in ['?total-gte]
                                      :where ['[?e :expected-deposit/total ?a]
                                              '[(>= ?a ?total-gte)]]}
                              :args [(:total-gte args)]})

                (:total-lte args) 
                (merge-query {:query {:in ['?total-lte]
                                      :where ['[?e :expected-deposit/total ?a]
                                              '[(<= ?a ?total-lte)]]}
                              :args [(:total-lte args)]})

                (:total args)
                (merge-query {:query {:in ['?total]
                                      :where ['[?e :expected-deposit/total ?expected-deposit-total]
                                              '[(auto-ap.utils/dollars= ?expected-deposit-total ?total)]]}
                              :args [(:total args)]})

                
                (:start (:date-range args))
                (merge-query {:query {:in '[?start-date]
                                      :where ['[?e :expected-deposit/date ?date]
                                              '[(>= ?date ?start-date)]]}
                              :args [(c/to-date (:start (:date-range args)))]})

                (:end (:date-range args))
                (merge-query {:query {:in '[?end-date]
                                      :where ['[?e :expected-deposit/date ?date]
                                              '[(<= ?date ?end-date)]]}
                              :args [(c/to-date (:end (:date-range args)))]})

                true
                (merge-query {:query {:find ['?sort-default '?e]
                                      :where ['[?e :expected-deposit/date ?sort-default]]}}))]
    
    (cond->> query
      true (d/query)
      true (apply-sort-3 args)
      true (apply-pagination args))))

(defn graphql-results [ids db args]
  (let [results (->> (d/pull-many db default-read ids)
                     (group-by :db/id))
        payments (->> ids
                     (map results)
                     (map first)
                     (mapv <-datomic))]
    payments))

(defn get-graphql [args]
  (log/info "ARGS" args)
  (let [db (d/db conn)
        {ids-to-retrieve :ids matching-count :count} (raw-graphql-ids db args)]

    [(->> (graphql-results ids-to-retrieve db args))
     matching-count]))

(defn get-by-id [id]
  (->> 
   (d/pull (d/db conn) default-read id)
   (<-datomic)))
