(ns auto-ap.ssr.indicators
  (:require [auto-ap.routes.indicators :as route]
            [auto-ap.ssr.components :as com]
            [auto-ap.ssr.utils :refer [clj-date-schema html-response
                                       wrap-schema-enforce]]
            [clj-time.coerce :as c]
            [clj-time.core :as t]))

(defn days-ago* [date]
  (if date 
    (let [start (c/to-date-time date)
          today (t/now)]

      (if (t/before? start today)
        (let [days (t/in-days (t/interval start today))]
          (com/pill {:color (cond (< days 30)
                                  :primary
                                  (< days 60)
                                  :secondary
                                  (< days 90)
                                  :yellow
                                  :else
                                  :red)}
                    (str days " days ago")))
        (com/pill {:color :primary}
                  (str (inc (t/in-days (t/interval today start))) " days from now"))))
    [:div]))

(defn days-ago [request]
  (html-response (days-ago* (:date (:query-params request)))))

(def key->handler
  {::route/days-ago (wrap-schema-enforce days-ago
                                         :query-schema
                                         [:map [:date {:optional false}
                                                clj-date-schema ]])})