package router

import (
	"github.com/gin-gonic/gin"
	"io.wdd.message_pusher/cmii"
	"io.wdd.message_pusher/pusher"
	"net/http"
)

const CmiiUpdateTopicName = "cmii"

func CMIIRouter(r *gin.Engine) {

	cmiiGroup := r.Group("/cmii")

	{
		// 在这个组中添加你需要的路由器
		cmiiGroup.GET("/", func(c *gin.Context) {
			c.JSON(http.StatusOK, gin.H{"message": "Welcome to cmii route group"})
		})

		cmiiGroup.POST("/update", CmiiUpdate)
	}
}

// CmiiUpdate godoc
// @Summary ping example
// @Schemes
// @Description response to cmii update notification
// @Tags CmiiUpdate
// @Accept json
// @Produce json
// @Param body body cmii.MessageBody true "请求体"
// @Success 200 {object} pusher.Message
// @Router /cmii/update [post]
func CmiiUpdate(c *gin.Context) {
	// 获取请求中的参数
	var messageBody cmii.MessageBody
	if err := c.ShouldBindJSON(&messageBody); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
		return
	}

	pusher.DefaultPusherClient.ChangeTopicName(CmiiUpdateTopicName)
	// 处理请求
	upgradeMessage := messageBody.DefaultPushUpgradeMessage()

	// *pusher.Message
	c.JSON(http.StatusOK, upgradeMessage)
}
