package ledger

import (
	"strings"
)

// RedPacketConfig 是后台红包配置的服务端事实，发送路径必须由 wallet-service 强校验。
type RedPacketConfig struct {
	AppCode            string
	Enabled            bool
	CountTiers         []int32
	AmountTiers        []int64
	DelayedOpenSeconds int32
	ExpireSeconds      int32
	DailySendLimit     int32
	UpdatedByAdminID   int64
	RuleURL            string
	CreatedAtMS        int64
	UpdatedAtMS        int64
}

// RedPacket 是红包资金池和状态的只读投影。
type RedPacket struct {
	AppCode           string
	PacketID          string
	CommandID         string
	SenderUserID      int64
	RoomID            string
	RegionID          int64
	PacketType        string
	TotalAmount       int64
	PacketCount       int32
	RemainingAmount   int64
	RemainingCount    int32
	Status            string
	OpenAtMS          int64
	ExpiresAtMS       int64
	CreatedAtMS       int64
	UpdatedAtMS       int64
	ClaimedByViewer   bool
	ViewerClaimAmount int64
	Claims            []RedPacketClaim
	RefundAmount      int64
	RefundStatus      string
	RefundedAtMS      int64
}

// RedPacketClaim 是单个用户抢红包的到账事实。
type RedPacketClaim struct {
	AppCode             string
	ClaimID             string
	CommandID           string
	PacketID            string
	UserID              int64
	Amount              int64
	WalletTransactionID string
	Status              string
	FailureReason       string
	CreatedAtMS         int64
	UpdatedAtMS         int64
}

type RedPacketCreateCommand struct {
	AppCode      string
	CommandID    string
	SenderUserID int64
	RoomID       string
	RegionID     int64
	PacketType   string
	TotalAmount  int64
	PacketCount  int32
	// RoomLocked 是 gateway 从 room-service snapshot 读取的展示快照，只随红包资金事实透传。
	RoomLocked bool
	// OwnerUserID 与 RoomLocked 来自同一份房间快照；wallet 不解释房主权限，只随已提交红包事实透传。
	OwnerUserID int64
}

type RedPacketCreateReceipt struct {
	Packet       RedPacket
	BalanceAfter int64
}

type RedPacketClaimCommand struct {
	AppCode   string
	CommandID string
	PacketID  string
	UserID    int64
}

type RedPacketClaimReceipt struct {
	Claim        RedPacketClaim
	Packet       RedPacket
	BalanceAfter int64
}

type RedPacketRefundReceipt struct {
	Packet         RedPacket
	RefundedAmount int64
}

type RedPacketQuery struct {
	AppCode      string
	RoomID       string
	SenderUserID int64
	RegionID     int64
	PacketType   string
	Status       string
	ViewerUserID int64
	StartAtMS    int64
	EndAtMS      int64
	Page         int32
	PageSize     int32
}

type RedPacketExpireResult struct {
	ExpiredCount   int32
	RefundedAmount int64
}

func NormalizeRedPacketType(packetType string) string {
	switch strings.ToUpper(strings.TrimSpace(packetType)) {
	case "IMMEDIATE":
		return RedPacketTypeNormal
	case "DELAYED":
		return RedPacketTypeDelayed
	}
	switch strings.ToLower(strings.TrimSpace(packetType)) {
	case RedPacketTypeNormal:
		return RedPacketTypeNormal
	case RedPacketTypeDelayed:
		return RedPacketTypeDelayed
	default:
		return ""
	}
}

func NormalizeRedPacketStatus(status string) string {
	switch strings.ToLower(strings.TrimSpace(status)) {
	case RedPacketStatusWaitingOpen:
		return RedPacketStatusWaitingOpen
	case RedPacketStatusActive:
		return RedPacketStatusActive
	case RedPacketStatusFinished:
		return RedPacketStatusFinished
	case RedPacketStatusRefunded:
		return RedPacketStatusRefunded
	default:
		return ""
	}
}
