package http

import (
	"fmt"

	"go.dev.pztrn.name/featurer/server/internal/services/core"

	"github.com/gin-gonic/gin"
)

func (h *http) RegisterMiddleware(serverName string, middleware gin.HandlerFunc) error {
	h.serversMutex.RLock()
	defer h.serversMutex.RUnlock()

	router, found := h.servers[serverName]
	if !found {
		return fmt.Errorf("%w: registering middleware: %w", core.ErrHTTP, core.ErrHTTPServerNotFound)
	}

	_ = router.Use(middleware)

	return nil
}
