package http

import (
	"fmt"

	"github.com/gin-gonic/gin"

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

func (h *http) RegisterHandler(serverName, method, path string, handler func(*gin.Context)) error {
	h.serversMutex.RLock()
	defer h.serversMutex.RUnlock()

	srv, found := h.servers[serverName]
	if !found {
		return fmt.Errorf(
			"%w: registering handler '%s %s': %w",
			core.ErrHTTP,
			method,
			path,
			core.ErrHTTPServerNotFound,
		)
	}

	_ = srv.Handle(method, path, handler)

	return nil
}
