package agent_base

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

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

func (l *GetAgentBaseByIdLogic) GetAgentBaseById(req *types.DataDetailReq) (*types.DataDetailResp, error) {
	resp, err := fastgpt.GetDataDetail(*req.ID)
	if err != nil {
		return nil, errorx.NewInvalidArgumentError("fastgpt get data failed " + err.Error())
	}

	if resp.Code == 200 {
		indexes := make([]types.Index, 0, len(resp.Data.Indexes))
		for _, val := range resp.Data.Indexes {
			indexes = append(indexes, types.Index{
				DefaultIndex: &val.DefaultIndex,
				Text:         &val.Text,
				DataId:       &val.DataID,
				ID:           &val.ID,
			})
		}

		return &types.DataDetailResp{
			BaseDataInfo: types.BaseDataInfo{
				Code: 0,
				Msg:  errormsg.Success,
			},
			Data: types.DataInfo{
				ID:           &resp.Data.ID,
				Q:            &resp.Data.Q,
				A:            &resp.Data.A,
				ChunkIndex:   &resp.Data.ChunkIndex,
				DatasetId:    &resp.Data.DatasetID,
				CollectionId: &resp.Data.CollectionID,
				SourceName:   &resp.Data.SourceName,
				CanWrite:     &resp.Data.CanWrite,
				IsOwner:      &resp.Data.IsOwner,
				Indexes:      indexes,
			},
		}, nil
	}

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