package wallet

import (
	"time"

	"hyapp/services/wallet-service/internal/service/wallet/ports"
	giftusecase "hyapp/services/wallet-service/internal/service/wallet/usecase/gift"
)

const walletBadgeGrantMetric = "wallet_badge_grant"

// Service 承载钱包账务用例。
type Service struct {
	repository       ports.Repository
	giftLedger       *giftusecase.Service
	activity         ports.ActivityBadgeClient
	googlePlay       ports.GooglePlayClient
	mifaPay          ports.MifaPayClient
	v5Pay            ports.V5PayClient
	tronUSDT         ports.TronUSDTClient
	bscUSDT          ports.BSCUSDTClient
	binance          ports.BinanceClient
	externalRecharge ExternalRechargeConfig
	now              func() time.Time
}

// New 创建钱包服务。
func New(repository ports.Repository, activity ...ports.ActivityBadgeClient) *Service {
	var activityClient ports.ActivityBadgeClient
	if len(activity) > 0 {
		activityClient = activity[0]
	}
	return &Service{
		repository: repository,
		giftLedger: giftusecase.New(repository),
		activity:   activityClient,
		now:        time.Now,
	}
}

// SetGooglePlayClient 注入 Google Play Developer API 客户端；未配置时 confirm 返回依赖不可用。
func (s *Service) SetGooglePlayClient(client ports.GooglePlayClient) {
	s.googlePlay = client
}

// SetMifaPayClient 注入 MiFaPay 下单和验签客户端；未配置时 H5 MiFaPay 下单返回依赖不可用。
func (s *Service) SetMifaPayClient(client ports.MifaPayClient) {
	s.mifaPay = client
}

// SetV5PayClient 注入 V5Pay 下单、查询和验签客户端；未配置时 H5 V5Pay 下单返回依赖不可用。
func (s *Service) SetV5PayClient(client ports.V5PayClient) {
	s.v5Pay = client
}

// SetTronUSDTClient 注入 TRON TRC20 交易校验客户端；未配置时 USDT tx 提交只会保留待确认订单。
func (s *Service) SetTronUSDTClient(client ports.TronUSDTClient) {
	s.tronUSDT = client
}

// SetBSCUSDTClient 注入 BSC BEP20 交易校验客户端；默认不配置，避免误把公共 RPC 当成生产依赖。
func (s *Service) SetBSCUSDTClient(client ports.BSCUSDTClient) {
	s.bscUSDT = client
}

// SetBinanceClient 注入 Binance 入金只读查询客户端；H5 充值和后台币商充值共用按 App 隔离的 off-chain 查账。
func (s *Service) SetBinanceClient(client ports.BinanceClient) {
	s.binance = client
}

// SetExternalRechargeConfig 保存 H5 外部充值的站内公共配置，不把密钥或地址散落到 handler。
func (s *Service) SetExternalRechargeConfig(config ExternalRechargeConfig) {
	s.externalRecharge = normalizeExternalRechargeConfig(config)
}
