package chatrecords

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

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

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

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

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

func (l *UpdateChatRecordsLogic) UpdateChatRecords(req *types.ChatRecordsInfo) (*types.BaseMsgResp, error) {
	if req.Id == nil || *req.Id <= 0 {
		return nil, errorx.NewInvalidArgumentError("id cannot be null")
	}

	err := l.svcCtx.DB.ChatRecords.UpdateOneID(*req.Id).
		SetNotNilSessionID(req.SessionId).
		SetNotNilUserID(req.UserId).
		SetNotNilContent(req.Content).
		SetNotNilContentType(req.ContentType).
		SetNotNilBotID(req.BotId).
		SetNotNilBotType(req.BotType).
		Exec(l.ctx)
	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	}
	return &types.BaseMsgResp{
		Code: 0,
		Msg:  errormsg.Success,
	}, nil
}
