package weblive

import (
	"net/http"
	"strings"

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

func GetLiveModeration(c *gin.Context) {
	c.JSON(http.StatusOK, ModerationStateSnapshot())
}

func PutLiveMuteAll(c *gin.Context) {
	var body struct {
		Enabled bool `json:"enabled"`
	}
	if err := c.ShouldBindJSON(&body); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "参数无效"})
		return
	}
	SetMuteAll(body.Enabled)
	c.JSON(http.StatusOK, gin.H{"ok": true})
}

func PutLiveMuteIP(c *gin.Context) {
	var body struct {
		IP      string `json:"ip"`
		Enabled bool   `json:"enabled"`
	}
	if err := c.ShouldBindJSON(&body); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "参数无效"})
		return
	}
	ip := strings.TrimSpace(body.IP)
	if ip == "" {
		c.JSON(http.StatusBadRequest, gin.H{"error": "IP 不能为空"})
		return
	}
	SetIPMuted(ip, body.Enabled)
	c.JSON(http.StatusOK, gin.H{"ok": true})
}

func PutLiveMuteUser(c *gin.Context) {
	var body struct {
		Username string `json:"username"`
		Enabled  bool   `json:"enabled"`
	}
	if err := c.ShouldBindJSON(&body); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "参数无效"})
		return
	}
	u := strings.TrimSpace(body.Username)
	if u == "" {
		c.JSON(http.StatusBadRequest, gin.H{"error": "用户名不能为空"})
		return
	}
	SetUserMuted(u, body.Enabled)
	c.JSON(http.StatusOK, gin.H{"ok": true})
}
