package handler

import (
	"net/http"

	"github.com/denisovdennis/autohero/internal/game"
)

// APITimePausedMiddleware blocks mutating /api/v1 requests while global simulation time is frozen.
// GET/HEAD/OPTIONS still work so clients can read state.
func APITimePausedMiddleware(engine *game.Engine) func(http.Handler) http.Handler {
	return func(next http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			switch r.Method {
			case http.MethodGet, http.MethodHead, http.MethodOptions:
				next.ServeHTTP(w, r)
				return
			}
			if engine != nil && engine.IsTimePaused() {
				writeJSON(w, http.StatusServiceUnavailable, map[string]string{
					"error": "server time is paused",
				})
				return
			}
			next.ServeHTTP(w, r)
		})
	}
}
