package router

import (
	"boostai-backend/internal/config"
	"boostai-backend/internal/database"
	webAuth "boostai-backend/internal/handlers/web/auth"
	"boostai-backend/internal/handlers/web/health"
	webReseed "boostai-backend/internal/handlers/web/reseed"
	"boostai-backend/internal/handlers/web/root"
	authmw "boostai-backend/internal/middleware"

	"github.com/gofiber/fiber/v2"
)

func buildAuthMiddleware(cfg *config.Config) *authmw.AuthMiddleware {
	return authmw.NewAuthMiddleware(cfg)
}

func registerWebRoutes(app *fiber.App, cfg *config.Config, db *database.DB, authMiddleware *authmw.AuthMiddleware) {
	rootHandler := root.NewHandler()
	healthHandler := health.NewHandler(cfg.Environment, db)
	authHandler := webAuth.NewHandler(cfg, db, authMiddleware)
	reseedHandler := webReseed.NewHandler(db, cfg)

	app.Get("/", rootHandler.Index)
	app.Get("/health", healthHandler.Check)
	app.Get("/reseed", reseedHandler.Page)
	app.Post("/reseed/login", reseedHandler.Login)
	app.Post("/reseed/run", reseedHandler.Run)
	app.Post("/reseed/logout", reseedHandler.Logout)

	authGroup := app.Group("/auth")
	authGroup.Post("/register", authHandler.RegisterUser)
	authGroup.Post("/login", authHandler.Login)
	authGroup.Get("/me", authMiddleware.RequireAuth(), authHandler.Me)
	authGroup.Patch("/me", authMiddleware.RequireAuth(), authHandler.UpdateMe)
	authGroup.Post("/logout", authMiddleware.RequireAuth(), authHandler.Logout)
}
