package handler

import (
	"net/http"

	"codeberg.org/VARASYS/ZDDC/zddc/internal/zddc"
)

// ZddcSchemaPath is the JSON endpoint serving the .zddc JSON Schema (the machine
// grammar). The browse client + the .zddc form view fetch it to drive editing
// (per-property x-zddc-tier marks structure vs option) and validation.
const ZddcSchemaPath = "/.api/zddc-schema"

// ServeZddcSchema returns the embedded .zddc JSON Schema. Read-only, no auth —
// it documents the policy grammar and leaks nothing. GET/HEAD only.
func ServeZddcSchema(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodGet && r.Method != http.MethodHead {
		w.Header().Set("Allow", "GET, HEAD")
		http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
		return
	}
	w.Header().Set("Content-Type", "application/schema+json; charset=utf-8")
	w.Header().Set("Cache-Control", "max-age=300")
	if r.Method == http.MethodHead {
		return
	}
	_, _ = w.Write(zddc.ZddcSchemaBytes())
}
