package compapi

import (
	"time"
	"wechat-api/internal/types"
)

type FormClient struct {
	MismatchClient
}

func (me *FormClient) ResponseFormatSetting(req *types.CompApiReq) ResponseFormatConfig {
	nowTime := time.Now().Format("2006-01-02 15:04:05")
	weekday := time.Now().Weekday().String()
	//Message重构的配置
	me.ResformatConfig.SysmesArgs = []any{nowTime, weekday}
	me.ResformatConfig.SysmesTmpl = `# 任务
请帮助user从聊天记录中提取表单值，并返回一个JSON格式的表单值。

# 背景信息
当前时间是：%s
当前星期是：%s

# 返回值示例
* 如表单类型为 input、autoComplete、textarea，返回示例：["表单值"]
* 如表单类型为 radio、select，返回示例：["值1"]
* 如表单类型为 checkbox，返回示例：["值1", "值2"]
* 如表单类型为 cascader，返回示例：["一级值1", "二级值3"]
* 如表单类型为 date，返回示例：["2025-01-01"]
* 如没有找到某个表单相关的值，请不要返回该表单`

	me.ResformatConfig.UsermesArgs = []any{req.Variables["form_data"], req.Variables["chat_history"]}
	me.ResformatConfig.UsermesTmpl = `# 待提取表单
%s

# 聊天记录
%s`

	//ResponseFormat设置的配置
	me.ResformatConfig.ResformatDesc = "从通话记录中提取表单"
	me.ResformatConfig.ResformatTxt = `[{
    "dataIndex": str, # 表单ID
    "value": list[str] # 表单值列表
}]`

	type formStruct struct {
		DataIndex string   `json:"dataIndex" jsonschema_description:"表单ID"`
		Value     []string `json:"value" jsonschema_description:"值列表"`
	}
	me.ResformatConfig.ResformatStruct = struct {
		Values []formStruct `json:"values" jsonschema_description:"表单列表"`
	}{}
	me.ResformatConfig.HaveSet = true

	return me.ResformatConfig
}

func (me *FormClient) BuildRequest(req *types.CompApiReq) error {

	//设置相关个性化ResponseFormat配置信息
	if !me.ResformatConfig.HaveSet {
		me.ResponseFormatSetting(req)
	}

	return me.MismatchClient.BuildRequest(req)
}

// 向Client.getClientActFace工厂方法注册
func init() {
	builder := func(c *Client) (clientActionFace, error) {
		return &FormClient{MismatchClient{StdClient: StdClient{Client: c}}}, nil
	}
	err := RegisterClient("form", builder)
	if err != nil {
		//panic(fmt.Sprintf("Failed to register client type 'mismatch': %v", err))
	}
}
