package agent

import (
	"context"
	"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 GetAgentDataListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

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

func (l *GetAgentDataListLogic) GetAgentDataList(req *types.DataListReq) (*types.DataListResp, error) {
	dataResp := types.DataListResp{}

	var params fastgpt.GetDataListReq
	params.CollectionId = *req.CollectionId
	params.PageSize = *req.PageSize
	params.PageNum = *req.Page
	resp, err := fastgpt.GetDataList(&params)
	if err != nil {
		return nil, errorx.NewInvalidArgumentError("fastgpt get data list failed " + err.Error())
	}

	dataResp.Data = types.BatchSimpleInfo{}
	dataResp.Data.Total = uint64(resp.Data.Total)
	if resp.Code == 200 && resp.Data.Total > 0 {
		for _, val := range resp.Data.Data {
			dataResp.Data.Data = append(dataResp.Data.Data, types.DataSimpleInfo{
				ID:           &val.ID,
				Q:            &val.Q,
				A:            &val.A,
				ChunkIndex:   &val.ChunkIndex,
				DatasetId:    &val.DatasetID,
				CollectionId: &val.CollectionID,
			})
		}
	}

	return &dataResp, nil
}
