package spec

import (
	_ "embed"
	"net/http"

	"github.com/uptrace/bunrouter"
)

//go:embed openapi.yaml
var openapiYAML []byte

// SpecHandler serves the raw OpenAPI YAML spec.
func SpecHandler() bunrouter.HandlerFunc {
	return func(w http.ResponseWriter, req bunrouter.Request) error {
		w.Header().Set("Content-Type", "application/yaml")
		_, err := w.Write(openapiYAML)
		return err
	}
}

// DocsHandler serves the Scalar API reference UI.
func DocsHandler() bunrouter.HandlerFunc {
	return func(w http.ResponseWriter, req bunrouter.Request) error {
		w.Header().Set("Content-Type", "text/html; charset=utf-8")
		_, err := w.Write([]byte(`<!doctype html>
<html>
<head><title>vecna API</title><meta charset="utf-8"/></head>
<body>
<script id="api-reference" data-url="/openapi.yaml"></script>
<script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script>
</body>
</html>`))
		return err
	}
}
