package logic

import (
	"context"
	"errors"
	"juwan-backend/app/snowflake/rpc/snowflake"

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

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

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

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

// -----------------------shopInvitations-----------------------
func (l *AddShopInvitationsLogic) AddShopInvitations(in *pb.AddShopInvitationsReq) (*pb.AddShopInvitationsResp, error) {
	idResp, err := l.svcCtx.Snowflake.NextId(l.ctx, &snowflake.NextIdReq{})
	if err != nil {
		logx.Errorf("addPlayerServices err:%v", err)
		return nil, errors.New("create player service id failed")
	}

	_, err = l.svcCtx.ShopModelRW.ShopInvitations.Create().
		SetID(idResp.Id).
		SetShopID(in.ShopId).
		SetPlayerID(in.PlayerId).
		SetStatus(in.Status).
		SetInvitedBy(in.InvitedBy).
		Save(l.ctx)

	if err != nil {
		return nil, errors.New("add shop invitation failed")
	}
	return &pb.AddShopInvitationsResp{}, nil
}
