 (ns clj-money.models.entities
   (:refer-clojure :exclude [update])
   (:require [clojure.pprint :refer [pprint]]
+            [clojure.spec :as s]
             [clojure.tools.logging :as log]
             [clojure.set :refer [rename-keys]]
             [clojure.reflect :refer :all]
-            [schema.core :as s]
             [clj-money.validation :as validation]
             [clj-money.models.helpers :refer [with-storage]]
             [clj-money.models.storage :refer [create-entity

  "Creates a new entity"
  [storage-spec entity]
  (with-storage [s storage-spec]
    (let [validated (validate-new-entity s entity)]
      (when (not validated)
        (throw (ex-info "The validated data is null" {:entity entity
                                                      :validated validated})))
      (if (validation/has-error? validated)
        validated
        (->> validated
             before-save
             (create-entity s ))))))

(defn select
  "Returns entities for the specified user"

                                              update-entity
                                              delete-entity]]))

(s/def ::name string?)
(s/def ::id integer?)
(s/def ::user-id integer?)
(s/def ::monitored-account-ids (s/coll-of integer?))
(s/def ::new-entity (s/keys :req-un [::name ::user-id] :opt-un [::monitored-account-ids]))
(s/def ::existing-entity (s/keys :req-un [::id ::name] :opt-un [::monitored-account-ids ::user-id]))

;(defn- name-must-be-unique
;  "Validation rule function that ensures an account
;  name is unique within an entity"
;  [storage {entity-name :name
;            entity-id :id
;            user-id :user-id
;            :as model}]
;  {:model model
;   :errors (let [existing (when (and entity-name user-id)
;                            (->> (select-entities storage user-id)
;                                 (remove #(= (:id %) entity-id))
;                                 (filter #(= (:name %) entity-name))))]
;             (if (seq existing)
;               [[:name "Name is already in use"]]
;               []))})

(defn- before-save
  [entity]
