package controllers

import (
	"github.com/gogf/gf/v2/net/ghttp"
	"yx-dataset-server/app/bll"
	"yx-dataset-server/library/dingtalk"
	"yx-dataset-server/library/gplus"
)

type Robot struct {
	RobotConfigBll bll.IRobotConfig
}

func NewRobot(bRobotConfig bll.IRobotConfig) *Robot {
	return &Robot{
		RobotConfigBll: bRobotConfig,
	}
}

// ReceiveMessage 接收HTTP Webhook消息（用于文本回复，ReplyType=1）
// @Tags API-RobotConfig
// @Summary 接收机器人消息（HTTP模式）
// @Param body schema.RobotConfig true "提交的数据"
// @Success 200  schema.RobotConfig
// @Failure 400  schema.HTTPError "{error:{code:0,message:无效的请求参数}}"
// @Failure 401  schema.HTTPError "{error:{code:0,message:未授权}}"
// @Failure 500  schema.HTTPError "{error:{code:0,message:服务器错误}}"
// @Router /robot/v1/robots/receive/{id}
func (a *Robot) ReceiveMessage(r *ghttp.Request) {
	var data dingtalk.Message
	if err := gplus.ParseJson(r, &data); err != nil {
		gplus.ResError(r, err)
	}
	id := r.Get("id").String()
	ctx := gplus.NewContext(r)
	err := a.RobotConfigBll.ReceiveMessage(ctx, id, data)
	if err != nil {
		gplus.ResError(r, err)
	}
	ResSuccess(r, nil)
}
