package chatsession

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

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

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

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

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

func (l *UpdateChatSessionLogic) UpdateChatSession(req *types.ChatSessionInfo) (*types.BaseMsgResp, error) {
	var id uint64
	var name string

	if req.Id != nil && *req.Id > 0 {
		id = *req.Id
	}
	if req.Name != nil && *req.Name != "" {
		name = *req.Name
	}
	if id == 0 || name == "" {
		return nil, errorx.NewInvalidArgumentError("参数错误")
	}

	_, err := l.svcCtx.DB.ChatSession.Update().Where(chatsession.ID(id)).SetName(name).Save(l.ctx)
	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	}

	return &types.BaseMsgResp{
		Code: 0,
		Msg:  errormsg.Success,
	}, nil
}
