package grpc

import (
	"context"

	walletv1 "hyapp.local/api/proto/wallet/v1"
	"hyapp/pkg/appcode"
	"hyapp/pkg/xerr"
	"hyapp/services/wallet-service/internal/domain/ledger"
)

// ApplyGameCoinChange 处理 game-service 发起的游戏专用金币改账。
func (s *Server) ApplyGameCoinChange(ctx context.Context, req *walletv1.ApplyGameCoinChangeRequest) (*walletv1.ApplyGameCoinChangeResponse, error) {
	ctx = appcode.WithContext(ctx, req.GetAppCode())
	receipt, err := s.svc.ApplyGameCoinChange(ctx, ledger.GameCoinChangeCommand{
		AppCode:         req.GetAppCode(),
		CommandID:       req.GetCommandId(),
		UserID:          req.GetUserId(),
		PlatformCode:    req.GetPlatformCode(),
		GameID:          req.GetGameId(),
		ProviderOrderID: req.GetProviderOrderId(),
		ProviderRoundID: req.GetProviderRoundId(),
		OpType:          req.GetOpType(),
		CoinAmount:      req.GetCoinAmount(),
		RoomID:          req.GetRoomId(),
		RequestHash:     req.GetRequestHash(),
	})
	if err != nil {
		return nil, xerr.ToGRPCError(err)
	}
	return &walletv1.ApplyGameCoinChangeResponse{
		WalletTransactionId: receipt.TransactionID,
		BalanceAfter:        receipt.BalanceAfter,
		IdempotentReplay:    receipt.IdempotentReplay,
	}, nil
}
