package logic

import (
	"context"
	"errors"

	"juwan-backend/app/shop/rpc/internal/svc"
	"juwan-backend/app/shop/rpc/pb"
	"juwan-backend/app/snowflake/rpc/snowflake"

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

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

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

// -----------------------shopPlayers-----------------------
func (l *AddShopPlayersLogic) AddShopPlayers(in *pb.AddShopPlayersReq) (*pb.AddShopPlayersResp, error) {
	idResp, err := l.svcCtx.Snowflake.NextId(l.ctx, &snowflake.NextIdReq{})
	if err != nil {
		logx.Errorf("addShopPlayers snowflake err:%v", err)
		return nil, errors.New("create shop player id failed")
	}

	_, err = l.svcCtx.ShopModelRW.ShopPlayers.Create().
		SetID(idResp.Id).
		SetShopID(in.ShopId).
		SetPlayerID(in.PlayerId).
		SetIsPrimary(in.IsPrimary).
		Save(l.ctx)
	if err != nil {
		logx.Errorf("addShopPlayers err:%v", err)
		return nil, errors.New("add shop player failed")
	}

	return &pb.AddShopPlayersResp{}, nil
}
