package agent

import (
	"context"
	"fmt"
	"github.com/suyuan32/simple-admin-common/msg/errormsg"
	"github.com/zeromicro/go-zero/core/errorx"
	"wechat-api/hook/fastgpt"

	"wechat-api/internal/svc"
	"wechat-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateAgentDataLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreateAgentDataLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateAgentDataLogic {
	return &CreateAgentDataLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx}
}

func (l *CreateAgentDataLogic) CreateAgentData(req *types.CreateDataInfoReq) (*types.BaseDataInfo, error) {
	params := fastgpt.CreateBulkDataReq{}
	params.CollectionID = *req.CollectionId
	params.TrainingMode = "chunk"
	pair := make([]fastgpt.DataQuestion, 0, 1)
	pair = append(pair, fastgpt.DataQuestion{
		Q: *req.Q,
		A: *req.A,
	})
	params.Data = append(params.Data, pair...)

	resp, err := fastgpt.CreateBulkData(&params)
	if err != nil {
		return nil, errorx.NewInvalidArgumentError("fastgpt create data failed " + err.Error())
	}

	if resp.Code == 200 {
		return &types.BaseDataInfo{
			Code: 0,
			Msg:  errormsg.Success,
			Data: fmt.Sprintf("Insert %d rows", resp.Data.InsertLen),
		}, nil
	}

	return nil, errorx.NewInvalidArgumentError(resp.StatusText)
}
