 (def profile-schema
   [ :id
     :entity_id
+    :timestamp
     { :name    :profile_data
       :type    :nested-item
       :schema  profile-data-schema }
     { :name    :window_sets
       :type    :associated-items
       :schema   window-set-schema}
-    { :name    :thermal-images
+    { :name    :thermal_images
       :type    :associated-items
       :schema  thermal-images-schema }
     { :name    :storeys


(defn resource-put! [store ctx]
  (db/with-session [session (:hecuba-session store)]
    (let [{request :request} ctx]
      (if-let [item (::item ctx)]
        (let [body       (decode-body request)
              entity_id  (-> item :entity_id)
              username   (sec/session-username (-> ctx :request :session))
              user_id    (:id (users/get-by-username session username))
              profile_id (-> item :profile-id)]
          (db/execute session (hayt/insert :profiles (hayt/values (-> body
                                                                      (assoc :id profile_id)
                                                                      (assoc :user_id user_id)
                                                                      ;; TODO: add storeys, walls, etc.
                                                                      stringify-values)))))
        (ring-response {:status 404 :body "Please provide valid entity_id and timestamp"})))))

(defn resource-handle-ok [store ctx]


(defn index-post! [store ctx]
  (db/with-session [session (:hecuba-session store)]
    (let [{:keys [request profile]} ctx
          {:keys [entity_id timestamp]} profile
          username   (sec/session-username (-> ctx :request :session))
          ;; FIXME: Why user_id?
          user_id    (-> (db/execute session (hayt/select :users (hayt/where [[= :username username]]))) first :id)
          profile_id (sha1/gen-key :profile profile)]
      (when (and entity_id timestamp)
        (when-not (empty? (-> (db/execute session (hayt/select :entities (hayt/where [[= :id entity_id]]))) first))
          (let [query-profile (-> profile
                                 (assoc :user_id user_id)
                                 (update-stringified-lists
                                  [:airflow_measurements :chps
                                   :conservatories :door_sets
                                   :extensions :floors :heat_pumps
                                   :heating_systems :hot_water_systems
                                   :low_energy_lights :photovoltaics
                                   :roof_rooms :roofs :small_hydros
                                   :solar_thermals :storeys :thermal_images
                                   :ventilation_systems :walls
                                   :wind_turbines :window_sets])
                                 (update-in [:profile_data] json/encode))]
          (db/execute session (hayt/insert :profiles (hayt/values query-profile)))
          {:profile_id profile_id}))))))


(defresource index [store]
