package rest

import (
	_ "embed"
	"net/http"

	repoDocs "github.com/ocdp/cluster-service/docs"
)

var (
	//go:embed swagger-ui.html
	swaggerHTML []byte

	//go:embed swaggerui/swagger-ui.css
	swaggerCSS []byte

	//go:embed swaggerui/swagger-ui-bundle.js
	swaggerBundleJS []byte

	//go:embed swaggerui/swagger-ui-standalone-preset.js
	swaggerStandalonePresetJS []byte
)

// SwaggerHandler Swagger UI Handler
type SwaggerHandler struct{}

// NewSwaggerHandler 创建 Swagger Handler
func NewSwaggerHandler() *SwaggerHandler {
	return &SwaggerHandler{}
}

// ServeSwaggerUI 提供 Swagger UI 页面
func (h *SwaggerHandler) ServeSwaggerUI(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	w.WriteHeader(http.StatusOK)
	w.Write(swaggerHTML)
}

// ServeSwaggerCSS 提供 Swagger UI 样式
func (h *SwaggerHandler) ServeSwaggerCSS(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/css; charset=utf-8")
	w.Header().Set("Cache-Control", "public, max-age=86400")
	w.WriteHeader(http.StatusOK)
	w.Write(swaggerCSS)
}

// ServeSwaggerBundle 提供 Swagger UI 主脚本
func (h *SwaggerHandler) ServeSwaggerBundle(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
	w.Header().Set("Cache-Control", "public, max-age=86400")
	w.WriteHeader(http.StatusOK)
	w.Write(swaggerBundleJS)
}

// ServeSwaggerStandalonePreset 提供 Swagger UI 预设脚本
func (h *SwaggerHandler) ServeSwaggerStandalonePreset(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
	w.Header().Set("Cache-Control", "public, max-age=86400")
	w.WriteHeader(http.StatusOK)
	w.Write(swaggerStandalonePresetJS)
}

// ServeOpenAPISpec 提供 OpenAPI 规范文件
func (h *SwaggerHandler) ServeOpenAPISpec(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/yaml; charset=utf-8")
	w.WriteHeader(http.StatusOK)
	w.Write(repoDocs.OpenAPISpec)
}
