package rootmux

import (
	"net/http"

	_ "forge.grandlyon.com/web-et-numerique/llle_project/backoffice-server/docs"
	"forge.grandlyon.com/web-et-numerique/llle_project/backoffice-server/internal/auth"
	"forge.grandlyon.com/web-et-numerique/llle_project/backoffice-server/internal/common"
	"forge.grandlyon.com/web-et-numerique/llle_project/backoffice-server/internal/file"
	"forge.grandlyon.com/web-et-numerique/llle_project/backoffice-server/internal/models"
	"github.com/gorilla/mux"
	httpSwagger "github.com/swaggo/http-swagger"
)

var imageFolder = common.StringValueFromEnv("IMAGE_FOLDER", "")

type RootMux struct {
	Router  *mux.Router
	Manager *auth.Manager
}

// @title Backoffice API
// @version 1.0
// @description This is a sample service for managing newsletters for Ecolyo
// @termsOfService http://swagger.io/terms/
// @contact.name API Support
// @contact.email rpailharey@grandlyon.com
// @host localhost:1443
// @BasePath /

func CreateRootMux() RootMux {

	r := mux.NewRouter()
	m := auth.NewManager()
	dh := models.NewDataHandler()

	r.HandleFunc("/OAuth2Login", m.HandleOAuth2Login)
	r.Handle("/OAuth2Callback", m.HandleOAuth2Callback())
	r.HandleFunc("/Logout", m.HandleLogout)
	r.Handle("/api/common/WhoAmI", auth.ValidateAuthMiddleware(auth.WhoAmI(), []string{"*"}, false))
	r.PathPrefix("/assets/").Handler(http.StripPrefix("/assets/", http.FileServer(http.Dir("./"+imageFolder+"/"))))

	r.HandleFunc("/api/common/monthlyReport", dh.GetMonthlyReport).Methods(http.MethodGet)
	r.HandleFunc("/api/common/monthlyReport/{year}/{month}", dh.GetMonthlyReport).Methods(http.MethodGet)
	r.HandleFunc("/api/common/partnersInfo", dh.GetPartnersInfo).Methods(http.MethodGet)

	apiAdmin := r.PathPrefix("/api/admin").Subrouter()
	apiAdmin.Use(auth.AdminAuthMiddleware)

	apiAdmin.HandleFunc("/monthlyNews", dh.GetAllMonthlyNews).Methods(http.MethodGet)
	apiAdmin.HandleFunc("/monthlyNews/{year}/{month}", dh.GetSingleMonthlyNews).Methods(http.MethodGet)
	apiAdmin.HandleFunc("/monthlyNews", dh.SaveMonthlyNews).Methods(http.MethodPut)
	apiAdmin.HandleFunc("/monthlyNews/{year}/{month}", dh.DeleteMonthlyNews).Methods(http.MethodDelete)

	apiAdmin.HandleFunc("/monthlyInfo", dh.GetAllMonthlyInfo).Methods(http.MethodGet)
	apiAdmin.HandleFunc("/monthlyInfo/{year}/{month}", dh.GetSingleMonthlyInfo).Methods(http.MethodGet)
	apiAdmin.HandleFunc("/monthlyInfo", dh.SaveMonthlyInfo).Methods(http.MethodPut)
	apiAdmin.HandleFunc("/monthlyInfo/{year}/{month}", dh.DeleteMonthlyInfo).Methods(http.MethodDelete)

	apiAdmin.HandleFunc("/poll", dh.GetAllPolls).Methods(http.MethodGet)
	apiAdmin.HandleFunc("/poll/{year}/{month}", dh.GetSinglePoll).Methods(http.MethodGet)
	apiAdmin.HandleFunc("/poll", dh.SavePoll).Methods(http.MethodPut)
	apiAdmin.HandleFunc("/poll/{year}/{month}", dh.DeletePoll).Methods(http.MethodDelete)

	apiAdmin.HandleFunc("/partnersInfo", dh.SavePartnersInfo).Methods(http.MethodPut)

	apiAdmin.HandleFunc("/imageNames", file.GetEcogestureImages).Methods(http.MethodGet)

	// Swagger
	r.PathPrefix("/swagger").Handler(httpSwagger.WrapHandler)

	// Redirect route
	r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {})

	return RootMux{r, &m}
}