package handlers

import (
	"net/http"
)

// Router wraps http.ServeMux and automatically injects RequestContext into handlers.
type Router struct {
	mux *http.ServeMux
	rc  *RequestContext
}

func NewRouter(rc *RequestContext) *Router {
	return &Router{
		mux: http.NewServeMux(),
		rc:  rc,
	}
}

// Handle registers a handler that returns an error.
// The RequestContext is automatically injected and error handling is applied.
func (rt *Router) Handle(pattern string, h Handler) {
	rt.mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
		rc := &RequestContext{
			DB:        rt.rc.DB,
			Logger:    rt.rc.Logger,
			TmplCache: rt.rc.TmplCache,
		}

		handler := WithErrorHandling(rc, h)
		handler(w, r)
	})
}

// HandleStd registers a standard http.Handler (for static files, etc.)
func (rt *Router) HandleStd(pattern string, h http.Handler) {
	rt.mux.Handle(pattern, h)
}

func (rt *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	rt.mux.ServeHTTP(w, r)
}
