(ns auto-ap.handler
  (:require [amazonica.core :refer [defcredential]]
            [auto-ap.routes.auth :as auth]
            [auto-ap.routes.clients :as clients]
            [auto-ap.routes.invoices :as invoices]
            [auto-ap.routes.reminders :as reminders]
            [auto-ap.routes.graphql :as graphql]
            [auto-ap.routes.yodlee :as yodlee]
            [auto-ap.routes.events :as events]
            [auto-ap.routes.checks :as checks]
            [auto-ap.routes.exports :as exports]
            [buddy.auth.backends.token :refer [jws-backend]]
            [buddy.auth.middleware :refer [wrap-authentication
                                           wrap-authorization]]
            [clojure.java.jdbc :as jdbc]
            [compojure.core :refer :all]
            [compojure.route :as route]
            [config.core :refer [env]]
            [ring.middleware.edn :refer [wrap-edn-params]]
            [ring.middleware.multipart-params :as mp]
            [ring.middleware.params :refer [wrap-params]]
            [ring.middleware.reload :refer [wrap-reload]]
            [ring.util.response :as response]))

(defcredential (:aws-access-key-id env) (:aws-secret-access-key env) (:aws-region env))

(defroutes static-routes
  (GET "/"  [] (response/resource-response "index.html" {:root "public"})) 
  (route/resources "/")
  (routes (ANY "*" [] (response/resource-response "index.html" {:root "public"}))))

(defroutes api-routes
  (context "/api" []
           exports/routes
           yodlee/routes
           invoices/routes
           clients/routes
           reminders/routes
           checks/routes
           graphql/routes
           auth/routes))


(def auth-backend (jws-backend {:secret (:jwt-secret env) :options {:alg :hs512}}))

(defn wrap-transaction [handler]
  (fn [request]
    (handler request)))

(def app-routes
  (routes 
   (wrap-transaction api-routes)
   (context "/api" [] events/routes)


   static-routes))



(def app
  (->  #'app-routes
      (wrap-authorization auth-backend)
      (wrap-authentication auth-backend)
      (wrap-reload)
      (wrap-params)
      (mp/wrap-multipart-params)
      (wrap-edn-params)))
