 ;; delete with a secondary index from seocondary and lookup tables
 ;; TODO use the :entity-key to delete stale secondary and lookup records
 (s/defschema PrimaryTableSchema
-  (merge TableSchema
-         {(s/optional-key :entity-key) SecondaryKeySchema}))
+  (merge BaseTableSchema
+         {(s/optional-key :entity-key) SecondaryKeySchema}
+         {:type (s/eq :primary)}))
 
 ;; some of the (non-partition) columns in a lookup-table
 ;; key may be collections... these will be expanded to the

     callbacks :- CallbacksSchema
     versioned? :- s/Bool])

(defn ^:private force-key-seq
  [table]
  (assoc table :key (v/coerce (:key table))))

(defn ^:private force-key-seqs
  [entity-schema table-seq-key]
  (assoc entity-schema
         table-seq-key
         (mapv force-key-seq
               (get entity-schema table-seq-key))))

(defn ^:private force-all-key-seqs
  "ensure all keys are given as sequences of components"
  [entity-schema]
  (reduce force-key-seqs
          (assoc entity-schema
                 :primary-table
                 (force-key-seq (:primary-table entity-schema)))
          [:unique-key-tables
           :secondary-tables
           :lookup-key-tables]))

(s/defn ^:always-validate create-entity :- Entity
  "create an entity record from a spec"
  [entity-spec :- EntitySchema]
  (map->Entity (merge {:unique-key-tables []
                       :secondary-tables []
                       :lookup-key-tables []
                       :callbacks {}
                       :versioned? false}
                      (force-all-key-seqs entity-spec))))

(defmacro defentity
  [name entity-spec]

  (s/conditional

   (fn [k] (-> k first sequential?))
   [(s/one
     [(s/one s/Keyword :partition-key-component) s/Keyword]
     :compound-parition-key)
    s/Keyword]

   :else
   [(s/one s/Keyword :key-component) s/Keyword]))

(s/defschema KeyValueSchema
  [(s/one s/Any :key-component) s/Any])
