package logic

import (
	"context"
	"errors"
	"juwan-backend/app/game/rpc/internal/models/games"

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

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

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

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

func (l *GetGamesByIdLogic) GetGamesById(in *pb.GetGamesByIdReq) (*pb.GetGamesByIdResp, error) {
	game, err := l.svcCtx.GameModelRO.Games.Query().Where(games.IDEQ(in.Id)).First(l.ctx)
	if err != nil {
		logx.WithContext(l.ctx).Errorf("GetGamesByIdLogic err: %v", err)
		return nil, errors.New("get games by id failed")
	}
	pbGame := pb.Games{}
	err = copier.Copy(&pbGame, &game)
	if err != nil {
		logx.WithContext(l.ctx).Errorf("GetGamesByIdLogic copier err: %v", err)
		return nil, errors.New("get games by id failed")
	}
	pbGame.CreatedAt = game.CreatedAt.Unix()
	pbGame.UpdatedAt = game.UpdatedAt.Unix()
	return &pb.GetGamesByIdResp{
		Games: &pbGame,
	}, nil
}
