// Package api provides HTTP routing and handlers for the {{COMPONENT_NAME}} service.
package api

import (
	"{{GO_MODULE}}/pkg/app"
	"{{GO_MODULE}}/pkg/auth"
	"{{GO_MODULE}}/services/{{COMPONENT_NAME}}/internal/api/handlers"
	"{{GO_MODULE}}/services/{{COMPONENT_NAME}}/internal/config"
)

// RegisterRoutes registers all HTTP routes for the service.
// Routes are mounted under /api/{{COMPONENT_NAME}} to match the ingress path routing.
// This allows the monorepo to expose multiple services under a single domain:
//   - https://domain/api/{{COMPONENT_NAME}}/health
//   - https://domain/api/{{COMPONENT_NAME}}/examples
func RegisterRoutes(application *app.App) {
	logger := application.Logger()
	cfg := config.Load()

	// Initialize handlers
	healthHandler := handlers.NewHealth(logger)
	exampleHandler := handlers.NewExample(logger)

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

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

		// Public routes (no auth required)
		r.Get("/examples", app.Wrap(exampleHandler.List))
		r.Get("/examples/{id}", app.Wrap(exampleHandler.Get))

		// Protected routes (auth required when enabled)
		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: "{{PROJECT_NAME}}",
					}),
				}))
			}

			r.Post("/examples", app.Wrap(exampleHandler.Create))
			r.Put("/examples/{id}", app.Wrap(exampleHandler.Update))
			r.Delete("/examples/{id}", app.Wrap(exampleHandler.Delete))
		})
	})
}
