package ai_doc

import (
	"goskeleton/app/global/consts"
	"goskeleton/app/http/controller/web"
	"goskeleton/app/http/validator/core/data_transfer"
	"goskeleton/app/utils/response"
	"strings"

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

type DocRefine struct {
	Type       string `form:"type" json:"type" binding:"required"` // 必填、
	Doc        string `form:"doc" json:"doc"  binding:"required"`  // 必填、对于文本,表示它的长度>=1
	Background string `form:"background" json:"background" `
}

var Types = []string{"summary", "decoration", "extension", "correction", "translation"}

func (d DocRefine) CheckParams(context *gin.Context) {
	if err := context.ShouldBind(&d); err != nil {
		// 将表单参数验证器出现的错误直接交给错误翻译器统一处理即可
		response.ValidatorError(context, err)
		return
	}

	// 判断是否在类型中
	t := d.Type
	isExit := false
	for _, e := range Types {
		if strings.TrimSpace(t) == e {
			isExit = true
			break
		}
	}
	if !isExit {
		response.ErrorSystem(context, "DocRefine表单参数验证器json化失败", "")
		return
	}
	//  该函数主要是将本结构体的字段（成员）按照 consts.ValidatorPrefix+ json标签对应的 键 => 值 形式绑定在上下文，便于下一步（控制器）可以直接通过 context.Get(键) 获取相关值
	extraAddBindDataContext := data_transfer.DataAddContext(d, consts.ValidatorPrefix, context)
	if extraAddBindDataContext == nil {
		response.ErrorSystem(context, "DocRefine表单参数验证器json化失败", "")
		return
	}
	(&web.DocRefine{}).DocRefine(extraAddBindDataContext)
}
