(ns auto-ap.graphql.clients
  (:require [auto-ap.datomic.clients :as d-clients]
            [datomic.api :as d]
            [auto-ap.datomic :refer [uri remove-nils]]
            [auto-ap.graphql.utils :refer [->graphql assert-admin can-see-client?]]
            [clojure.string :as str])) 

#_(def role->datomic-role {":none" :user-role/none
                         ":admin" :user-role/admin
                         ":user" :user-role/user})

(defn assert-client-code-is-unique [code]
  (when (seq (d/query {:query {:find '[?id]
                               :in ['$ '?code]
                               :where ['[?id :client/code ?code]]}
                       :args [(d/db (d/connect uri)) code]}))
    (throw (ex-info "Client is not unique" {:validation-error (str "Client code '" code "' is not unique.")}))))

(defn edit-client [context {:keys [edit_client new_bank_accounts] :as args} value]
  (assert-admin (:id context))
  (when-not (:id edit_client)
    (assert-client-code-is-unique (:code edit_client)))
 
  (let [client (when (:id edit_client) (d-clients/get-by-id (:id edit_client)))
        id (or (:db/id client) "new-client")
        _ (println id)
        transactions [(remove-nils {:db/id id
                                          :client/code (if (str/blank? (:client/code client))
                                                         (:code edit_client)
                                                         (:client/code client))
                                          :client/name (:name edit_client)
                                          :client/email (:email edit_client)
                                          :client/locations (filter identity (:locations edit_client))
                                          :client/address (remove-nils {
                                                            :address/street1  (:street1 (:address edit_client))
                                                            :address/street2  (:street2 (:address edit_client))
                                                            :address/city  (:city (:address edit_client))
                                                            :address/state  (:state (:address edit_client))
                                                                        :address/zip  (:zip (:address edit_client))})

                                          :client/bank-accounts (map #(remove-nils
                                                                       {:db/id (:id %)
                                                                        :bank-account/code (:code %)
                                                                        :bank-account/bank-name (:bank_name %)
                                                                        :bank-account/bank-code (:bank_code %)
                                                                        :bank-account/routing (:routing %)

                                                                        :bank-account/name (:name %)
                                                                        :bank-account/number (:number %)
                                                                        :bank-account/check-number (:check_number %)

                                                                        :bank-account/yodlee-account-id (:yodlee_account_id %)
                                                                        :bank-account/type :bank-account-type/check
                                                                        }
                                                                       ) (:bank_accounts edit_client))

                                          })]
        result @(d/transact (d/connect uri) transactions)]
    (println result "ID" id)
    (-> result :tempids (get id) (or id) d-clients/get-by-id ->graphql)))


(defn get-client [context args value]
  (->graphql
   (filter #(can-see-client? (:id context) %)
           (d-clients/get-all))))
