package models

import (
	"time"
)

// TrainingContent 训练内容模型
type TrainingContent struct {
	ID          uint      `gorm:"primaryKey" json:"id"`
	Name        string    `gorm:"type:varchar(128)" json:"name"`
	Type        string    `gorm:"type:varchar(32)" json:"type"` // eye_exercise, crystal_ball, acupoint, relax
	Duration    int       `json:"duration"`                     // 时长 (秒)
	VideoURL    string    `gorm:"type:text" json:"video_url"`
	ThumbnailURL string `gorm:"type:text" json:"thumbnail_url"`
	Description string    `gorm:"type:text" json:"description"`
	Difficulty  int       `gorm:"default:1" json:"difficulty"` // 1-5
	Status      int       `gorm:"default:1" json:"status"`     // 1:启用, 0:禁用
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`

	// 关联的训练任务
	TrainingTasks []TrainingTask `gorm:"foreignKey:ContentID" json:"training_tasks"`
}

// TrainingTask 训练任务模型
type TrainingTask struct {
	ID           uint      `gorm:"primaryKey" json:"id"`
	StudentID    uint      `json:"student_id"`
	Student      Student   `gorm:"foreignKey:StudentID" json:"student"`
	ContentID    uint      `json:"content_id"`
	Content      TrainingContent `gorm:"foreignKey:ContentID" json:"content"`
	ScheduledDate time.Time `json:"scheduled_date"`
	ScheduledTime *time.Time `json:"scheduled_time"`
	Status       int       `json:"status"` // 0:待完成, 1:已完成, 2:已跳过
	CompletedAt  *time.Time `json:"completed_at"`
	Score        *int      `json:"score"`         // 动作评分
	PointsEarned int       `json:"points_earned"` // 获得积分
	CreatedAt    time.Time `json:"created_at"`
	UpdatedAt    time.Time `json:"updated_at"`

	// 关联的训练记录
	TrainingRecords []TrainingRecord `gorm:"foreignKey:TaskID" json:"training_records"`
}

// TrainingRecord 训练记录模型
type TrainingRecord struct {
	ID                 uint      `gorm:"primaryKey" json:"id"`
	StudentID          uint      `json:"student_id"`
	Student            Student   `gorm:"foreignKey:StudentID" json:"student"`
	TaskID             uint      `json:"task_id"`
	Task               TrainingTask `gorm:"foreignKey:TaskID" json:"task"`
	ContentID          uint      `json:"content_id"`
	Content            TrainingContent `gorm:"foreignKey:ContentID" json:"content"`
	Score              int       `json:"score"`                    // 动作评分
	Accuracy           float64   `json:"accuracy"`                 // 准确率
	Duration           float64   `json:"duration"`                 // 实际用时
	PerformanceMetrics JSONMap   `gorm:"type:json" json:"performance_metrics"` // 性能指标
	Feedback           string    `gorm:"type:text" json:"feedback"` // 反馈信息
	CreatedAt          time.Time `json:"created_at"`
	UpdatedAt          time.Time `json:"updated_at"`
}

// TrainingPerformance 训练表现统计
type TrainingPerformance struct {
	ID              uint      `gorm:"primaryKey" json:"id"`
	EntityID        uint      `json:"entity_id"` // 学生/班级/学校ID
	EntityType      string    `gorm:"type:varchar(20)" json:"entity_type"` // student, class, school
	CompletionRate  float64   `json:"completion_rate"`                     // 完成率
	AverageScore    float64   `json:"average_score"`                       // 平均分数
	TotalTrainingCount int    `json:"total_training_count"`                // 总训练次数
	EngagementRate  float64   `json:"engagement_rate"`                     // 参与率
	ImprovementRate float64   `json:"improvement_rate"`                    // 改善率
	CreatedAt       time.Time `json:"created_at"`
	UpdatedAt       time.Time `json:"updated_at"`
}

// UserPoints 用户积分模型
type UserPoints struct {
	ID          uint      `gorm:"primaryKey" json:"id"`
	UserID      uint      `json:"user_id"`
	UserType    string    `gorm:"type:varchar(16)" json:"user_type"` // student, parent, teacher
	TotalPoints int       `gorm:"default:0" json:"total_points"`
	UsedPoints  int       `gorm:"default:0" json:"used_points"`
	Level       string    `gorm:"type:varchar(32);default:'bronze'" json:"level"` // bronze, silver, gold, diamond
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`

	// 关联的积分流水
	
}

// PointTransaction 积分流水模型
type PointTransaction struct {
	ID            uint      `gorm:"primaryKey" json:"id"`
	UserID        uint      `json:"user_id"`
	UserType      string    `gorm:"type:varchar(16)" json:"user_type"`
	ChangeType    string    `gorm:"type:varchar(32)" json:"change_type"` // earn, use
	Points        int       `json:"points"`                              // 变化积分数
	BalanceAfter  int       `json:"balance_after"`                       // 变化后的余额
	Source        string    `gorm:"type:varchar(64)" json:"source"`      // 来源: training, detection, activity
	Description   string    `gorm:"type:varchar(256)" json:"description"`
	CreatedAt     time.Time `json:"created_at"`
}

// TrainingRecommendation 训练建议模型
type TrainingRecommendation struct {
	ID          uint      `gorm:"primaryKey" json:"id"`
	StudentID   uint      `json:"student_id"`
	Student     Student   `gorm:"foreignKey:StudentID" json:"student"`
	Recommendation string `gorm:"type:text" json:"recommendation"`
	Priority    int       `json:"priority"` // 优先级: 1-5
	ValidFrom   time.Time `json:"valid_from"`
	ValidUntil  time.Time `json:"valid_until"`
	IsApplied   bool      `json:"is_applied"` // 是否已采纳
	AppliedAt   *time.Time `json:"applied_at"`
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
}

// TrainingType 训练类型枚举
const (
	TrainingTypeEyeExercise = "eye_exercise" // 眼保健操
	TrainingTypeCrystalBall = "crystal_ball" // 晶状体调焦训练
	TrainingTypeAcupoint    = "acupoint"     // 穴位按摩
	TrainingTypeRelax       = "relax"        // 放松训练
)

// TrainingStatus 训练状态枚举
const (
	TrainingStatusPending = 0 // 待完成
	TrainingStatusDone    = 1 // 已完成
	TrainingStatusSkipped = 2 // 已跳过
)

// PointsChangeType 积分变化类型枚举
const (
	PointsChangeTypeEarn = "earn" // 获得
	PointsChangeTypeUse  = "use"  // 使用
)

// PointsSource 积分来源枚举
const (
	PointsSourceTraining   = "training"   // 训练获得
	PointsSourceDetection  = "detection"  // 检测获得
	PointsSourceActivity   = "activity"   // 活动获得
	PointsSourceAttendance = "attendance" // 出勤获得
	PointsSourceAchievement = "achievement" // 成就获得
)

// UserLevel 用户等级枚举
const (
	UserLevelBronze  = "bronze"  // 青铜
	UserLevelSilver  = "silver"  // 白银
	UserLevelGold    = "gold"    // 黄金
	UserLevelDiamond = "diamond" // 钻石
)