package middleware

import (
	"net/http"

	"github.com/seifghazi/claude-code-monitor/internal/runtime"
)

// InFlight tracks the number of requests currently being served by the
// wrapped handler. Apply it to the /v1/* subrouter only — the gauge is
// meant to drive deploy-time draining decisions and shouldn't be polluted
// by fast dashboard or health-probe traffic.
//
// The decrement runs in a defer so a panicking handler can't strand the
// counter.
func InFlight(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		runtime.IncInFlight()
		defer runtime.DecInFlight()
		next.ServeHTTP(w, r)
	})
}
