(ns auto-ap.ssr.admin
  (:require
   [auto-ap.datomic :refer [conn]]
   [auto-ap.routes.utils
    :refer [wrap-admin wrap-client-redirect-unauthenticated]]
   [auto-ap.ssr-routes :as ssr-routes]
   [auto-ap.ssr.components :as com]
   [auto-ap.ssr.hx :as hx]
   [auto-ap.ssr.ui :refer [base-page]]
   [bidi.bidi :as bidi]
   [clj-time.coerce :as coerce]
   [clj-time.core :as time]
   [datomic.api :as dc]
   [hiccup2.core :as hiccup]))

(defn hourly-changes []
  (let [tx-instant-attr (:db/id (dc/pull (dc/db conn) '[:db/id] :db/txInstant))
        tx-lookup       (->> 
                    (dc/tx-range
                      (dc/log conn)
                      (coerce/to-date (time/plus (time/now) (time/hours -24)))
                      (coerce/to-date (time/now)))
                    (map (fn extract-tx-instant [tx]
                           (let [tx-id      (->> (:data tx)
                                                 (map (fn [d]
                                                        (:tx d)))
                                                 first)
                                 tx-instant (->> tx
                                                 :data
                                                 (filter (fn [d]
                                                           (and (= (:e d) tx-id)
                                                                (= tx-instant-attr (:a d)))))
                                                 (map :v)
                                                 first)]
                             
                             tx-instant)))
                    (group-by (fn hours-ago [d]
                                (time/in-hours (time/interval (coerce/to-date-time d) (time/now)))
                                ))
                    )]
    (for [h (range 24)]
      (count (tx-lookup h [])))))

(defn page [request]
  (base-page 
    request
    (com/page {:nav              com/admin-aside-nav
               :client-selection (:client-selection request)
               :clients          (:clients request)
               :client           (:client request)
               :identity         (:identity request)}
              (com/breadcrumbs {} [:a {:href (bidi/path-for ssr-routes/only-routes :auto-ap.routes.admin/page)}
                                   "Admin"])
              [:div.flex.space-x-4 
               (com/content-card {:class "w-1/4"}
                                 [:div {:class "flex flex-col px-4 py-3 space-y-3"}
                                  [:div 
                                   [:h1.text-2xl.mb-3.font-bold "Growth in clients"]
                                   [:div
                                    [:div {:class      "w-full h-64"
                                           :id         "client-chart"
                                           :data-chart (hx/json {
                                                                 :labels ["2 years ago" "1 year ago" "today"],
                                                                 :series [(for [n    [2 1 0]
                                                                                :let [start (time/plus (time/now) (time/years (- n)))]]
                                                                            (->> (dc/q '[:find (count ?c)
                                                                                         :in $
                                                                                         :where [?c :client/code]]
                                                                                       (dc/as-of (dc/db conn) (coerce/to-date start)))
                                                                                 first
                                                                                 first))]})}]
                                    [:script {:lang "javascript"}
                                     (hiccup/raw
                                       "new Chartist.Bar('#client-chart', JSON.parse(document.getElementById('client-chart').getAttribute('data-chart')))")]]]])

               (com/content-card {:class "w-1/2"}
                                 [:div {:class "flex flex-col px-4 py-3 space-y-3"}
                                  [:div 
                                   [:h1.text-2xl.mb-3.font-bold "Changes by hour"]
                                   [:div
                                    [:div {:class      "w-full h-64"
                                           :id         "changes"
                                           :data-chart (hx/json {
                                                                 :labels (for [n (range -24 0)]
                                                                           (format "%d" n)),
                                                                 :series [(hourly-changes)]})}]
                                    [:script {:lang "javascript"}
                                     (hiccup/raw
                                       "new Chartist.Line('#changes', JSON.parse(document.getElementById('changes').getAttribute('data-chart')))")]]]])]
              )
    "Admin")
  )

(def key->handler
  {
   :auto-ap.routes.admin/page (wrap-client-redirect-unauthenticated (wrap-admin page))
   })

