(ns auto-ap.routes.queries
  (:require [auto-ap.datomic :refer [conn]]
            [auto-ap.datomic.clients :as d-clients]
            [auto-ap.datomic.ledger :as d-ledger]
            [auto-ap.datomic.transactions :as d-transactions]
            [auto-ap.datomic.vendors :as d-vendors]
            [clojure.string :as str]

            [unilog.context :as lc]
            [buddy.sign.jwt :as jwt]
            [auto-ap.graphql :as graphql]
            [auto-ap.graphql.utils :refer [->graphql <-graphql assert-admin assert-can-see-client]]
            [auto-ap.routes.utils :refer [wrap-secure]]
            [clojure.tools.logging :as log]
            [auto-ap.logging :refer [error-event info-event warn-event]]
            [clj-time.coerce :as coerce :refer [to-date]]
            [amazonica.aws.s3 :as s3]
            [clj-time.core :as time]
            [clojure.java.io :as io]
            [clojure.data.csv :as csv]
            [config.core :refer [env]]
            [compojure.core :refer [context defroutes GET POST PUT wrap-routes routes]]
            [datomic.api :as d]
            [ring.middleware.json :refer [wrap-json-response]]
            [venia.core :as venia]
            [yang.time :refer [time-it]]
            [ring.util.request :refer [body-string]]
            )
  (:import java.util.UUID))


(defn wrap-csv-response [handler]
  (fn [request]
    (let [response (handler request)]
      (update response :body #(with-open [w (java.io.StringWriter.)]
                                (csv/write-csv w %)
                                (.toString w))))))

#_(GET "/raw" {:keys [query-params identity]}
     (assert-admin identity)
     (log/info "Executing raw query " (get query-params "query" ))
     (into (list) (apply d/q (clojure.edn/read-string (get query-params "query" )) (into [(d/db conn)] (clojure.edn/read-string (get query-params "args" "[]"))))))

(defn execute-query [query-params params]
  (let [{:keys [query-id]} params]
    (lc/with-context {:query-id query-id}

      (log/info "Executing raw query " query-id)
      (let [query-string (str (slurp  (:object-content (s3/get-object :bucket-name (:data-bucket env)
                                                                      :key (str "queries/" (:query-id params))))))]
        (log/info "Executing query " query-string)
        (into (list) (apply d/q (clojure.edn/read-string query-string)
                            (into [(d/db conn)] (clojure.edn/read-string (get query-params "args" "[]")))))))))


(defn put-query [id body note]
  (s3/put-object :bucket-name (:data-bucket env)
                 :key (str "queries/" id)
                 :input-stream (io/make-input-stream (.getBytes body) {})
                 :metadata {:content-type "application/text"
                            :user-metadata {:note note}})
  {:body {:query body
          :id id
          :results-url (str "/api/queries/" id "/results")
          :csv-results-url (str "/api/queries/" id "/results/csv")
          :json-results-url (str "/api/queries/" id "/results/json")}})

(def json-routes
  (context "/queries" []
           (POST "/" {:keys [query-params identity] :as request}
                 (assert-admin identity)
                 (log/info "Note" (query-params "note"))
                 (put-query (str (UUID/randomUUID)) (body-string request) (query-params "note")))
           (PUT "/:query-id" {:keys [query-params identity params] :as request}
                (assert-admin identity)
                (log/info "Note" (query-params "note"))
                (put-query (:query-id params) (body-string request) (query-params "note")))
           (GET "/:query-id" {:keys [query-params identity params]}
                (assert-admin identity)
                (let [{:keys [query-id]} params
                      obj (s3/get-object :bucket-name (:data-bucket env)
                                         :key (str "queries/" query-id))
                      query-string (str (slurp  (:object-content obj)))]
                  (log/info obj)
                  {:body {:query query-string
                          :note (:note (:user-metadata (:object-metadata obj)))
                          :id query-id
                          :csv-results-url (str "/api/queries/" query-id "/results/csv")
                          :json-results-url (str "/api/queries/" query-id "/results/json")}}))
           

           (GET "/" {:keys [query-params identity params]}
                (assert-admin identity)
                (let [{:keys [query-id]} params
                      obj (s3/list-objects :bucket-name (:data-bucket env)
                                           :prefix (str "queries/"))]
                  (log/info obj)
                  {:body (->> (:object-summaries obj)
                              (map (fn [o]
                                     {:last-modified (.toString (:last-modified o))
                                      :key (str/replace (:key o) #"^queries\/" "")})))}))

           (GET "/:query-id/results/json" {:keys [query-params identity params]}
                {:body (execute-query query-params params)})
           ))


(def raw-routes 
  (context "/queries" []
           (GET "/:query-id/raw" {:keys [query-params identity params]}
                (assert-admin identity)
                (let [{:keys [query-id]} params
                      obj (s3/get-object :bucket-name (:data-bucket env)
                                         :key (str "queries/" query-id))
                      query-string (str (slurp  (:object-content obj)))]
                  (log/info obj)
                  {:body query-string}))))

(def csv-routes 
  (context "/queries" []
           (GET "/:query-id/results/csv" {:keys [query-params identity params]}
                {:body (execute-query query-params params)})))
(defroutes query2-routes
  (routes 
   raw-routes
   (wrap-routes json-routes
                wrap-json-response)
   (wrap-routes csv-routes wrap-csv-response)))
