(ns auto-ap.entities.companies
  (: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 ::address ::address/address)

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


(s/def ::company (s/keys :req-un [::name]
                         :opt-un [::email
                                  ::address
                                  ::id]))


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