package ledger

import "strings"

const (
	// AssetCoin 是用户送礼消费资产。
	AssetCoin = "COIN"
	// AssetCoinSellerCoin 是币商专用金币资产，只能通过币商转账兑换成玩家普通 COIN。
	AssetCoinSellerCoin = "COIN_SELLER_COIN"
	// AssetGiftPoint 是主播礼物积分，不能直接消费或提现。
	AssetGiftPoint = "GIFT_POINT"
	// AssetDiamond 是后续兑换用资产，当前只在余额模型中保留。
	AssetDiamond = "DIAMOND"
	// AssetUSDBalance 是主播可提现美元余额，单位由后续提现策略固定。
	AssetUSDBalance = "USD_BALANCE"

	// StockTypeUSDTPurchase 表示币商线下 USDT 进货后发放专用金币库存。
	StockTypeUSDTPurchase = "usdt_purchase"
	// StockTypeCoinCompensation 表示平台人工补偿币商专用金币库存，不计入进货金额。
	StockTypeCoinCompensation = "coin_compensation"
	// PaidCurrencyUSDT 是首版币商进货支持的唯一线下付款币种。
	PaidCurrencyUSDT = "USDT"
	// RechargeProductStatusActive 表示内购商品已上架，可进入 App 充值页。
	RechargeProductStatusActive = "active"
	// RechargeProductStatusDisabled 表示内购商品未上架，仅后台可见。
	RechargeProductStatusDisabled = "disabled"
	// RechargeProductPlatformAndroid 表示 Google Play 侧内购商品。
	RechargeProductPlatformAndroid = "android"
	// RechargeProductPlatformIOS 表示 Apple IAP 侧内购商品。
	RechargeProductPlatformIOS = "ios"
	// RechargeChannelGoogle 是 android 平台内购渠道标识。
	RechargeChannelGoogle = "google"
	// RechargeChannelApple 是 iOS 平台内购渠道标识。
	RechargeChannelApple = "apple"

	// WithdrawalStatusPending 表示主播美元余额提现已提交，等待后台人工审核和线下转账。
	WithdrawalStatusPending = "pending"

	// VipStatusActive 表示 VIP 配置或用户会员状态当前有效。
	VipStatusActive = "active"
	// VipStatusDisabled 表示 VIP 配置被后台停用。
	VipStatusDisabled = "disabled"

	// GameOpDebit 表示游戏平台下注、局内道具等扣金币行为。
	GameOpDebit = "debit"
	// GameOpCredit 表示游戏平台中奖、派奖等加金币行为。
	GameOpCredit = "credit"
	// GameOpRefund 表示游戏平台扣款失败或取消后的退款入账。
	GameOpRefund = "refund"
	// GameOpReverse 表示平台冲正；首版按扣回用户 COIN 处理。
	GameOpReverse = "reverse"
)

// DebitGiftCommand 是 room-service 送礼扣费的账务命令。
type DebitGiftCommand struct {
	AppCode      string
	CommandID    string
	RoomID       string
	SenderUserID int64
	TargetUserID int64
	GiftID       string
	GiftCount    int32
	PriceVersion string
	RegionID     int64
}

// Receipt 是账务命令落账后的稳定回执。
type Receipt struct {
	BillingReceiptID string
	TransactionID    string
	CoinSpent        int64
	ChargeAssetType  string
	ChargeAmount     int64
	GiftPointAdded   int64
	HeatValue        int64
	PriceVersion     string
	BalanceAfter     int64
}

// CoinSellerTransferCommand 是币商给玩家转普通金币的账务命令。
type CoinSellerTransferCommand struct {
	AppCode        string
	CommandID      string
	SellerUserID   int64
	TargetUserID   int64
	SellerRegionID int64
	TargetRegionID int64
	Amount         int64
	Reason         string
}

// CoinSellerTransferReceipt 是币商转账完成后的稳定回执。
type CoinSellerTransferReceipt struct {
	TransactionID              string
	SellerBalanceAfter         int64
	TargetBalanceAfter         int64
	Amount                     int64
	RechargeUSDMinor           int64
	RechargeCurrencyCode       string
	RechargePolicyID           int64
	RechargePolicyVersion      string
	RechargePolicyCoinAmount   int64
	RechargePolicyUSDMinorUnit int64
}

// CoinSellerStockCreditCommand 是后台给币商专用金币库存入账的最小账务命令。
type CoinSellerStockCreditCommand struct {
	AppCode          string
	CommandID        string
	SellerUserID     int64
	StockType        string
	CoinAmount       int64
	PaidCurrencyCode string
	PaidAmountMicro  int64
	PaymentRef       string
	EvidenceRef      string
	OperatorUserID   int64
	Reason           string
}

// CoinSellerStockCreditReceipt 是币商库存入账成功后的稳定回执。
type CoinSellerStockCreditReceipt struct {
	TransactionID          string
	SellerUserID           int64
	StockType              string
	CoinAmount             int64
	PaidCurrencyCode       string
	PaidAmountMicro        int64
	CountsAsSellerRecharge bool
	BalanceAfter           int64
	CreatedAtMS            int64
}

// 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
	CreatedAtMS            int64
}

// ListRechargeBillsQuery 是后台查询所有充值渠道账单的筛选条件。
type ListRechargeBillsQuery struct {
	AppCode      string
	UserID       int64
	SellerUserID int64
	RegionID     int64
	RechargeType string
	Status       string
	Keyword      string
	StartAtMS    int64
	EndAtMS      int64
	Page         int32
	PageSize     int32
}

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

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

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

// RechargeProduct 是用户充值页读取的充值档位。
type RechargeProduct struct {
	AppCode           string
	ProductID         int64
	ProductCode       string
	ProductName       string
	Description       string
	Platform          string
	Channel           string
	CurrencyCode      string
	AmountMicro       int64
	AmountMinor       int64
	CoinAmount        int64
	PolicyVersion     string
	Enabled           bool
	Status            string
	SortOrder         int32
	RegionIDs         []int64
	ResourceAssetType string
	CreatedByUserID   int64
	UpdatedByUserID   int64
	CreatedAtMS       int64
	UpdatedAtMS       int64
}

// ListRechargeProductsQuery 是后台内购商品配置列表的筛选条件。
type ListRechargeProductsQuery struct {
	AppCode  string
	Status   string
	Platform string
	RegionID int64
	Keyword  string
	Page     int32
	PageSize int32
}

// RechargeProductCommand 是后台创建或更新内购商品配置的完整事实输入。
type RechargeProductCommand struct {
	AppCode        string
	ProductID      int64
	AmountMicro    int64
	CoinAmount     int64
	ProductName    string
	Description    string
	Platform       string
	RegionIDs      []int64
	Enabled        bool
	OperatorUserID 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
}

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

// WithdrawalCommand 是主播余额提现申请命令。人工审核和线下转账不在 App 主链路完成。
type WithdrawalCommand struct {
	AppCode       string
	CommandID     string
	UserID        int64
	Amount        int64
	PayoutAccount string
	Reason        string
}

// WithdrawalRequest 是提现申请事实。
type WithdrawalRequest struct {
	WithdrawalID  string
	UserID        int64
	AssetType     string
	Amount        int64
	Status        string
	PayoutAccount string
	Reason        string
	CreatedAtMS   int64
	UpdatedAtMS   int64
}

// VipRewardItem 是 VIP 资源组权益的轻量展示投影。
type VipRewardItem struct {
	ResourceID   int64
	ResourceCode string
	ResourceType string
	Name         string
	Quantity     int64
	ExpiresAtMS  int64
}

// VipLevel 是可购买 VIP 等级配置。
type VipLevel struct {
	Level                 int32
	Name                  string
	Status                string
	PriceCoin             int64
	DurationMS            int64
	RewardResourceGroupID int64
	RewardItems           []VipRewardItem
	CanPurchase           bool
	SortOrder             int32
}

// UserVip 是用户当前会员状态。是否有效以 ExpiresAtMS 和当前时间判断。
type UserVip struct {
	UserID      int64
	Level       int32
	Name        string
	Active      bool
	StartedAtMS int64
	ExpiresAtMS int64
	UpdatedAtMS int64
}

// PurchaseVipCommand 是 App 购买或升级 VIP 的账务命令。
type PurchaseVipCommand struct {
	AppCode   string
	CommandID string
	UserID    int64
	Level     int32
}

// PurchaseVipReceipt 是购买 VIP 成功后的稳定回执。
type PurchaseVipReceipt struct {
	OrderID          string
	TransactionID    string
	Vip              UserVip
	CoinSpent        int64
	CoinBalanceAfter int64
	RewardItems      []VipRewardItem
}

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

// AdminCreditAssetCommand 是后台手动入账的最小审计命令。
type AdminCreditAssetCommand struct {
	AppCode        string
	CommandID      string
	TargetUserID   int64
	AssetType      string
	Amount         int64
	OperatorUserID int64
	Reason         string
	EvidenceRef    string
}

// TaskRewardCommand 是 activity-service 任务领奖的 COIN 入账命令。
type TaskRewardCommand struct {
	AppCode      string
	CommandID    string
	TargetUserID int64
	Amount       int64
	TaskType     string
	TaskID       string
	CycleKey     string
	Reason       string
}

// TaskRewardReceipt 是任务奖励入账后的稳定回执。
type TaskRewardReceipt struct {
	TransactionID string
	Balance       AssetBalance
	Amount        int64
	GrantedAtMS   int64
}

// GameCoinChangeCommand 是 game-service 对钱包发起的游戏专用金币改账命令。
type GameCoinChangeCommand struct {
	AppCode         string
	CommandID       string
	UserID          int64
	PlatformCode    string
	GameID          string
	ProviderOrderID string
	ProviderRoundID string
	OpType          string
	CoinAmount      int64
	RoomID          string
	RequestHash     string
}

// GameCoinChangeReceipt 是游戏改账成功或幂等重放后的稳定回执。
type GameCoinChangeReceipt struct {
	TransactionID    string
	BalanceAfter     int64
	IdempotentReplay bool
}

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

func NormalizeGiftChargeAssetType(assetType string) string {
	switch strings.ToUpper(strings.TrimSpace(assetType)) {
	case AssetDiamond:
		return AssetDiamond
	default:
		return AssetCoin
	}
}

func ValidGiftChargeAssetType(assetType string) bool {
	switch strings.ToUpper(strings.TrimSpace(assetType)) {
	case AssetCoin, AssetDiamond:
		return true
	default:
		return false
	}
}

func NormalizeCoinSellerStockType(stockType string) string {
	switch strings.ToLower(strings.TrimSpace(stockType)) {
	case StockTypeUSDTPurchase:
		return StockTypeUSDTPurchase
	case StockTypeCoinCompensation:
		return StockTypeCoinCompensation
	default:
		return ""
	}
}

func NormalizeRechargeProductStatus(status string) string {
	switch strings.ToLower(strings.TrimSpace(status)) {
	case RechargeProductStatusActive:
		return RechargeProductStatusActive
	case RechargeProductStatusDisabled:
		return RechargeProductStatusDisabled
	default:
		return ""
	}
}

func NormalizeRechargeProductPlatform(platform string) string {
	switch strings.ToLower(strings.TrimSpace(platform)) {
	case RechargeProductPlatformAndroid:
		return RechargeProductPlatformAndroid
	case RechargeProductPlatformIOS:
		return RechargeProductPlatformIOS
	default:
		return ""
	}
}

func RechargeChannelForPlatform(platform string) string {
	switch NormalizeRechargeProductPlatform(platform) {
	case RechargeProductPlatformAndroid:
		return RechargeChannelGoogle
	case RechargeProductPlatformIOS:
		return RechargeChannelApple
	default:
		return ""
	}
}

func NormalizeGameOpType(opType string) string {
	switch strings.ToLower(strings.TrimSpace(opType)) {
	case GameOpDebit:
		return GameOpDebit
	case GameOpCredit:
		return GameOpCredit
	case GameOpRefund:
		return GameOpRefund
	case GameOpReverse:
		return GameOpReverse
	default:
		return ""
	}
}
