package handlers

import (
	"net/http"

	"github.com/breakpilot/ai-compliance-sdk/internal/iace"
	"github.com/gin-gonic/gin"
)

// ListMinimumDistances handles GET /minimum-distances.
// Read-only OSHA safety-distance reference (29 CFR 1910, US public domain)
// plus the curated measure→distance link table, so an auditor can see WHERE a
// measure's mm figure comes from. Scope to one measure via ?measure_id= or
// ?measure_name= (the latter lets a persisted mitigation — which stores the
// name, not the catalog ID — resolve its anchor without a schema change).
func (h *IACEHandler) ListMinimumDistances(c *gin.Context) {
	mid := c.Query("measure_id")
	mname := c.Query("measure_name")
	if mid == "" && mname != "" {
		mid, _ = iace.MeasureIDByName(mname) // "" if unknown → empty scoped result
	}
	if mid != "" || mname != "" {
		c.JSON(http.StatusOK, gin.H{
			"measure_id":   mid,
			"measure_name": mname,
			"distances":    iace.MinimumDistancesForMeasure(mid),
			"links":        iace.LinksForMeasure(mid),
			"note":         iace.MinimumDistanceNote,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"distances": iace.GetOSHAMinimumDistances(),
		"links":     iace.AllMeasureDistanceLinks(),
		"note":      iace.MinimumDistanceNote,
	})
}
