package grpc

import (
	"context"

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

// AdminCreditAsset 处理后台手动调账，公网权限和审计入口由 gateway/admin 层控制。
func (s *Server) AdminCreditAsset(ctx context.Context, req *walletv1.AdminCreditAssetRequest) (*walletv1.AdminCreditAssetResponse, error) {
	ctx = appcode.WithContext(ctx, req.GetAppCode())
	balance, transactionID, err := s.svc.AdminCreditAsset(ctx, ledger.AdminCreditAssetCommand{
		AppCode:        req.GetAppCode(),
		CommandID:      req.GetCommandId(),
		TargetUserID:   req.GetTargetUserId(),
		AssetType:      req.GetAssetType(),
		Amount:         req.GetAmount(),
		OperatorUserID: req.GetOperatorUserId(),
		Reason:         req.GetReason(),
		Remark:         req.GetRemark(),
		EvidenceRef:    req.GetEvidenceRef(),
	})
	if err != nil {
		return nil, xerr.ToGRPCError(err)
	}

	return &walletv1.AdminCreditAssetResponse{
		TransactionId: transactionID,
		Balance: &walletv1.AssetBalance{
			AppCode:         balance.AppCode,
			AssetType:       balance.AssetType,
			AvailableAmount: balance.AvailableAmount,
			FrozenAmount:    balance.FrozenAmount,
			Version:         balance.Version,
		},
	}, nil
}
