package logic

import (
	"context"
	"juwan-backend/app/player/rpc/internal/svc"
	"juwan-backend/app/player/rpc/pb"

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

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

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

func (l *UpdatePlayersLogic) UpdatePlayers(in *pb.UpdatePlayersReq) (*pb.UpdatePlayersResp, error) {
	update := l.svcCtx.PlayerModelRW.Players.UpdateOneID(in.Id).
		SetNillableStatus(in.Status).
		SetNillableUserID(in.UserId).
		SetNillableShopID(in.ShopId)
	if in.Rating != nil {
		rating := decimal.NewFromFloat(*in.Rating)
		update.SetRating(rating)
	}

	err := update.Exec(l.ctx)
	if err != nil {
		return nil, err
	}

	return &pb.UpdatePlayersResp{}, nil
}
