package api

import (
	"github.com/gin-gonic/gin"
	"github.com/tdvorak/seen/backend/internal/api/handlers"
	v1 "github.com/tdvorak/seen/backend/internal/api/routes/v1"
	"github.com/tdvorak/seen/backend/internal/config"
	"github.com/tdvorak/seen/backend/internal/middleware"
	"go.uber.org/zap"
)

type Handlers struct {
	Health      *handlers.HealthHandler
	Auth        *handlers.AuthHandler
	Catalog     *handlers.CatalogHandler
	Download    *handlers.DownloadHandler
	Placeholder *handlers.PlaceholderHandler
}

func NewRouter(cfg config.Config, log *zap.Logger, handlers Handlers) *gin.Engine {
	if cfg.Env == "production" {
		gin.SetMode(gin.ReleaseMode)
	}

	engine := gin.New()
	engine.Use(middleware.RequestID())
	engine.Use(middleware.CORS(cfg.CORS))
	engine.Use(middleware.AccessLog(log))
	engine.Use(middleware.Recovery(log))

	engine.GET("/healthz", handlers.Health.Live)

	apiV1 := engine.Group("/api/v1")
	v1.Register(apiV1, handlers.Health, handlers.Auth, handlers.Catalog, handlers.Download, handlers.Placeholder)

	return engine
}
