package ports

import (
	"context"

	"google.golang.org/grpc"
	activityv1 "hyapp.local/api/proto/activity/v1"
	"hyapp/services/wallet-service/internal/domain/ledger"
)

// ActivityBadgeClient 是 wallet -> activity 的最小投影接口，避免 wallet 依赖 activity 内部包。
type ActivityBadgeClient interface {
	ConsumeAchievementEvent(ctx context.Context, req *activityv1.ConsumeAchievementEventRequest, opts ...grpc.CallOption) (*activityv1.ConsumeAchievementEventResponse, error)
}

// GooglePlayClient 隔离 Google Play Developer API，service 只依赖购买校验、消耗和订单实付查询能力。
type GooglePlayClient interface {
	GetProductPurchase(ctx context.Context, packageName string, purchaseToken string) (ledger.GooglePlayPurchase, error)
	ConsumeProduct(ctx context.Context, packageName string, productID string, purchaseToken string) error
	GetOrder(ctx context.Context, packageName string, orderID string) (ledger.GooglePlayOrder, error)
}

// MifaPayCreateOrderRequest 是 wallet-service 传给 MiFaPay client 的已校验下单快照。
type MifaPayCreateOrderRequest struct {
	OrderID             string
	TargetUserID        int64
	ProductName         string
	CoinAmount          int64
	AmountMinor         int64
	CurrencyCode        string
	CountryCode         string
	PayWay              string
	PayType             string
	NotifyURL           string
	ReturnURL           string
	ClientIP            string
	Language            string
	ProviderAmountMinor int64
	PayerName           string
	PayerAccount        string
	PayerEmail          string
}

type MifaPayCreateOrderResponse struct {
	OrderID         string
	ProviderOrderID string
	PayURL          string
	RawJSON         string
}

type MifaPayQueryOrderRequest struct {
	OrderID          string
	ProviderOrderID  string
	OrderCreatedAtMS int64
}

type MifaPayQueryOrderResponse struct {
	OrderID         string
	ProviderOrderID string
	Status          string
	Amount          string
	Currency        string
	PayType         string
	RawJSON         string
}

type MifaPayNotifyData struct {
	OrderID         string
	ProviderOrderID string
	OrderStatus     string
	Amount          string
	Currency        string
	PayType         string
	RawJSON         string
}

// V5PayCreateOrderRequest 是 wallet-service 传给 V5Pay client 的已校验下单快照。
type V5PayCreateOrderRequest struct {
	OrderID             string
	TargetUserID        int64
	ProductName         string
	CoinAmount          int64
	AmountMinor         int64
	CurrencyCode        string
	CountryCode         string
	ProductType         string
	NotifyURL           string
	ReturnURL           string
	Language            string
	ProviderAmountMinor int64
	PayerName           string
	PayerAccount        string
	PayerEmail          string
}

type V5PayCreateOrderResponse struct {
	OrderID         string
	ProviderOrderID string
	PayURL          string
	RawJSON         string
}

type V5PayQueryOrderRequest struct {
	OrderID         string
	ProviderOrderID string
	CountryCode     string
	CurrencyCode    string
}

type V5PayQueryOrderResponse struct {
	OrderID         string
	ProviderOrderID string
	Status          string
	Amount          string
	Currency        string
	ProductType     string
	RawJSON         string
}

type V5PayNotifyData struct {
	OrderID         string
	ProviderOrderID string
	OrderStatus     string
	Amount          string
	Currency        string
	ProductType     string
	RawJSON         string
}

type V5PayProductTypesRequest struct {
	CountryCode  string
	CurrencyCode string
}

type V5PayProductType struct {
	ProductType        string
	ProductName        string
	Description        string
	Logo               string
	Currency           string
	ProductMode        string
	SupportCashierMode int
}

type V5PayProductTypesResponse struct {
	PayinList []V5PayProductType
	RawJSON   string
}

// BinanceTransferLookupRequest 是 H5 充值和后台 USDT 币商充值共用的 Binance 收款查单条件。
// OrderTimeMS 是运营看到的付款时间；Pay History 每次最多返回 100 条，因此必须用它缩小高频账户的检索窗口。
type BinanceTransferLookupRequest struct {
	AppCode     string
	ExternalID  string
	Coin        string
	Network     string
	ToAddress   string
	AmountMinor int64
	OrderTimeMS int64
}

// BinanceTransferRecord 是链上 Deposit History 或站内 Pay History 命中的只读快照。
// Source 明确事实来源；Pay 记录没有链上网络和地址，上层不得把配置地址误写成 Binance 返回事实。
type BinanceTransferRecord struct {
	Source         string
	ID             string
	TxID           string
	Coin           string
	Network        string
	Status         int64
	Address        string
	Amount         string
	AmountMinor    int64
	TransferType   int64
	InsertTimeMS   int64
	CompleteTimeMS int64
	ConfirmTimes   string
	UnlockConfirm  int64
	WalletType     int64
	RawJSON        string
}

// MifaPayClient 隔离 MiFaPay RSA 签名、验签和 HTTP 协议细节，service 只处理订单状态。
type MifaPayClient interface {
	CreateOrder(ctx context.Context, req MifaPayCreateOrderRequest) (MifaPayCreateOrderResponse, error)
	QueryOrder(ctx context.Context, req MifaPayQueryOrderRequest) (MifaPayQueryOrderResponse, error)
	ParseNotification(notification ledger.MifaPayNotification) (MifaPayNotifyData, error)
}

// V5PayClient 隔离 V5Pay MD5 签名、验签和 HTTP 协议细节，service 只处理订单状态。
type V5PayClient interface {
	CreateOrder(ctx context.Context, req V5PayCreateOrderRequest) (V5PayCreateOrderResponse, error)
	QueryOrder(ctx context.Context, req V5PayQueryOrderRequest) (V5PayQueryOrderResponse, error)
	ListProductTypes(ctx context.Context, req V5PayProductTypesRequest) (V5PayProductTypesResponse, error)
	ParseNotification(notification ledger.V5PayNotification) (V5PayNotifyData, error)
}

// TronUSDTClient 校验用户提交的 TRC20 tx_hash 是否真实打到平台共享地址。
type TronUSDTClient interface {
	VerifyUSDTTransfer(ctx context.Context, txHash string, toAddress string, amountMinor int64) (string, error)
}

// BSCUSDTClient 校验用户提交的 BEP20 tx_hash 是否真实打到平台共享地址。
type BSCUSDTClient interface {
	VerifyUSDTTransfer(ctx context.Context, txHash string, toAddress string, amountMinor int64) (string, error)
}

// BinanceClient 隔离 Binance signed API；service 只关心收款事实是否匹配当前 App 订单。
type BinanceClient interface {
	FindUSDTTransfer(ctx context.Context, req BinanceTransferLookupRequest) (BinanceTransferRecord, error)
}
