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

(def email-regex #"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,63}$")
(s/def ::id int)
(s/def ::identifier (s/nilable string?))
(s/def ::required-identifier (s/and string?
                                    #(not (str/blank? %))))

(s/def ::name ::required-identifier)

(s/def ::email (s/nilable (s/and string? (s/or :is-email #(re-matches email-regex %)
                                               :is-empty #(= % "")))))
(s/def ::phone (s/nilable string?))

(s/def ::invoice-reminder-schedule (s/nilable #{"Weekly" "Never" nil}))
(s/def ::primary-contact ::identifier)
(s/def ::primary-email ::email)
(s/def ::primary-phone ::phone)

(s/def ::secondary-contact ::identifier)
(s/def ::secondary-email ::email)
(s/def ::secondary-phone ::phone)
(s/def ::address ::address/address)

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

(s/def ::vendor (s/keys :req-un [::name]
                        :opt-un [::code
                              ::id
                              ::invoice-reminder-schedule
                              ::primary-contact
                              ::primary-email
                              ::primary-phone
                              ::secondary-contact
                              ::secondary-email
                              ::secondary-phone
                              ::address
                              ]))


(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))))
