(ns auto-ap.entities.vendors
  (:require [clojure.spec.alpha :as s]
            [clojure.string :as str]
            [auto-ap.entities.contact :as contact]
            [auto-ap.entities.address :as address]))

(s/def ::id string?)
(s/def ::identifier (s/nilable string?)) (s/def ::required-identifier (s/and string?
                                    #(not (str/blank? %))))

(s/def ::name ::required-identifier)
(s/def ::hidden boolean?)
(s/def ::print-as (s/nilable string?))
(s/def ::terms (s/nilable int?))
(s/def ::dom (s/nilable int?))

(s/def ::invoice-reminder-schedule (s/nilable #{"Weekly" "Never" nil}))

(s/def ::primary-contact (s/nilable ::contact/contact))
(s/def ::secondary-contact (s/nilable ::contact/contact))
(s/def ::address (s/nilable ::address/address))
(s/def ::default-account-id (s/nilable string?))

(s/def ::code (s/nilable string?))

(s/def ::vendor (s/and
                 (s/keys :req-un [::name]
                         :opt-un [::code
                                  ::default-account-id
                                  ::terms
                                  ::hidden
                                  ::id
                                  ::print-as
                                  ::primary-contact
                                  ::secondary-contact
                                  ::address])

                 (s/or :hidden #(= (:hidden %) true)
                       :has-expense-account #(not (nil? (:id (:default-account %)))))))


(def vendor-spec (apply hash-map  (drop 1 (s/form ::vendor))))
(def all-keys (map #(keyword (name %)) (concat (:req-un vendor-spec) (:opt-un vendor-spec))))
