package gateway

import (
	"github.com/acp/interceptor/internal/auth"
	"github.com/acp/interceptor/internal/config"
	"github.com/acp/interceptor/internal/handlers"
	"github.com/acp/interceptor/internal/middleware"
	"github.com/gin-gonic/gin"
)

// NewRouter wires middleware and routes.
func NewRouter(
	cfg config.Config,
	tool *handlers.ToolHandler,
	insights *handlers.InsightsHandler,
	catalog *handlers.PolicyCatalogHandler,
	approvals *handlers.ApprovalHandler,
	agents *handlers.AgentHandler,
	jwtValidator *auth.Validator,
) *gin.Engine {
	if cfg.Environment == "production" {
		gin.SetMode(gin.ReleaseMode)
	}

	r := gin.New()
	r.Use(middleware.CORS(cfg.CORSAllowOrigins))
	r.Use(middleware.Recovery())
	r.Use(middleware.Tracing())
	r.Use(middleware.StructuredLogging())
	r.Use(middleware.APIKeyAuth(cfg.APIKey))
	r.Use(middleware.JWTAuth(jwtValidator))

	registerRoutes(r, tool, insights, catalog, approvals, agents)
	return r
}
