package models

import (
	"time"
)

// TestType 测试类型
type TestType string

const (
	TestTypeQuick        TestType = "quick"        // 快速测试
	TestTypeComprehensive TestType = "comprehensive" // 综合测试
	TestTypeDaily        TestType = "daily"        // 每日测试
	TestTypeCustom       TestType = "custom"       // 自定义测试
)

// TestDifficulty 测试难度
type TestDifficulty string

const (
	TestDifficultyBeginner     TestDifficulty = "beginner"     // 初级
	TestDifficultyIntermediate TestDifficulty = "intermediate" // 中级
	TestDifficultyAdvanced     TestDifficulty = "advanced"     // 高级
)

// TestStatus 测试状态
type TestStatus string

const (
	TestStatusPending    TestStatus = "pending"     // 待开始
	TestStatusInProgress TestStatus = "in_progress" // 进行中
	TestStatusPaused     TestStatus = "paused"      // 已暂停
	TestStatusCompleted  TestStatus = "completed"   // 已完成
	TestStatusAbandoned  TestStatus = "abandoned"   // 已放弃
)

// SkillType 技能类型
type SkillType string

const (
	SkillTypeVocabulary SkillType = "vocabulary" // 词汇
	SkillTypeGrammar    SkillType = "grammar"    // 语法
	SkillTypeReading    SkillType = "reading"    // 阅读
	SkillTypeListening  SkillType = "listening"  // 听力
	SkillTypeSpeaking   SkillType = "speaking"   // 口语
	SkillTypeWriting    SkillType = "writing"    // 写作
)

// QuestionType 题目类型
type QuestionType string

const (
	QuestionTypeSingleChoice   QuestionType = "single_choice"   // 单选题
	QuestionTypeMultipleChoice QuestionType = "multiple_choice" // 多选题
	QuestionTypeTrueFalse      QuestionType = "true_false"      // 判断题
	QuestionTypeFillBlank      QuestionType = "fill_blank"      // 填空题
	QuestionTypeShortAnswer    QuestionType = "short_answer"    // 简答题
)

// TestTemplate 测试模板
type TestTemplate struct {
	ID               string         `json:"id" gorm:"primaryKey;type:varchar(36)"`
	Title            string         `json:"title" gorm:"type:varchar(255);not null"`
	Description      string         `json:"description" gorm:"type:text"`
	Type             TestType       `json:"type" gorm:"type:varchar(50);not null"`
	Difficulty       TestDifficulty `json:"difficulty" gorm:"type:varchar(50)"`
	Duration         int            `json:"duration" gorm:"comment:测试时长(秒)"`
	TotalQuestions   int            `json:"total_questions" gorm:"comment:总题目数"`
	PassingScore     int            `json:"passing_score" gorm:"comment:及格分数"`
	MaxScore         int            `json:"max_score" gorm:"comment:最高分数"`
	QuestionConfig   string         `json:"question_config" gorm:"type:json;comment:题目配置"`
	SkillDistribution string        `json:"skill_distribution" gorm:"type:json;comment:技能分布"`
	IsActive         bool           `json:"is_active" gorm:"default:true"`
	CreatedAt        time.Time      `json:"created_at"`
	UpdatedAt        time.Time      `json:"updated_at"`
}

// TableName 指定表名
func (TestTemplate) TableName() string {
	return "test_templates"
}

// TestQuestion 测试题目
type TestQuestion struct {
	ID           string       `json:"id" gorm:"primaryKey;type:varchar(36)"`
	TemplateID   string       `json:"template_id" gorm:"type:varchar(36);index"`
	QuestionType QuestionType `json:"question_type" gorm:"type:varchar(50);not null"`
	SkillType    SkillType    `json:"skill_type" gorm:"type:varchar(50);not null"`
	Difficulty   TestDifficulty `json:"difficulty" gorm:"type:varchar(50)"`
	Content      string       `json:"content" gorm:"type:text;not null;comment:题目内容"`
	Options      string       `json:"options" gorm:"type:json;comment:选项(JSON数组)"`
	CorrectAnswer string      `json:"correct_answer" gorm:"type:text;comment:正确答案"`
	Explanation  string       `json:"explanation" gorm:"type:text;comment:答案解析"`
	Points       int          `json:"points" gorm:"default:1;comment:分值"`
	OrderIndex   int          `json:"order_index" gorm:"comment:题目顺序"`
	CreatedAt    time.Time    `json:"created_at"`
	UpdatedAt    time.Time    `json:"updated_at"`
}

// TableName 指定表名
func (TestQuestion) TableName() string {
	return "test_questions"
}

// TestSession 测试会话
type TestSession struct {
	ID                   string     `json:"id" gorm:"primaryKey;type:varchar(36)"`
	TemplateID           string     `json:"template_id" gorm:"type:varchar(36);not null;index"`
	UserID               string     `json:"user_id" gorm:"type:varchar(36);not null;index"`
	Status               TestStatus `json:"status" gorm:"type:varchar(50);not null;default:'pending'"`
	StartTime            *time.Time `json:"start_time"`
	EndTime              *time.Time `json:"end_time"`
	PausedAt             *time.Time `json:"paused_at"`
	TimeRemaining        int        `json:"time_remaining" gorm:"comment:剩余时间(秒)"`
	CurrentQuestionIndex int        `json:"current_question_index" gorm:"default:0"`
	CreatedAt            time.Time  `json:"created_at"`
	UpdatedAt            time.Time  `json:"updated_at"`
	
	// 关联
	Template  *TestTemplate  `json:"template,omitempty" gorm:"foreignKey:TemplateID"`
	Questions []TestQuestion `json:"questions,omitempty" gorm:"many2many:test_session_questions"`
	Answers   []TestAnswer   `json:"answers,omitempty" gorm:"foreignKey:SessionID"`
}

// TableName 指定表名
func (TestSession) TableName() string {
	return "test_sessions"
}

// TestAnswer 测试答案
type TestAnswer struct {
	ID         string    `json:"id" gorm:"primaryKey;type:varchar(36)"`
	SessionID  string    `json:"session_id" gorm:"type:varchar(36);not null;index"`
	QuestionID string    `json:"question_id" gorm:"type:varchar(36);not null;index"`
	Answer     string    `json:"answer" gorm:"type:text;comment:用户答案"`
	IsCorrect  *bool     `json:"is_correct" gorm:"comment:是否正确"`
	Score      int       `json:"score" gorm:"default:0;comment:得分"`
	TimeSpent  int       `json:"time_spent" gorm:"comment:答题用时(秒)"`
	CreatedAt  time.Time `json:"created_at"`
	UpdatedAt  time.Time `json:"updated_at"`
	
	// 关联
	Question *TestQuestion `json:"question,omitempty" gorm:"foreignKey:QuestionID"`
}

// TableName 指定表名
func (TestAnswer) TableName() string {
	return "test_answers"
}

// TestResult 测试结果
type TestResult struct {
	ID              string    `json:"id" gorm:"primaryKey;type:varchar(36)"`
	SessionID       string    `json:"session_id" gorm:"type:varchar(36);not null;unique;index"`
	UserID          string    `json:"user_id" gorm:"type:varchar(36);not null;index"`
	TemplateID      string    `json:"template_id" gorm:"type:varchar(36);not null;index"`
	TotalScore      int       `json:"total_score" gorm:"comment:总得分"`
	MaxScore        int       `json:"max_score" gorm:"comment:最高分"`
	Percentage      float64   `json:"percentage" gorm:"type:decimal(5,2);comment:得分百分比"`
	CorrectCount    int       `json:"correct_count" gorm:"comment:正确题数"`
	WrongCount      int       `json:"wrong_count" gorm:"comment:错误题数"`
	SkippedCount    int       `json:"skipped_count" gorm:"comment:跳过题数"`
	TimeSpent       int       `json:"time_spent" gorm:"comment:总用时(秒)"`
	SkillScores     string    `json:"skill_scores" gorm:"type:json;comment:各技能得分"`
	Passed          bool      `json:"passed" gorm:"comment:是否通过"`
	CompletedAt     time.Time `json:"completed_at"`
	CreatedAt       time.Time `json:"created_at"`
	UpdatedAt       time.Time `json:"updated_at"`
	
	// 关联
	Session  *TestSession  `json:"session,omitempty" gorm:"foreignKey:SessionID"`
	Template *TestTemplate `json:"template,omitempty" gorm:"foreignKey:TemplateID"`
}

// TableName 指定表名
func (TestResult) TableName() string {
	return "test_results"
}

// TestSessionQuestion 测试会话题目关联表
type TestSessionQuestion struct {
	SessionID  string `gorm:"primaryKey;type:varchar(36)"`
	QuestionID string `gorm:"primaryKey;type:varchar(36)"`
	OrderIndex int    `gorm:"comment:题目顺序"`
}

// TableName 指定表名
func (TestSessionQuestion) TableName() string {
	return "test_session_questions"
}
