package handlers

// docforge variable catalogue handler (t-paliad-349 slice 5).
//
// Endpoint: GET /api/docforge/variables → the full variable catalogue
// (key + bilingual label + namespace group) the sidebar form and the
// authoring palette render. The catalogue is the Go-side single source of
// truth, built from the submission resolvers' Keys(); it replaces the
// duplicated TS VARIABLE_LABELS table so labels can't drift between the
// resolver that produces a value and the form that labels it.
//
// Static — no DB call, no per-user state. Auth-gated only (anonymous 401);
// the catalogue is the same for every authenticated user.

import (
	"net/http"

	"mgit.msbls.de/m/paliad/internal/services"
)

type docforgeVariablesResponse struct {
	Variables []variableEntry `json:"variables"`
}

type variableEntry struct {
	Key     string `json:"key"`
	LabelDE string `json:"label_de"`
	LabelEN string `json:"label_en"`
	Group   string `json:"group"`
}

// handleDocforgeVariables backs GET /api/docforge/variables.
func handleDocforgeVariables(w http.ResponseWriter, r *http.Request) {
	if _, ok := requireUser(w, r); !ok {
		return
	}
	cat := services.SubmissionVariableCatalogue()
	out := make([]variableEntry, 0, len(cat))
	for _, e := range cat {
		out = append(out, variableEntry{
			Key:     e.Key,
			LabelDE: e.LabelDE,
			LabelEN: e.LabelEN,
			Group:   e.Group,
		})
	}
	writeJSON(w, http.StatusOK, docforgeVariablesResponse{Variables: out})
}
