package task_center

import (
	"time"

	"gorm.io/datatypes"
	"gorm.io/gorm"
)

type Task struct {
	ID               int64          `gorm:"primaryKey;autoIncrement"`
	Name             string         `gorm:"size:64;not null"`
	Description      string         `gorm:"type:text"`
	Status           int32          `gorm:"not null;index"` // 增加索引，常用于过滤活跃任务
	StartTime        *time.Time     `gorm:"index"`
	EndTime          *time.Time     `gorm:"index"`
	Visibility       int32          `gorm:"not null"`
	ConditionsSchema datatypes.JSON `gorm:"type:json"`
	Tiers            []TaskTier     `gorm:"foreignKey:TaskID"`
	Rewards          []TaskReward   `gorm:"foreignKey:TaskID"`
	CreatedAt        time.Time
	UpdatedAt        time.Time
}

func (Task) TableName() string { return "task_center_tasks" }

type TaskTier struct {
	ID          int64          `gorm:"primaryKey;autoIncrement"`
	TaskID      int64          `gorm:"index;not null"`
	Metric      string         `gorm:"size:32;not null"`
	Operator    string         `gorm:"size:8;not null"`
	Threshold   int64          `gorm:"not null"`
	Window      string         `gorm:"size:32;not null"`
	Repeatable  int32          `gorm:"not null"`
	Priority    int32          `gorm:"not null"`
	ActivityID  int64          `gorm:"not null;default:0;index"` // 关联活动ID，0为全局
	ExtraParams datatypes.JSON `gorm:"type:json"`
	CreatedAt   time.Time
	UpdatedAt   time.Time
}

func (TaskTier) TableName() string { return "task_center_task_tiers" }

type TaskReward struct {
	ID            int64          `gorm:"primaryKey;autoIncrement"`
	TaskID        int64          `gorm:"index;not null"`
	TierID        int64          `gorm:"index;not null"`
	RewardType    string         `gorm:"size:32;not null"`
	RewardPayload datatypes.JSON `gorm:"type:json"`
	Quantity      int64          `gorm:"not null"`
	CreatedAt     time.Time
	UpdatedAt     time.Time
}

func (TaskReward) TableName() string { return "task_center_task_rewards" }

type UserTaskProgress struct {
	ID           int64          `gorm:"primaryKey;autoIncrement"`
	UserID       int64          `gorm:"uniqueIndex:uk_user_task_act;not null"` // 联合唯一索引
	TaskID       int64          `gorm:"uniqueIndex:uk_user_task_act;not null"`
	ActivityID   int64          `gorm:"uniqueIndex:uk_user_task_act;not null;default:0"`
	OrderCount   int64          `gorm:"not null"`
	OrderAmount  int64          `gorm:"not null;default:0"` // 累计消费金额（单位分）
	InviteCount  int64          `gorm:"not null"`
	FirstOrder   int32          `gorm:"not null"`
	ClaimedTiers datatypes.JSON `gorm:"type:json"`
	CreatedAt    time.Time
	UpdatedAt    time.Time
}

func (UserTaskProgress) TableName() string { return "task_center_user_progress" }

type TaskEventLog struct {
	ID             int64          `gorm:"primaryKey;autoIncrement"`
	EventID        string         `gorm:"size:64;index"`
	SourceType     string         `gorm:"size:16"`
	SourceID       int64          `gorm:"index"`
	UserID         int64          `gorm:"index"`
	TaskID         int64          `gorm:"index"`
	TierID         int64          `gorm:"index"`
	IdempotencyKey string         `gorm:"size:128;uniqueIndex"`
	Status         string         `gorm:"size:16"`
	Result         datatypes.JSON `gorm:"type:json"`
	CreatedAt      time.Time
}

func (TaskEventLog) TableName() string { return "task_center_event_logs" }

func AutoMigrate(db *gorm.DB) error {
	return db.AutoMigrate(&Task{}, &TaskTier{}, &TaskReward{}, &UserTaskProgress{}, &TaskEventLog{})
}
