(ns yenu.handler
  (:require [compojure.core :refer [routes wrap-routes]]
            [yenu.layout :refer [error-page]]
            [yenu.routes.auth :refer [auth-routes]]
            [yenu.routes.core :refer [core-routes]]
            [yenu.routes.feed :refer [feed-routes]]
            [yenu.routes.admin :refer [admin-routes]]
            [compojure.route :as route]
            [yenu.env :refer [defaults]]
            [mount.core :as mount]
            [yenu.middleware :as middleware]))

(mount/defstate init-app
                :start ((or (:init defaults) identity))
                :stop  ((or (:stop defaults) identity)))

(def app-routes
  (routes
   (-> #'auth-routes
       (wrap-routes middleware/wrap-csrf)
       (wrap-routes middleware/wrap-identity)
       (wrap-routes middleware/wrap-formats))
   (-> #'feed-routes
       (wrap-routes middleware/wrap-identity)
       (wrap-routes middleware/wrap-formats)
       (wrap-routes middleware/wrap-auth))
   (-> #'core-routes
       (wrap-routes middleware/wrap-csrf)
       (wrap-routes middleware/wrap-formats)
       (wrap-routes middleware/wrap-identity)
       (wrap-routes middleware/wrap-auth))
   (-> #'admin-routes
       (wrap-routes middleware/wrap-csrf)
       (wrap-routes middleware/wrap-formats)
       (wrap-routes middleware/wrap-identity)
       (wrap-routes middleware/wrap-auth))
   (route/not-found
    (:body
     (error-page {:status 404
                  :title "page not found"})))))


(defn app [] (middleware/wrap-base #'app-routes))
