package api

import (
	"git.threesix.ai/jordan/slack5-1770529463/pkg/app"
	"git.threesix.ai/jordan/slack5-1770529463/pkg/auth"
	"git.threesix.ai/jordan/slack5-1770529463/services/preferences-api/internal/api/handlers"
	"git.threesix.ai/jordan/slack5-1770529463/services/preferences-api/internal/config"
	"git.threesix.ai/jordan/slack5-1770529463/services/preferences-api/internal/service"
)

// RegisterRoutes registers all HTTP routes for the service.
func RegisterRoutes(application *app.App, preferenceService *service.PreferenceService) {
	logger := application.Logger()
	cfg := config.Load()

	// Initialize handlers with injected services
	healthHandler := handlers.NewHealth(logger)
	prefHandler := handlers.NewPreference(preferenceService, logger)

	// Build and mount OpenAPI spec
	spec := NewServiceSpec()
	application.EnableDocs(spec)

	// Register API routes under /api/{service-name} to match ingress path routing.
	application.Route("/api/preferences-api", func(r app.Router) {
		r.Get("/health", healthHandler.Check)

		// Preferences routes (auth-protectable)
		r.Group(func(r app.Router) {
			if cfg.AuthEnabled {
				r.Use(auth.Middleware(auth.MiddlewareConfig{
					Validator: auth.NewJWTValidator(auth.JWTConfig{
						Secret: []byte(cfg.JWTSecret),
						Issuer: "slack5-1770529463",
					}),
				}))
			}

			r.Get("/preferences/{user_id}", app.Wrap(prefHandler.GetPreferences))
			r.Put("/preferences/{user_id}", app.Wrap(prefHandler.UpdatePreferences))
		})
	})
}
