package weblive

import (
	"net/http"
	"strings"
	"time"

	"github.com/gin-gonic/gin"
)

// liveQualitySet 与前端开播档位一致；非法 query 回落为 high
var liveQualitySet = map[string]struct{}{
	"source": {}, "high": {}, "mid": {}, "low": {},
}

func normalizeQuality(q string) string {
	q = strings.TrimSpace(strings.ToLower(q))
	if _, ok := liveQualitySet[q]; ok {
		return q
	}
	return "high"
}

func liveQualityList() []gin.H {
	return []gin.H{
		{"id": "source", "label": "原画（设备默认）"},
		{"id": "high", "label": "高清 720p"},
		{"id": "mid", "label": "标清 480p"},
		{"id": "low", "label": "流畅 360p"},
	}
}

// handleLiveInfo 仅 GET、无请求体、不读 query；只输出直播状态与画质元数据
func handleLiveInfo(c *gin.Context) {
	h, herr := getHub()
	live := false
	cq := ""
	if herr == nil {
		h.mu.RLock()
		live = h.publishConn != nil && h.pubPC != nil && len(h.forwarders) > 0
		cq = h.publishQuality
		h.mu.RUnlock()
	}
	c.JSON(http.StatusOK, gin.H{
		"live":            live,
		"qualities":       liveQualityList(),
		"current_quality": cq,
		"ts":              time.Now().UnixMilli(),
	})
}
