package chatrecords

import (
	"context"
	"github.com/suyuan32/simple-admin-common/msg/errormsg"
	"github.com/zeromicro/go-zero/core/errorx"
	"wechat-api/ent"
	"wechat-api/ent/chatrecords"
	"wechat-api/ent/predicate"
	"wechat-api/internal/utils/dberrorhandler"

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

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

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

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

func (l *GetChatRecordsByIdLogic) GetChatRecordsById(req *types.IDReq) (resp *types.ChatRecordsInfoResp, err error) {
	var predicates []predicate.ChatRecords

	if req.Id > 0 {
		predicates = append(predicates, chatrecords.ID(req.Id))
	}

	data, err := l.svcCtx.DB.ChatRecords.Query().Where(predicates...).Only(l.ctx)

	if err != nil {
		if ent.IsNotFound(err) {
			return nil, errorx.NewInvalidArgumentError("会话记录不存在")
		}
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	}

	return &types.ChatRecordsInfoResp{
		BaseDataInfo: types.BaseDataInfo{
			Code: 0,
			Msg:  errormsg.Success,
		},
		Data: types.ChatRecordsInfo{
			Content:     &data.Content,
			ContentType: &data.ContentType,
			SessionId:   &data.SessionID,
			UserId:      &data.UserID,
			BotId:       &data.BotID,
			BotType:     &data.BotType,
		},
	}, nil
}
