package logic

import (
	"context"
	"errors"
	"juwan-backend/app/player/rpc/internal/models/playerservices"

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

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

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

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

func (l *GetPlayerServicesByIdLogic) GetPlayerServicesById(in *pb.GetPlayerServicesByIdReq) (*pb.GetPlayerServicesByIdResp, error) {
	playerService, err := l.svcCtx.PlayerModelRO.PlayerServices.Query().Where(playerservices.IDEQ(in.Id)).First(l.ctx)
	if err != nil {
		logx.WithContext(l.ctx).Errorf("GetPlayerServicesByIdLogic err: %v", err)
		return nil, errors.New("get player services by id failed")
	}

	pbPlayerService := pb.PlayerServices{
		Id:           playerService.ID,
		PlayerId:     playerService.PlayerID,
		GameId:       playerService.GameID,
		Title:        playerService.Title,
		Price:        playerService.Price.InexactFloat64(),
		Unit:         playerService.Unit,
		Availability: playerService.Availability,
		Rating:       playerService.Rating.InexactFloat64(),
		IsActive:     playerService.IsActive,
		CreatedAt:    playerService.CreatedAt.Unix(),
		UpdatedAt:    playerService.UpdatedAt.Unix(),
	}
	if playerService.Description != nil {
		pbPlayerService.Description = *playerService.Description
	}
	if playerService.RankRange != nil {
		pbPlayerService.RankRange = *playerService.RankRange
	}

	return &pb.GetPlayerServicesByIdResp{PlayerServices: &pbPlayerService}, nil
}
