// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2

package game

import (
	"context"
	"errors"
	"juwan-backend/app/game/rpc/pb"

	"juwan-backend/app/game/api/internal/svc"
	"juwan-backend/app/game/api/internal/types"

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

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

// 创建游戏
func NewCreateGameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateGameLogic {
	return &CreateGameLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreateGameLogic) CreateGame(req *types.Game) (resp *types.Game, err error) {
	game, err := l.svcCtx.GameRpc.AddGames(l.ctx, &pb.AddGamesReq{
		Name:     req.Name,
		Icon:     req.Icon,
		Category: req.Category,
		IsActive: false,
	})
	if err != nil {
		logx.Errorf("add game err: %v", err)
		return nil, errors.New("add game err")
	}

	return &types.Game{
		Id:       game.Games.Id,
		Name:     game.Games.Name,
		Icon:     game.Games.Icon,
		Category: game.Games.Category,
	}, nil
}
