(ns auto-ap.graphql.vendors
  (:require [auto-ap.graphql.utils :refer [->graphql assert-can-see-client]]
            [auto-ap.datomic.vendors :as d-vendors]
            [auto-ap.time :refer [parse iso-date]]
            [datomic.api :as d]
            [auto-ap.datomic :refer [uri remove-nils]]
            [clj-time.coerce :as coerce]
            [clojure.set :as set]))


(defn upsert-vendor [context {{:keys [id name code print_as primary_contact secondary_contact address default_expense_account invoice_reminder_schedule] :as in} :vendor} value]
  (let [transaction [(remove-nils #:vendor {:db/id (if id
                                                     id
                                                     "vendor")
                                            :name name
                                            :code code
                                            :print-as print_as
                                            :default-expense-account default_expense_account
                                            :invoice-reminder-schedule (keyword invoice_reminder_schedule)
                                            :address (when address
                                                       (remove-nils #:address {:db/id (if (:id address)
                                                                                        (:id address)
                                                                                        "address")
                                                                               :street1 (:street1 address)
                                                                               :street2 (:street2 address)
                                                                               :city (:city address)
                                                                               :state (:state address)
                                                                               :zip (:zip address)}))
                                            :primary-contact (when primary_contact

                                                               (remove-nils #:contact {:db/id (if (:id primary_contact)
                                                                                                (:id primary_contact)
                                                                                                "primary")
                                                                                       :name (:name primary_contact)
                                                                                       :phone (:phone primary_contact)
                                                                                       :email (:email primary_contact)}))
                                            
                                            :secondary-contact (when secondary_contact

                                                                 (remove-nils #:contact {:db/id (if (:id secondary_contact)
                                                                                                  (:id secondary_contact)
                                                                                                  "secondary")
                                                                                       :name (:name secondary_contact)
                                                                                       :phone (:phone secondary_contact)
                                                                                       :email (:email secondary_contact)})
                                                               )})]
        transaction-result @(d/transact (d/connect uri) transaction)]
    
    (-> (d-vendors/get-by-id (or (-> transaction-result :tempids (get "vendor"))
                                 id))
        (->graphql))))


