(ns wanijo.public.routes
  (:require [compojure.core :refer [defroutes wrap-routes GET]]
            [wanijo.infrastructure.routing :refer [register!]]
            [wanijo.public.db :as db-public]
            [wanijo.public.view :as view-public]))

(defn index []
  (view-public/index))

(defn show-coc [coc-key]
  (view-public/show-coc (db-public/coc! coc-key)
                        (db-public/roles-with-levels-of-coc! coc-key)))

(defn show-level [coc-key level-uuid]
  (view-public/show-level
   (db-public/coc! coc-key)
   (db-public/instance! level-uuid)
   (db-public/modules-with-comps! level-uuid)))

(defn show-role [coc-key role-uuid]
  (view-public/show-role
   (db-public/coc! coc-key)
   (db-public/role! role-uuid)
   (db-public/modules-and-levels! role-uuid)))

(defn show-module [coc-key module-uuid]
  (view-public/show-module
    (db-public/coc! coc-key)
    (db-public/module! module-uuid)))

(defn show-comp [coc-key module-uuid comp-uuid]
  (view-public/show-comp
    (db-public/coc! coc-key)
    (db-public/module! module-uuid)
    (db-public/comp! comp-uuid)))

(defn show-search-result [term]
  (view-public/show-search-result
    term
    (db-public/search! term)))

(defroutes routes
  (GET (register! :public-index "/public") [] (index))
  (GET (register! :public-search "/public/search")
       [term]
       (show-search-result term))
  (GET (register! :public-coc "/public/:coc")
       [coc]
       (show-coc coc))
  (GET (register! :public-level "/public/:coc/level/:level")
       [coc level]
       (show-level coc level))
  (GET (register! :public-role "/public/:coc/role/:role")
       [coc role]
       (show-role coc role))
  (GET (register! :public-module "/public/:coc/module/:module")
       [coc module]
       (show-module coc module))
  (GET (register! :public-comp "/public/:coc/module/:module/comp/:comp")
       [coc module comp]
       (show-comp coc module comp)))
