package growth

const (
	TrackWealth = "wealth"
	TrackGame   = "game"
	TrackCharm  = "charm"

	StatusActive   = "active"
	StatusDisabled = "disabled"

	RewardStatusPending  = "pending"
	RewardStatusRunning  = "running"
	RewardStatusGranted  = "granted"
	RewardStatusFailed   = "failed"
	RewardStatusCanceled = "canceled"

	RewardOriginOrganic   = "organic"
	RewardOriginTemporary = "temporary"

	RevokeStatusNone    = "none"
	RevokeStatusPending = "pending"
	RevokeStatusRunning = "running"
	RevokeStatusRevoked = "revoked"
	RevokeStatusFailed  = "failed"
	RevokeStatusSkipped = "skipped"

	TemporaryLevelStatusActive     = "active"
	TemporaryLevelStatusSuperseded = "superseded"
	TemporaryLevelStatusExpired    = "expired"

	NoticeStatusPending = "pending"
	NoticeStatusSent    = "sent"
	NoticeStatusSkipped = "skipped"
	NoticeStatusFailed  = "failed"

	RewardSourceLevel = "level"
	RewardSourceTier  = "tier"

	EventStatusConsumed  = "consumed"
	EventStatusDuplicate = "duplicate"

	MetricGiftSpendCoin     = "gift_spend_coin"
	MetricReceivedGiftValue = "received_gift_value"
	MetricGameSpendCoin     = "game_spend_coin"

	GrantSourceGrowthLevel = "growth_level"
)

// Track 是等级体系的一条成长轨道；默认有 wealth/game/charm 三条。
type Track struct {
	AppCode           string
	Track             string
	Name              string
	Status            string
	SortOrder         int32
	DisplayConfigJSON string
	CreatedAtMS       int64
	UpdatedAtMS       int64
}

// Rule 是单个等级的阈值和可选奖励配置。
type Rule struct {
	AppCode               string
	Track                 string
	Level                 int32
	RequiredValue         int64
	Name                  string
	Status                string
	RewardResourceGroupID int64
	SortOrder             int32
	DisplayConfigJSON     string
	CreatedByAdminID      int64
	UpdatedByAdminID      int64
	CreatedAtMS           int64
	UpdatedAtMS           int64
}

// Tier 是等级段展示配置；等级段奖励通常用于永久头像框和徽章。
type Tier struct {
	AppCode                      string
	TierID                       int64
	Track                        string
	MinLevel                     int32
	MaxLevel                     int32
	Name                         string
	DisplayAvatarFrameResourceID int64
	DisplayBadgeResourceID       int64
	RewardResourceGroupID        int64
	Status                       string
	DisplayConfigJSON            string
	CreatedByAdminID             int64
	UpdatedByAdminID             int64
	CreatedAtMS                  int64
	UpdatedAtMS                  int64
}

// Account 是用户在某条轨道上的累计值和当前等级。
type Account struct {
	AppCode          string
	UserID           int64
	Track            string
	TotalValue       int64
	CurrentLevel     int32
	CurrentTierID    int64
	LevelUpdatedAtMS int64
	TierUpdatedAtMS  int64
	Version          int64
	UpdatedAtMS      int64
}

// TrackOverview 是 App 等级页展示的单轨道摘要。
type TrackOverview struct {
	Track                        string
	Name                         string
	Level                        int32
	TierID                       int64
	TotalValue                   int64
	CurrentLevelRequiredValue    int64
	NextLevel                    int32
	NextLevelRequiredValue       int64
	DisplayAvatarFrameResourceID int64
	DisplayBadgeResourceID       int64
	DisplayBadgeSourceLevel      int32
	RewardPendingCount           int64
	SortOrder                    int32
}

// LevelDisplayTrackProfile 是房间资料、资料卡这类轻量展示面读取的单轨道投影。
type LevelDisplayTrackProfile struct {
	Track                 string
	Level                 int32
	TierID                int64
	AvatarFrameResourceID int64
	BadgeResourceID       int64
	BadgeSourceLevel      int32
	UpdatedAtMS           int64
}

// UserLevelDisplayProfile 固定携带 wealth/game/charm 三条轨道，避免展示层按数组顺序猜含义。
type UserLevelDisplayProfile struct {
	UserID int64
	Wealth LevelDisplayTrackProfile
	Game   LevelDisplayTrackProfile
	Charm  LevelDisplayTrackProfile
}

// LevelDisplayProfiles 是批量用户展示投影的只读快照。
type LevelDisplayProfiles struct {
	Profiles     []UserLevelDisplayProfile
	ServerTimeMS int64
}

// RegistrationLevelBadgeGrant 是注册后按等级规则补发的 1 级等级徽章发放结果。
type RegistrationLevelBadgeGrant struct {
	Track      string
	Level      int32
	ResourceID int64
	GrantID    string
}

type RegistrationLevelBadgeGrants struct {
	Grants       []RegistrationLevelBadgeGrant
	ServerTimeMS int64
}

// TrackDetail 返回一个轨道的完整阈值、等级段和当前用户进度。
type TrackDetail struct {
	Overview     TrackOverview
	Rules        []Rule
	Tiers        []Tier
	ServerTimeMS int64
}

// Config 是后台等级配置页读取的完整配置快照；它不携带任何用户进度。
type Config struct {
	Tracks       []Track
	Rules        []Rule
	Tiers        []Tier
	ServerTimeMS int64
}

// ConfigQuery 约束后台等级配置读取范围；status 为空时返回全部状态。
type ConfigQuery struct {
	Track  string
	Status string
}

// Overview 是 App 我的等级总览。
type Overview struct {
	Tracks       []TrackOverview
	ServerTimeMS int64
}

// ValueEvent 是事件 relay 投递给等级系统的幂等等级增量。
type ValueEvent struct {
	EventID         string
	SourceEventID   string
	SourceService   string
	SourceEventType string
	UserID          int64
	Track           string
	MetricType      string
	ValueDelta      int64
	OccurredAtMS    int64
	DimensionsJSON  string
}

// EventResult 表达等级事件消费结果；重复事件也返回成功语义。
type EventResult struct {
	EventID        string
	Status         string
	LevelChanged   bool
	PreviousLevel  int32
	NewLevel       int32
	RewardJobCount int64
}

// SetUserLevelCommand 描述经理中心直接设置用户等级的事实输入。
// TotalValue 不由调用方提交，activity-service 会读取目标等级阈值后在事务内写入。
type SetUserLevelCommand struct {
	CommandID      string
	UserID         int64
	Track          string
	Level          int32
	OperatorUserID int64
	Reason         string
}

// SetUserLevelResult 返回经理设置等级后的账户事实和补发奖励数量。
type SetUserLevelResult struct {
	EventID        string
	Status         string
	Track          string
	PreviousLevel  int32
	NewLevel       int32
	TotalValue     int64
	RewardJobCount int64
	ServerTimeMS   int64
}

// TemporaryLevel 是真实成长账户之上的限时展示层。BaselineTotalValue 只做审计，
// 有效期经验始终按 value event 的 occurred_at_ms 落在 [StartedAtMS, ExpiresAtMS) 内聚合。
type TemporaryLevel struct {
	AppCode                string
	TemporaryLevelID       string
	CommandID              string
	UserID                 int64
	Track                  string
	TargetLevel            int32
	TargetRequiredValue    int64
	BaselineTotalValue     int64
	StartedAtMS            int64
	ExpiresAtMS            int64
	Status                 string
	Generation             int32
	OperatorAdminID        int64
	Reason                 string
	FinalLevel             int32
	ActivationNoticeStatus string
	ExpiryNoticeStatus     string
	AttemptCount           int32
	FailureReason          string
	CreatedAtMS            int64
	UpdatedAtMS            int64
}

// TemporaryLevelAdjustment 是一次后台临时等级调整。wealth/charm 可在同一命令中各出现一次。
type TemporaryLevelAdjustment struct {
	Track        string
	Level        int32
	DurationDays int32
}

// AdjustTemporaryLevelsCommand 以 CommandID 为整批幂等键；RequestHash 用于拒绝同键不同载荷。
type AdjustTemporaryLevelsCommand struct {
	CommandID       string
	RequestHash     string
	UserID          int64
	Adjustments     []TemporaryLevelAdjustment
	OperatorAdminID int64
	Reason          string
}

// AdminTrackProfile 同时返回真实账户和当前展示层，后台无需从临时目标反推真实经验。
type AdminTrackProfile struct {
	Track                string
	RealLevel            int32
	RealTotalValue       int64
	DisplayLevel         int32
	DisplayValue         int64
	TemporaryLevelID     string
	TemporaryTargetLevel int32
	StartedAtMS          int64
	ExpiresAtMS          int64
}

type AdminUserLevelProfile struct {
	UserID       int64
	Tracks       []AdminTrackProfile
	ServerTimeMS int64
}

type AdminLevelProfiles struct {
	Profiles     []AdminUserLevelProfile
	ServerTimeMS int64
}

// TemporaryLevelWork 是 cron 认领后的副作用快照。过期状态、奖励永久化/撤回准备均已在认领事务内提交。
type TemporaryLevelWork struct {
	TemporaryLevel
	SendActivationNotice bool
	SendExpiryNotice     bool
}

// RewardWalletGrant 保存一个奖励任务下的每次钱包发放。资源组、头像框和短徽章必须分别留痕。
type RewardWalletGrant struct {
	RewardJobID   string
	GrantKind     string
	WalletGrantID string
	CreatedAtMS   int64
}

// RewardRevocation 是一条可独立幂等重试的钱包 grant 撤回任务。
type RewardRevocation struct {
	RewardJobID      string
	WalletGrantID    string
	TemporaryLevelID string
	UserID           int64
	Track            string
	AttemptCount     int32
}

// RewardJob 是等级奖励资源组的异步发放任务。
type RewardJob struct {
	AppCode              string
	RewardJobID          string
	UserID               int64
	Track                string
	RewardSourceType     string
	RewardSourceID       string
	ResourceGroupID      int64
	WalletCommandID      string
	WalletGrantID        string
	RewardOrigin         string
	TemporaryLevelID     string
	GrantGeneration      int32
	RewardLevel          int32
	Permanent            bool
	TemporaryExpiresAtMS int64
	RevokeStatus         string
	Status               string
	AttemptCount         int32
	FailureReason        string
	CreatedAtMS          int64
	UpdatedAtMS          int64
}

// RewardQuery 是用户奖励记录分页筛选。
type RewardQuery struct {
	UserID   int64
	Track    string
	Status   string
	Page     int32
	PageSize int32
}

// TrackCommand 是后台创建或更新轨道配置的命令。
type TrackCommand struct {
	Track             string
	Name              string
	Status            string
	SortOrder         int32
	DisplayConfigJSON string
}

// RuleCommand 是后台创建或更新等级规则的命令。
type RuleCommand struct {
	Track                 string
	Level                 int32
	RequiredValue         int64
	Name                  string
	Status                string
	RewardResourceGroupID int64
	SortOrder             int32
	DisplayConfigJSON     string
	OperatorAdminID       int64
}

// TierCommand 是后台创建或更新等级段配置的命令。
type TierCommand struct {
	TierID                       int64
	Track                        string
	MinLevel                     int32
	MaxLevel                     int32
	Name                         string
	DisplayAvatarFrameResourceID int64
	DisplayBadgeResourceID       int64
	RewardResourceGroupID        int64
	Status                       string
	DisplayConfigJSON            string
	OperatorAdminID              int64
}

// BatchConfigCommand 是一次批量编辑的完整写集合；repository 必须在同一个事务中提交或回滚。
type BatchConfigCommand struct {
	Rules           []RuleCommand
	Tiers           []TierCommand
	OperatorAdminID int64
}
