package model

// GiftCardTemplate represents a template for generating gift cards with specific rewards and conditions.
type GiftCardTemplate struct {
	ID              int    `gorm:"primaryKey;autoIncrement" json:"id"`
	Name            string `gorm:"size:255;not null" json:"name"`
	Description     string `gorm:"type:text" json:"description"`
	Type            int    `gorm:"not null;default:1" json:"type"` // 1: General, 2: Plan, 3: Mystery
	Status          bool   `gorm:"not null;default:true" json:"status"`
	Conditions      string `gorm:"type:text" json:"conditions"`      // JSON: New user only, allowed plans, etc.
	Rewards         string `gorm:"type:text" json:"rewards"`         // JSON: Balance, data, time, etc.
	Limits          string `gorm:"type:text" json:"limits"`          // JSON: Max usage per user, cooldown, etc.
	SpecialConfig   string `gorm:"type:text" json:"special_config"`   // JSON: Festival bonuses, etc.
	Icon            string `gorm:"size:255" json:"icon"`
	BackgroundImage string `gorm:"size:255" json:"background_image"`
	ThemeColor      string `gorm:"size:50;default:'#6366f1'" json:"theme_color"`
	Sort            int    `gorm:"not null;default:0" json:"sort"`
	AdminID         int    `gorm:"not null" json:"admin_id"`
	CreatedAt       int64  `gorm:"autoCreateTime" json:"created_at"`
	UpdatedAt       int64  `gorm:"autoUpdateTime" json:"updated_at"`
}

func (GiftCardTemplate) TableName() string {
	return "v2_gift_card_template"
}

// GiftCardCode represents an individual exchangeable code generated from a template.
type GiftCardCode struct {
	ID            int    `gorm:"primaryKey;autoIncrement" json:"id"`
	TemplateID    int    `gorm:"not null;index" json:"template_id"`
	Code          string `gorm:"size:64;uniqueIndex;not null" json:"code"`
	BatchID       string `gorm:"size:64;index" json:"batch_id"`
	Status        int    `gorm:"not null;default:0" json:"status"` // 0: Unused, 1: Used, 2: Expired, 3: Disabled
	UserID        *int   `gorm:"index" json:"user_id"`             // The user who used it
	UsedAt        *int64 `json:"used_at"`
	ExpiresAt     *int64 `gorm:"index" json:"expires_at"`
	ActualRewards string `gorm:"type:text" json:"actual_rewards"` // JSON: Rewards actually given (can differ from template)
	UsageCount    int    `gorm:"not null;default:0" json:"usage_count"`
	MaxUsage      int    `gorm:"not null;default:1" json:"max_usage"`
	Metadata      string `gorm:"type:text" json:"metadata"` // JSON: Extra data
	CreatedAt     int64  `gorm:"autoCreateTime" json:"created_at"`
	UpdatedAt     int64  `gorm:"autoUpdateTime" json:"updated_at"`

	Template *GiftCardTemplate `gorm:"foreignKey:TemplateID" json:"template"`
}

func (GiftCardCode) TableName() string {
	return "v2_gift_card_code"
}

// GiftCardUsage tracks every instance of a gift card being redeemed.
type GiftCardUsage struct {
	ID                int     `gorm:"primaryKey;autoIncrement" json:"id"`
	CodeID            int     `gorm:"not null;index" json:"code_id"`
	TemplateID        int     `gorm:"not null;index" json:"template_id"`
	UserID            int     `gorm:"not null;index" json:"user_id"`
	InviteUserID      *int    `gorm:"index" json:"invite_user_id"`
	RewardsGiven      string  `gorm:"type:text" json:"rewards_given"` // JSON
	InviteRewards     string  `gorm:"type:text" json:"invite_rewards"` // JSON
	UserLevelAtUse    *int    `json:"user_level_at_use"`
	PlanIDAtUse       *int    `json:"plan_id_at_use"`
	MultiplierApplied float64 `gorm:"not null;default:1.0" json:"multiplier_applied"`
	IPAddress         string  `gorm:"size:45" json:"ip_address"`
	UserAgent         string  `gorm:"type:text" json:"user_agent"`
	Notes             string  `gorm:"type:text" json:"notes"`
	CreatedAt         int64   `gorm:"autoCreateTime" json:"created_at"`
}

func (GiftCardUsage) TableName() string {
	return "v2_gift_card_usage"
}
