(ns auto-ap.server
  (:gen-class)
  (:require
   [auto-ap.handler :refer [app]]
   [auto-ap.jobs.bulk-journal-import :as job-bulk-journal-import]
   [auto-ap.jobs.close-auto-invoices :as job-close-auto-invoices]
   [auto-ap.jobs.current-balance-cache :as job-current-balance-cache]
   [auto-ap.jobs.ezcater-upsert :as job-ezcater-upsert]
   [auto-ap.jobs.import-uploaded-invoices :as job-import-uploaded-invoices]
   [auto-ap.jobs.intuit :as job-intuit]
   [auto-ap.jobs.ledger-reconcile :as job-reconcile-ledger]
   [auto-ap.jobs.load-historical-sales :as job-load-historical-sales]
   [auto-ap.jobs.plaid :as job-plaid]
   [auto-ap.jobs.register-invoice-import :as job-register-invoice-import]
   [auto-ap.jobs.square :as job-square]
   [auto-ap.jobs.square2 :as job-square2]
   [auto-ap.jobs.sysco :as job-sysco]
   [auto-ap.jobs.vendor-usages :as job-vendor-usages]
   [auto-ap.jobs.yodlee2 :as job-yodlee2]
   [clojure.tools.logging :as log]
   [com.unbounce.dogstatsd.core :as statsd]
   [config.core :refer [env]]
   [mount.core :as mount]
   [nrepl.server :refer [start-server]]
   [ring.adapter.jetty :refer [run-jetty]]
   [yang.scheduler :as scheduler])
  (:import
   (org.eclipse.jetty.server.handler StatisticsHandler)
   (org.eclipse.jetty.server.handler.gzip GzipHandler)))

(defn add-shutdown-hook! [^Runnable f]
  (.addShutdownHook (Runtime/getRuntime)
                    (Thread. f)))


(defn gzip-handler []
  (doto (GzipHandler.)
    (.setIncludedMimeTypes (into-array ["text/css"
                                        "text/plain"
                                        "text/javascript"
                                        "text/csv"
                                        "text/html"
                                        "text/html;charset=utf-8"
                                        "application/javascript"
                                        "application/csv"
                                        "application/edn"
                                        "application/json"
                                        "image/svg+xml"]))
    (.setMinGzipSize 1024)))

(defn configure-jetty [server]
  (let [stats-handler (StatisticsHandler.)
        gzip-handler (gzip-handler)
        default-handler (.getHandler server)]
    
    (.setHandler gzip-handler default-handler)
    (.setHandler stats-handler gzip-handler)
    (.setHandler server stats-handler))
  (.setStopAtShutdown server true))

(mount/defstate port :start (Integer/parseInt (or (env :port) "3000")))
(mount/defstate jetty
  :start (run-jetty app {:port port
                         :join? false
                         :configurator configure-jetty})
  :stop (.stop jetty))

(defn collect-jetty-stats []
  (try 
    (statsd/gauge "requests.active" (double (.getRequestsActive (.getHandler jetty))))
    (statsd/gauge "requests.1xx" (double (.getResponses1xx (.getHandler jetty))))
    (statsd/gauge "requests.2xx" (double (.getResponses2xx (.getHandler jetty))))
    (statsd/gauge "requests.3xx" (double (.getResponses3xx (.getHandler jetty))))
    (statsd/gauge "requests.4xx" (double (.getResponses4xx (.getHandler jetty))))
    (statsd/gauge "requests.5xx" (double (.getResponses5xx (.getHandler jetty))))
    (.statsReset (.getHandler jetty))
    (catch Exception e
      (log/warn e))))

#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(mount/defstate jetty-stats
  :start (scheduler/every (* 1000 10) collect-jetty-stats)
  :stop (scheduler/stop jetty-stats))

(defn shutdown-mount []
  (mount/stop))

 (defn -main [& _]
   (let [job (System/getenv "INTEGREAT_JOB")]
     (cond (= job "square-import-job")
           (job-square/-main)

           (= job "square2-import-job")
           (job-square2/-main)

           (= job "reconcile-ledger")
           (job-reconcile-ledger/-main)

           (= job "current-balance-cache")
           (job-current-balance-cache/-main)

           (= job "yodlee2")
           (job-yodlee2/-main)

           (= job "yodlee2-accounts")
           (job-yodlee2/accounts-only)

           (= job "plaid")
           (job-plaid/-main)

           (= job "intuit")
           (job-intuit/-main)

           (= job "vendor-usages")
           (job-vendor-usages/-main)

           (= job "import-uploaded-invoices")
           (job-import-uploaded-invoices/-main)

           (= job "sysco")
           (job-sysco/-main)

           (= job "close-auto-invoices")
           (job-close-auto-invoices/-main)

           (= job "ezcater-upsert")
           (job-ezcater-upsert/-main)


           (= job "register-invoice-import")
           (job-register-invoice-import/-main)

           (= job "load-historical-sales")
           (job-load-historical-sales/-main)

           (= job "bulk-journal-import")
           (job-bulk-journal-import/-main)

           :else
           (do 
             (add-shutdown-hook! shutdown-mount)
             (start-server :port 9000 :bind "0.0.0.0" #_#_:handler (cider-nrepl-handler))
             (mount/start)
             #_(alter-var-root #'nrepl.middleware.print/*print-fn* (constantly clojure.pprint/pprint))))))

