package ledger

import (
	"strings"
)

// RechargeBill 是充值账单的只读投影；充值事实由 wallet_recharge_records 保存。
type RechargeBill struct {
	AppCode                string
	TransactionID          string
	CommandID              string
	RechargeType           string
	Status                 string
	ExternalRef            string
	UserID                 int64
	SellerUserID           int64
	SellerRegionID         int64
	TargetRegionID         int64
	PolicyID               int64
	PolicyVersion          string
	CurrencyCode           string
	CoinAmount             int64
	USDMinorAmount         int64
	ExchangeCoinAmount     int64
	ExchangeUSDMinorAmount int64
	ProviderAmountMinor    int64
	CreatedAtMS            int64
	// ProviderCode 标识实际支付渠道：google_play/mifapay/v5pay/usdt_trc20。
	ProviderCode string
	// UserPaid* 是用户实付事实：外部订单来自三方下单/回调快照，Google 订单来自 Play Orders API。
	UserPaidCurrencyCode string
	UserPaidAmountMicro  int64
	// ProviderFee/Tax/Net 是三方扣款事实（实付币种微单位）；0 表示渠道未提供。
	ProviderFeeMicro int64
	ProviderTaxMicro int64
	ProviderNetMicro int64
	// PaidSyncedAtMS 仅对 Google 账单有意义：Orders API 实付明细的同步时间，0 表示未同步。
	PaidSyncedAtMS int64
}

// RechargeBillSummaryBucket 是充值账单的聚合桶：笔数、金币总数与美元最小单位总额。
type RechargeBillSummaryBucket struct {
	BillCount      int64
	CoinAmount     int64
	USDMinorAmount int64
}

// RechargeBillSummary 拆分财务系统关心的四个口径：总和、Google 充值、三方充值与币商进货。
type RechargeBillSummary struct {
	Total      RechargeBillSummaryBucket
	GooglePlay RechargeBillSummaryBucket
	ThirdParty RechargeBillSummaryBucket
	CoinSeller RechargeBillSummaryBucket
}

// UserRechargeStats 是用户维度的累计充值读模型；字段直接对应 wallet_user_recharge_stats 聚合行。
type UserRechargeStats struct {
	UserID              int64
	RechargeCount       int64
	TotalCoinAmount     int64
	TotalUSDMinorAmount int64
	FirstRechargedAtMS  int64
	UpdatedAtMS         int64
}

// ListRechargeBillsQuery 是后台查询所有充值渠道账单的筛选条件。
type ListRechargeBillsQuery struct {
	AppCode      string
	UserID       int64
	SellerUserID int64
	RegionID     int64
	RechargeType string
	Status       string
	Keyword      string
	// PaidState=unsynced 只保留实付明细未同步的谷歌账单。
	PaidState string
	StartAtMS int64
	EndAtMS   int64
	Page      int32
	PageSize  int32
}

// RechargeBillDailyBucket 是充值趋势的按日聚合桶；日期按查询时区切边界。
type RechargeBillDailyBucket struct {
	Date                 string
	GoogleUSDMinor       int64
	ThirdPartyUSDMinor   int64
	CoinSellerUSDMinor   int64
	GoogleCoinAmount     int64
	ThirdPartyCoinAmount int64
	CoinSellerCoinAmount int64
}

// RechargeBillRegionBucket 是按区域聚合的充值分布（用户充值按目标区域，币商按币商区域）。
type RechargeBillRegionBucket struct {
	RegionID       int64
	BillCount      int64
	USDMinorAmount int64
}

// RechargeBillGooglePaidStats 是谷歌实付同步覆盖度与扣费估算（USD 最小单位）。
type RechargeBillGooglePaidStats struct {
	GoogleBillCount int64
	SyncedCount     int64
	UnsyncedCount   int64
	CoveredUSDMinor int64
	EstFeeUSDMinor  int64
	EstTaxUSDMinor  int64
	EstNetUSDMinor  int64
}

// RechargeBillSalaryTransferStats 是用户提现口径中的钱包账本部分：用户把工资 USDT 转给币商。
type RechargeBillSalaryTransferStats struct {
	TransferCount    int64
	TransferUSDMinor int64
}

// RechargeBillOverview 汇集财务概览页所需的趋势、区域分布、谷歌扣费与提现口径。
type RechargeBillOverview struct {
	Daily          []RechargeBillDailyBucket
	Regions        []RechargeBillRegionBucket
	GooglePaid     RechargeBillGooglePaidStats
	SalaryTransfer RechargeBillSalaryTransferStats
}

// WalletFeatureFlags 是 App 钱包入口的开关投影；首版由 wallet-service 固定返回，后续可迁移到配置表。
type WalletFeatureFlags struct {
	RechargeEnabled        bool
	DiamondExchangeEnabled bool
}

// WalletOverview 是我的页和钱包首页共同使用的轻量摘要。
type WalletOverview struct {
	Balances     []AssetBalance
	FeatureFlags WalletFeatureFlags
}

// WalletValueSummary 是我的页钱包卡片的最小余额投影。
type WalletValueSummary struct {
	CoinAmount  int64
	UpdatedAtMS int64
}

// GiftWallResource 是礼物墙展示所需的资源快照字段，避免读取接口再依赖资源配置实时状态。
type GiftWallResource struct {
	AppCode           string
	ResourceID        int64
	ResourceCode      string
	ResourceType      string
	Name              string
	Status            string
	Grantable         bool
	GrantStrategy     string
	WalletAssetType   string
	WalletAssetAmount int64
	UsageScopes       []string
	AssetURL          string
	PreviewURL        string
	AnimationURL      string
	MetadataJSON      string
	SortOrder         int32
	CreatedAtMS       int64
	UpdatedAtMS       int64
}

// GiftWallItem 是当前用户收到礼物的按 gift_id 聚合投影。
type GiftWallItem struct {
	GiftID               string
	GiftName             string
	ResourceID           int64
	Resource             GiftWallResource
	ResourceSnapshotJSON string
	GiftTypeCode         string
	PresentationJSON     string
	GiftCount            int64
	TotalValue           int64
	TotalCoinValue       int64
	TotalDiamondValue    int64
	// TotalGiftPoint 是历史礼物墙字段，新送礼不再累加，保留用于旧数据展示兼容。
	TotalGiftPoint    int64
	TotalHeatValue    int64
	ChargeAssetType   string
	LastPriceVersion  string
	FirstReceivedAtMS int64
	LastReceivedAtMS  int64
	SortOrder         int32
}

// UserGiftWall 汇总礼物墙首屏需要的全量统计和按礼物类型聚合列表。
type UserGiftWall struct {
	Items             []GiftWallItem
	GiftKindCount     int64
	GiftTotalCount    int64
	TotalValue        int64
	TotalCoinValue    int64
	TotalDiamondValue int64
	// TotalGiftPoint 是历史礼物墙汇总字段，新送礼不再累加，保留用于旧数据展示兼容。
	TotalGiftPoint int64
	TotalHeatValue int64
}

// DiamondExchangeRule 是钻石兑换金币或余额的固定规则投影。
type DiamondExchangeRule struct {
	ExchangeType  string
	FromAssetType string
	ToAssetType   string
	FromAmount    int64
	ToAmount      int64
	Enabled       bool
}

// WalletTransaction 是钱包流水页按用户分录查询的投影。
type WalletTransaction struct {
	EntryID              int64
	TransactionID        string
	BizType              string
	AssetType            string
	AvailableDelta       int64
	FrozenDelta          int64
	AvailableAfter       int64
	FrozenAfter          int64
	CounterpartyUserID   int64
	RoomID               string
	CreatedAtMS          int64
	TransferUSDMinor     int64
	TransferCurrencyCode string
	EntryRole            string
	// HostBaseAmount / AgencyAmount 是礼物收益分录对应的拆分值；同人合并入账时两者同时有值。
	HostBaseAmount   int64
	AgencyAmount     int64
	PolicyID         uint64
	PolicyVersion    uint64
	RatioPointAmount int64
	RatioCoinAmount  int64
	GrossUSDMinor    int64
	FeeUSDMinor      int64
	NetUSDMinor      int64
	WithdrawFeeBPS   int64
}

// ListWalletTransactionsQuery 描述 App 钱包流水分页条件。
type ListWalletTransactionsQuery struct {
	AppCode   string
	UserID    int64
	AssetType string
	Page      int32
	PageSize  int32
	StartAtMS int64
	EndAtMS   int64
}

// AssetBalance 是账户当前可用/冻结余额投影。
type AssetBalance struct {
	AppCode         string
	UserID          int64
	AssetType       string
	AvailableAmount int64
	FrozenAmount    int64
	Version         int64
	UpdatedAtMs     int64
}

// AdminCreditAssetCommand 是后台手动调账的最小审计命令，Amount 为正数入账、负数扣账。
type AdminCreditAssetCommand struct {
	AppCode        string
	CommandID      string
	TargetUserID   int64
	AssetType      string
	Amount         int64
	OperatorUserID int64
	Reason         string
	Remark         string
	EvidenceRef    string
}

// ValidAssetType 限定账本允许落账的资产类型，避免写入任意字符串资产。
func ValidAssetType(assetType string) bool {
	switch assetType {
	case AssetCoin, AssetCoinSellerCoin, AssetPoint, AssetPointDiamond, AssetCoinSellerPoint, AssetRobotCoin, AssetGiftPoint, AssetHostSalaryUSD, AssetAgencySalaryUSD, AssetBDSalaryUSD, AssetAdminSalaryUSD:
		return true
	default:
		return false
	}
}

// ValidSalaryAssetType 只允许身份工资美元钱包进入工资兑换和转币商链路，避免普通金币或币商库存被误扣。
func ValidSalaryAssetType(assetType string) bool {
	switch strings.ToUpper(strings.TrimSpace(assetType)) {
	case AssetHostSalaryUSD, AssetAgencySalaryUSD, AssetBDSalaryUSD, AssetAdminSalaryUSD:
		return true
	default:
		return false
	}
}

// UsesHostAgencySalaryPolicy identifies the two balances produced by Host policy settlement.
// Withdrawal rules follow the asset's business source instead of branching on an application name.
func UsesHostAgencySalaryPolicy(assetType string) bool {
	switch strings.ToUpper(strings.TrimSpace(assetType)) {
	case AssetHostSalaryUSD, AssetAgencySalaryUSD:
		return true
	default:
		return false
	}
}

// ValidPointWithdrawalAssetType 只允许历史政策积分进入积分提现链路，避免普通 COIN 被当成积分出款。
func ValidPointWithdrawalAssetType(assetType string) bool {
	switch strings.ToUpper(strings.TrimSpace(assetType)) {
	case AssetPoint, AssetPointDiamond, AssetCoinSellerPoint:
		return true
	default:
		return false
	}
}
