package logic

import (
	"context"
	"errors"
	"juwan-backend/app/shop/rpc/internal/models/shopplayers"

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

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

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

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

func (l *DelShopPlayersLogic) DelShopPlayers(in *pb.DelShopPlayersReq) (*pb.DelShopPlayersResp, error) {
	affected, err := l.svcCtx.ShopModelRW.ShopPlayers.Delete().
		Where(
			shopplayers.ShopIDEQ(in.ShopId),
			shopplayers.PlayerIDEQ(in.PlayerId),
		).
		Exec(l.ctx)
	if err != nil {
		logx.Errorf("delete shop players failed, %s", err.Error())
		return nil, errors.New("delete failed")
	}
	if affected == 0 {
		return nil, errors.New("delete failed")
	}

	return &pb.DelShopPlayersResp{}, nil
}
