 (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

        entity_id (:entity_id route-params)]
    (case request-method
      :post (let [decoded-body  (decode-body request)
                  _ (log/info "BODY SIZE: " (count decoded-body))
                  _ (log/info "Oh damn, why is it just one vector-in-a-list? Let's see what's in there at least")
                  raw-body (first decoded-body)
                  _ (log/info "RAW BODY:\n" raw-body)
                  _ (log/info "BODY SIZE:\t" (count raw-body))
                  _ (log/info "doesn't make much sense does it... it's like we got back a vector of strings, joined on \\n")
                  body     (if (= "text/csv" (:content-type request))
                             (let [body-map (reduce conj {} raw-body)
                                   _ (println "BODY:\n" body-map)]
                               (parse-by-schema body-map profile-schema))
                             raw-body)]
              ;; We need to assert a few things
              (if (not= (:entity_id body) entity_id)
                true                  ; it's malformed, game over


(defn resource-put! [store ctx]
  (db/with-session [session (:hecuba-session store)]
    (let [request (:request ctx)]
      (if-let [item (::item ctx)]
        (let [decoded-body  (decode-body request)
              body     (if (= "text/csv" (:content-type request))
                         (let [body-map (reduce conj {} decoded-body)]
                           (parse-by-schema body-map profile-schema))
                         decoded-body)
              entity_id  (-> item :entity_id)
              username   (sec/session-username (-> ctx :request :session))
              user_id    (-> (db/execute session (hayt/select :users (hayt/where [[= :username username]]))) first :id)
              profile_id (-> item :profile_id)]
          (db/execute session (hayt/insert :profiles (hayt/values (-> body
                                                                      (assoc :id profile_id)
                                                                      (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))))))
        (ring-response {:status 404 :body "Please provide valid entity_id and timestamp"})))))

(defn resource-handle-ok [store ctx]
