package api

import (
	"log/slog"

	"github.com/gin-gonic/gin"

	"go.opentelemetry.io/otel/metric"
	"go.opentelemetry.io/otel/trace"

	swaggerfiles "github.com/swaggo/files"
	ginswagger "github.com/swaggo/gin-swagger"

	_ "nudgebee/services/docs/swagger" // generated by `swag init`
	"nudgebee/services/license"
)

func ConfigureRoutes(r *gin.Engine, tracer *trace.Tracer, meter *metric.Meter, logger *slog.Logger) {
	handleApis(r, tracer, meter, logger)
	handleCrons(r, tracer, meter, logger)
	handleHeathCheckApis(r, tracer, meter, logger)
	handleAuditApis(r, tracer, meter, logger)
	handleAuthzApis(r, tracer, meter, logger)
	handleCacheApis(r, tracer, meter, logger)
	handleIntegrationApis(r, tracer, meter, logger)
	handlePublicWebhooksApis(r, tracer, meter, logger)
	handleEventRuleApis(r, tracer, meter, logger)
	handleTriageApis(r, tracer, meter, logger)
	handleExportApis(r, tracer, meter, logger)
	handleRelayApis(r, tracer, meter, logger)
	license.RegisterRoutes(r)

	// EE route handlers (marketplace, entitlement, billing migration, etc.)
	// register themselves via api.RegisterEERoute from ee/api init().
	for _, reg := range eeRouteRegistrars {
		reg(r, tracer, meter, logger)
	}

	// OpenAPI spec — swag-generated docs merged with stub entries for every
	// other registered gin route, so the UI reflects 100% of the route table.
	r.GET("/openapi.json", mergedOpenAPIHandler(r))

	// Swagger UI: /swagger/index.html. Reads the merged spec above.
	// Auth bypassed for /swagger and /openapi.json in main.go.
	r.GET("/swagger/*any", ginswagger.WrapHandler(
		swaggerfiles.Handler,
		ginswagger.URL("/openapi.json"),
	))
}
