// Package cp 定义 CP/兄弟/姐妹关系的领域事实和值对象。
package cp

const (
	RelationTypeCP      = "cp"
	RelationTypeBrother = "brother"
	RelationTypeSister  = "sister"

	ApplicationStatusPending  = "pending"
	ApplicationStatusAccepted = "accepted"
	ApplicationStatusRejected = "rejected"
	ApplicationStatusExpired  = "expired"
	ApplicationStatusBlocked  = "blocked"

	RelationshipStatusActive = "active"
	RelationshipStatusEnded  = "ended"

	BreakupStatusPending   = "pending"
	BreakupStatusConfirmed = "confirmed"
	BreakupStatusCanceled  = "canceled"

	GiftTypeCodeCP = "cp"

	EventTypeApplicationCreated          = "UserCPApplicationCreated"
	EventTypeApplicationAccepted         = "UserCPApplicationAccepted"
	EventTypeApplicationRejected         = "UserCPApplicationRejected"
	EventTypeRelationshipCreated         = "UserCPRelationshipCreated"
	EventTypeRelationshipCapacityReached = "UserCPRelationshipCapacityReached"
	EventTypeRelationshipIntimacyChanged = "UserCPRelationshipIntimacyChanged"
	EventTypeRelationshipLevelUp         = "UserCPRelationshipLevelUp"
	EventTypeRelationshipEnded           = "UserCPRelationshipEnded"
)

// UserProfile 是 CP 申请和关系展示用的用户快照。
type UserProfile struct {
	UserID        int64
	DisplayUserID string
	Username      string
	Avatar        string
}

// AvatarFrameSnapshot 是用户当前佩戴头像框的展示快照；数据来自 wallet-service 资源佩戴读模型。
type AvatarFrameSnapshot struct {
	ResourceID    int64  `json:"resource_id,omitempty"`
	ResourceCode  string `json:"resource_code,omitempty"`
	Name          string `json:"name,omitempty"`
	AssetURL      string `json:"asset_url,omitempty"`
	PreviewURL    string `json:"preview_url,omitempty"`
	AnimationURL  string `json:"animation_url,omitempty"`
	MetadataJSON  string `json:"metadata_json,omitempty"`
	EntitlementID string `json:"entitlement_id,omitempty"`
}

// IntimacyLeaderboardUser 是 CP 亲密值排行榜中一方用户的展示资料。
type IntimacyLeaderboardUser struct {
	UserID        int64                `json:"user_id"`
	DisplayUserID string               `json:"display_user_id"`
	Username      string               `json:"username"`
	Avatar        string               `json:"avatar"`
	AvatarFrame   *AvatarFrameSnapshot `json:"avatar_frame,omitempty"`
}

// IntimacyLeaderboardEntry 是 Redis zset member 保存的一对 active 关系快照。
type IntimacyLeaderboardEntry struct {
	Rank           int64                   `json:"rank,omitempty"`
	RelationshipID string                  `json:"relationship_id"`
	RelationType   string                  `json:"relation_type"`
	IntimacyValue  int64                   `json:"intimacy_value"`
	Level          int32                   `json:"level"`
	UserA          IntimacyLeaderboardUser `json:"user_a"`
	UserB          IntimacyLeaderboardUser `json:"user_b"`
	FormedAtMS     int64                   `json:"formed_at_ms"`
	UpdatedAtMS    int64                   `json:"updated_at_ms"`
}

// WeeklyRankEntry 是按一个 UTC 周期聚合的 CP 关系新增亲密值；activity-service 用它固化和发奖，不能回退到关系累计 intimacy_value。
type WeeklyRankEntry struct {
	Rank            int64
	RelationshipID  string
	RelationType    string
	Score           int64
	UserA           IntimacyLeaderboardUser
	UserB           IntimacyLeaderboardUser
	FormedAtMS      int64
	UpdatedAtMS     int64
	FirstScoredAtMS int64
	LastScoredAtMS  int64
}

// IntimacyLeaderboardPage 是 CP 亲密值排行榜分页读模型。
type IntimacyLeaderboardPage struct {
	Items        []IntimacyLeaderboardEntry
	Total        int64
	Page         int32
	PageSize     int32
	ServerTimeMS int64
}

// GiftSnapshot 固化触发申请或亲密值增长的礼物展示信息。
type GiftSnapshot struct {
	GiftID           string
	GiftName         string
	GiftIconURL      string
	GiftAnimationURL string
	GiftCount        int32
	GiftValue        int64
	CoinSpent        int64
	BillingReceiptID string
}

// FormationGiftFeedItem 是组 CP 成立礼物的只读展示模型；金额固定使用 wallet 结算出的礼物金币价值，不复用热度字段。
type FormationGiftFeedItem struct {
	FormationID   string
	GiftCoinValue int64
	Requester     UserProfile
	Target        UserProfile
	FormedAtMS    int64
}

// Application 表达一次关系申请，只有 target 用户可以处理 pending 状态。
type Application struct {
	ApplicationID string
	RelationType  string
	Status        string
	Requester     UserProfile
	Target        UserProfile
	RoomID        string
	RoomRegionID  int64
	// RoomLocked 是触发关系申请的送礼命令在 Room Cell 内固化的锁房快照，供跨房关系飘屏安全进入来源房间。
	RoomLocked      bool
	Gift            GiftSnapshot
	CreatedAtMS     int64
	UpdatedAtMS     int64
	ExpiresAtMS     int64
	DecidedAtMS     int64
	SourceEventID   string
	SourceCommandID string
}

// Relationship 表达 A/B 已建立的一种 active 关系；展示时由 service 填充 me/partner。
type Relationship struct {
	RelationshipID        string
	RelationType          string
	Status                string
	UserAID               int64
	UserBID               int64
	Me                    UserProfile
	Partner               UserProfile
	IntimacyValue         int64
	Level                 int32
	CurrentLevelThreshold int64
	NextLevelThreshold    int64
	NeededForNextLevel    int64
	LevelProgressPercent  int32
	MaxLevel              bool
	SourceApplicationID   string
	FormedAtMS            int64
	EndedAtMS             int64
	BreakupCostCoins      int64
	UpdatedAtMS           int64
}

// RelationshipBreakup 是一次解除关系命令的占位事实；pending 阻止同一关系被并发重复扣费。
type RelationshipBreakup struct {
	CommandID           string
	RelationshipID      string
	RequesterUserID     int64
	RelationType        string
	BreakupCostCoins    int64
	Status              string
	WalletTransactionID string
	CoinBalanceAfter    int64
	Reason              string
	CreatedAtMS         int64
	UpdatedAtMS         int64
	ConfirmedAtMS       int64
	CanceledAtMS        int64
}

// GiftEvent 是 room outbox 中 RoomGiftSent 转换后的 CP 关系消费输入。
type GiftEvent struct {
	AppCode          string
	EventID          string
	RoomID           string
	RoomVersion      int64
	OccurredAtMS     int64
	SenderUserID     int64
	TargetUserID     int64
	GiftID           string
	GiftCount        int32
	GiftValue        int64
	CoinSpent        int64
	BillingReceiptID string
	VisibleRegionID  int64
	// RoomLocked 直接来自 RoomGiftSent owner fact；user-service 不异步回查房间当前状态。
	RoomLocked       bool
	CommandID        string
	GiftTypeCode     string
	CPRelationType   string
	GiftName         string
	GiftIconURL      string
	GiftAnimationURL string
}

// ConsumeResult 汇总一次送礼事件对 CP 领域产生的变化。
type ConsumeResult struct {
	Consumed     bool
	Application  Application
	Relationship Relationship
}

// RelationshipCapacityReachedNotice 是关系礼物已扣费、但任一方该类型 active 关系名额已满时的通知事实。
// user-service 只固化判断结果；notice-service 再把它转换为发给送礼人的系统 IM。
type RelationshipCapacityReachedNotice struct {
	RelationType      string `json:"relation_type"`
	SenderUserID      int64  `json:"sender_user_id"`
	TargetUserID      int64  `json:"target_user_id"`
	LimitedUserID     int64  `json:"limited_user_id"`
	CurrentCount      int64  `json:"current_count"`
	MaxCount          int32  `json:"max_count"`
	RoomID            string `json:"room_id"`
	SourceRoomEventID string `json:"source_room_event_id"`
	MessageText       string `json:"message_text"`
}
