package middleware

import (
	"net/http"
	"time"

	"github.com/niels/banking-app/backend/internal/observability"
)

func Metrics(registry *observability.Registry) Middleware {
	return func(next http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			start := time.Now()
			recorder := &statusRecorder{ResponseWriter: w, status: http.StatusOK}
			next.ServeHTTP(recorder, r)
			registry.RecordHTTPRequest(r.Method, r.URL.Path, recorder.status, time.Since(start))
		})
	}
}
