package models

import (
	"time"

	"gorm.io/gorm"
)

// VocabularyCategory 词汇分类模型
type VocabularyCategory struct {
	ID          string    `json:"id" gorm:"type:varchar(36);primaryKey;comment:分类ID"`
	Name        string    `json:"name" gorm:"type:varchar(100);not null;comment:分类名称"`
	Description *string   `json:"description" gorm:"type:text;comment:分类描述"`
	Level       string    `json:"level" gorm:"type:enum('beginner','intermediate','advanced');not null;comment:难度级别"`
	Icon        *string   `json:"icon" gorm:"type:varchar(255);comment:图标URL"`
	Color       *string   `json:"color" gorm:"type:varchar(7);comment:主题色"`
	SortOrder   int       `json:"sort_order" gorm:"type:int;default:0;comment:排序"`
	IsActive    bool      `json:"is_active" gorm:"type:boolean;default:true;comment:是否启用"`
	CreatedAt   time.Time `json:"created_at" gorm:"type:timestamp;default:CURRENT_TIMESTAMP;comment:创建时间"`
	UpdatedAt   time.Time `json:"updated_at" gorm:"type:timestamp;default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;comment:更新时间"`
	
	// 关联关系
    Vocabularies []Vocabulary `json:"vocabularies,omitempty" gorm:"many2many:ai_vocabulary_category_relations;foreignKey:ID;joinForeignKey:CategoryID;References:ID;joinReferences:VocabularyID;"`
}

// Vocabulary 词汇模型
type Vocabulary struct {
	ID             int64                 `json:"id" gorm:"column:id;primaryKey;autoIncrement;comment:词汇ID"`
	Word           string                `json:"word" gorm:"column:word;type:varchar(100);uniqueIndex;not null;comment:单词"`
	Phonetic       *string               `json:"phonetic" gorm:"column:phonetic;type:varchar(200);comment:音标"`
	AudioURL       *string               `json:"audio_url" gorm:"column:audio_url;type:varchar(500);comment:音频URL"`
	Level          string                `json:"level" gorm:"column:level;type:enum('beginner','intermediate','advanced');not null;comment:难度级别"`
	Frequency      int                   `json:"frequency" gorm:"column:frequency;type:int;default:0;comment:使用频率"`
	IsActive       bool                  `json:"is_active" gorm:"column:is_active;type:boolean;default:true;comment:是否启用"`
	CreatedAt      time.Time             `json:"created_at" gorm:"column:created_at;type:timestamp;default:CURRENT_TIMESTAMP;comment:创建时间"`
	UpdatedAt      time.Time             `json:"updated_at" gorm:"column:updated_at;type:timestamp;default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;comment:更新时间"`
	DeletedAt      gorm.DeletedAt        `json:"-" gorm:"column:deleted_at;index;comment:删除时间"`
	
	// 关联关系
	Definitions    []VocabularyDefinition `json:"definitions,omitempty" gorm:"foreignKey:VocabularyID"`
	Examples       []VocabularyExample    `json:"examples,omitempty" gorm:"foreignKey:VocabularyID"`
	Images         []VocabularyImage      `json:"images,omitempty" gorm:"foreignKey:VocabularyID"`
    Categories     []VocabularyCategory   `json:"categories,omitempty" gorm:"many2many:ai_vocabulary_category_relations;foreignKey:ID;joinForeignKey:VocabularyID;References:ID;joinReferences:CategoryID;"`
	UserProgress   []UserVocabularyProgress `json:"user_progress,omitempty" gorm:"foreignKey:VocabularyID"` 
}

// VocabularyDefinition 词汇定义模型
type VocabularyDefinition struct {
	ID           int64     `json:"id" gorm:"column:id;primaryKey;autoIncrement;comment:定义ID"`
	VocabularyID int64     `json:"vocabulary_id" gorm:"column:vocabulary_id;not null;index;comment:词汇ID"`
	PartOfSpeech string    `json:"part_of_speech" gorm:"column:part_of_speech;type:varchar(20);not null;comment:词性"`
	Definition   string    `json:"definition" gorm:"column:definition_en;type:text;not null;comment:英文定义"`
	Translation  string    `json:"translation" gorm:"column:definition_cn;type:text;not null;comment:中文翻译"`
	SortOrder    int       `json:"sort_order" gorm:"column:sort_order;type:int;default:0;comment:排序"`
	CreatedAt    time.Time `json:"created_at" gorm:"column:created_at;type:timestamp;default:CURRENT_TIMESTAMP;comment:创建时间"`
	
	// 关联关系
	Vocabulary Vocabulary `json:"-" gorm:"foreignKey:VocabularyID"`
}

// VocabularyExample 词汇例句模型
type VocabularyExample struct {
	ID           int64     `json:"id" gorm:"column:id;primaryKey;autoIncrement;comment:例句ID"`
	VocabularyID int64     `json:"vocabulary_id" gorm:"column:vocabulary_id;not null;index;comment:词汇ID"`
	Example      string    `json:"example" gorm:"column:sentence_en;type:text;not null;comment:英文例句"`
	Translation  string    `json:"translation" gorm:"column:sentence_cn;type:text;not null;comment:中文翻译"`
	AudioURL     *string   `json:"audio_url" gorm:"column:audio_url;type:varchar(500);comment:音频URL"`
	SortOrder    int       `json:"sort_order" gorm:"column:sort_order;type:int;default:0;comment:排序"`
	CreatedAt    time.Time `json:"created_at" gorm:"column:created_at;type:timestamp;default:CURRENT_TIMESTAMP;comment:创建时间"`
	
	// 关联关系
	Vocabulary Vocabulary `json:"-" gorm:"foreignKey:VocabularyID"`
}

// VocabularyImage 词汇图片模型
type VocabularyImage struct {
	ID           string    `json:"id" gorm:"type:varchar(36);primaryKey;comment:图片ID"`
	VocabularyID string    `json:"vocabulary_id" gorm:"type:varchar(36);not null;index;comment:词汇ID"`
	ImageURL     string    `json:"image_url" gorm:"type:varchar(500);not null;comment:图片URL"`
	AltText      *string   `json:"alt_text" gorm:"type:varchar(255);comment:替代文本"`
	Caption      *string   `json:"caption" gorm:"type:text;comment:图片说明"`
	SortOrder    int       `json:"sort_order" gorm:"type:int;default:0;comment:排序"`
	CreatedAt    time.Time `json:"created_at" gorm:"type:timestamp;default:CURRENT_TIMESTAMP;comment:创建时间"`
	UpdatedAt    time.Time `json:"updated_at" gorm:"type:timestamp;default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;comment:更新时间"`
	
	// 关联关系
	Vocabulary Vocabulary `json:"-" gorm:"foreignKey:VocabularyID"`
}

// VocabularyCategoryRelation 词汇分类关系模型
type VocabularyCategoryRelation struct {
	VocabularyID string    `json:"vocabulary_id" gorm:"type:varchar(36);primaryKey;comment:词汇ID"`
	CategoryID   string    `json:"category_id" gorm:"type:varchar(36);primaryKey;comment:分类ID"`
	CreatedAt    time.Time `json:"created_at" gorm:"type:timestamp;default:CURRENT_TIMESTAMP;comment:创建时间"`
}

// UserVocabularyProgress 用户词汇学习进度模型
type UserVocabularyProgress struct {
	ID                int64      `json:"id" gorm:"type:bigint;primaryKey;autoIncrement;comment:进度ID"`
	UserID            int64      `json:"user_id" gorm:"type:bigint;not null;index;comment:用户ID"`
	VocabularyID      string     `json:"vocabulary_id" gorm:"type:varchar(36);not null;index;comment:词汇ID"`
	MasteryLevel      int        `json:"mastery_level" gorm:"type:int;default:0;comment:掌握程度(0-100)"`
	StudyCount        int        `json:"study_count" gorm:"type:int;default:0;comment:学习次数"`
	CorrectCount      int        `json:"correct_count" gorm:"type:int;default:0;comment:正确次数"`
	IncorrectCount    int        `json:"incorrect_count" gorm:"type:int;default:0;comment:错误次数"`
	LastStudiedAt     *time.Time `json:"last_studied_at" gorm:"type:timestamp;comment:最后学习时间"`
	NextReviewAt      *time.Time `json:"next_review_at" gorm:"type:timestamp;comment:下次复习时间"`
	IsMarkedDifficult bool       `json:"is_marked_difficult" gorm:"type:boolean;default:false;comment:是否标记为困难"`
	IsFavorite        bool       `json:"is_favorite" gorm:"type:boolean;default:false;comment:是否收藏"`
	Notes             *string    `json:"notes" gorm:"type:text;comment:学习笔记"`
	CreatedAt         time.Time  `json:"created_at" gorm:"type:timestamp;default:CURRENT_TIMESTAMP;comment:创建时间"`
	UpdatedAt         time.Time  `json:"updated_at" gorm:"type:timestamp;default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;comment:更新时间"`
	
	// 关联关系
	User       User       `json:"-" gorm:"foreignKey:UserID"`
	Vocabulary Vocabulary `json:"-" gorm:"foreignKey:VocabularyID"`
}

// UserWordProgress 用户单词学习进度模型
type UserWordProgress struct {
	ID             int64      `json:"id" gorm:"type:bigint;primaryKey;autoIncrement;comment:进度ID"`
	UserID         int64      `json:"user_id" gorm:"type:bigint;not null;index:idx_user_vocab;comment:用户ID"`
	VocabularyID   int64      `json:"vocabulary_id" gorm:"type:bigint;not null;index:idx_user_vocab;comment:单词ID"`
	Status         string     `json:"status" gorm:"type:varchar(20);default:'not_started';comment:学习状态"`
	StudyCount     int        `json:"study_count" gorm:"type:int;default:0;comment:学习次数"`
	CorrectCount   int        `json:"correct_count" gorm:"type:int;default:0;comment:正确次数"`
	WrongCount     int        `json:"wrong_count" gorm:"type:int;default:0;comment:错误次数"`
	Proficiency    int        `json:"proficiency" gorm:"type:int;default:0;comment:熟练度(0-100)"`
	IsFavorite     bool       `json:"is_favorite" gorm:"type:boolean;default:false;comment:是否收藏"`
	NextReviewAt   *time.Time `json:"next_review_at" gorm:"type:timestamp;comment:下次复习时间"`
	ReviewInterval int        `json:"review_interval" gorm:"type:int;default:1;comment:复习间隔(天)"`
	FirstStudiedAt time.Time  `json:"first_studied_at" gorm:"type:timestamp;comment:首次学习时间"`
	LastStudiedAt  time.Time  `json:"last_studied_at" gorm:"type:timestamp;comment:最后学习时间"`
	MasteredAt     *time.Time `json:"mastered_at" gorm:"type:timestamp;comment:掌握时间"`
	CreatedAt      time.Time  `json:"created_at" gorm:"type:timestamp;default:CURRENT_TIMESTAMP;comment:创建时间"`
	UpdatedAt      time.Time  `json:"updated_at" gorm:"type:timestamp;default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;comment:更新时间"`
	
	// 关联关系
	User       User       `json:"-" gorm:"foreignKey:UserID"`
	Vocabulary Vocabulary `json:"-" gorm:"foreignKey:VocabularyID"`
}

// VocabularyTest 词汇测试模型
type VocabularyTest struct {
	ID          int64     `json:"id" gorm:"type:bigint;primaryKey;autoIncrement;comment:测试ID"`
	UserID      int64     `json:"user_id" gorm:"type:bigint;not null;index;comment:用户ID"`
	TestType    string    `json:"test_type" gorm:"type:enum('placement','progress','review');not null;comment:测试类型"`
	Level       string    `json:"level" gorm:"type:enum('beginner','intermediate','advanced');comment:测试级别"`
	TotalWords  int       `json:"total_words" gorm:"type:int;not null;comment:总词汇数"`
	CorrectWords int      `json:"correct_words" gorm:"type:int;default:0;comment:正确词汇数"`
	Score       float64   `json:"score" gorm:"type:decimal(5,2);comment:得分"`
	Duration    int       `json:"duration" gorm:"type:int;comment:测试时长(秒)"`
	StartedAt   time.Time `json:"started_at" gorm:"type:timestamp;default:CURRENT_TIMESTAMP;comment:开始时间"`
	CompletedAt *time.Time `json:"completed_at" gorm:"type:timestamp;comment:完成时间"`
	CreatedAt   time.Time `json:"created_at" gorm:"type:timestamp;default:CURRENT_TIMESTAMP;comment:创建时间"`
	UpdatedAt   time.Time `json:"updated_at" gorm:"type:timestamp;default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;comment:更新时间"`
	
	// 关联关系
	User User `json:"-" gorm:"foreignKey:UserID"`
}

// TableName 指定表名
func (VocabularyCategory) TableName() string {
    return "ai_vocabulary_categories"
}

func (Vocabulary) TableName() string {
    return "ai_vocabulary"
}

func (VocabularyDefinition) TableName() string {
    return "ai_vocabulary_definitions"
}

func (VocabularyExample) TableName() string {
    return "ai_vocabulary_examples"
}

func (VocabularyImage) TableName() string {
    return "ai_vocabulary_images"
}

func (VocabularyCategoryRelation) TableName() string {
    return "ai_vocabulary_category_relations"
}

func (UserVocabularyProgress) TableName() string {
    return "ai_user_vocabulary_progress"
}

func (VocabularyTest) TableName() string {
    return "ai_vocabulary_tests"
}

// VocabularyBook 词汇书模型
type VocabularyBook struct {
	ID          string    `json:"id" gorm:"type:varchar(36);primaryKey;comment:词汇书ID"`
	Name        string    `json:"name" gorm:"type:varchar(200);not null;comment:词汇书名称"`
	Description *string   `json:"description" gorm:"type:text;comment:词汇书描述"`
	Category    string    `json:"category" gorm:"type:varchar(100);not null;comment:分类"`
	Level       string    `json:"level" gorm:"type:enum('beginner','elementary','intermediate','advanced','expert');not null;comment:难度级别"`
	TotalWords  int       `json:"total_words" gorm:"type:int;default:0;comment:总单词数"`
	CoverImage  *string   `json:"cover_image" gorm:"type:varchar(500);comment:封面图片URL"`
	Icon        *string   `json:"icon" gorm:"type:varchar(255);comment:图标"`
	Color       *string   `json:"color" gorm:"type:varchar(7);comment:主题色"`
	IsSystem    bool      `json:"is_system" gorm:"type:boolean;default:true;comment:是否系统词汇书"`
	IsActive    bool      `json:"is_active" gorm:"type:boolean;default:true;comment:是否启用"`
	SortOrder   int       `json:"sort_order" gorm:"type:int;default:0;comment:排序"`
	CreatedAt   time.Time `json:"created_at" gorm:"type:timestamp;default:CURRENT_TIMESTAMP;comment:创建时间"`
	UpdatedAt   time.Time `json:"updated_at" gorm:"type:timestamp;default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;comment:更新时间"`
	
	// 关联关系（不使用外键约束）
	Words []VocabularyBookWord `json:"words,omitempty" gorm:"-"`
}

// VocabularyBookWord 词汇书单词关联模型
type VocabularyBookWord struct {
	ID           int64      `json:"id" gorm:"type:bigint;primaryKey;autoIncrement;comment:关联ID"`
	BookID       string     `json:"book_id" gorm:"type:varchar(36);not null;index;comment:词汇书ID"`
	VocabularyID string     `json:"vocabulary_id" gorm:"type:varchar(36);not null;index;comment:词汇ID"`
	SortOrder    int        `json:"sort_order" gorm:"type:int;default:0;comment:排序"`
	CreatedAt    time.Time  `json:"created_at" gorm:"type:timestamp;default:CURRENT_TIMESTAMP;comment:创建时间"`
	
	// 关联关系（不使用外键约束）
	Book       VocabularyBook `json:"-" gorm:"-"`
	Vocabulary *Vocabulary    `json:"word,omitempty" gorm:"-"`
}

func (VocabularyBook) TableName() string {
    return "ai_vocabulary_books"
}

func (VocabularyBookWord) TableName() string {
    return "ai_vocabulary_book_words"
}

func (UserWordProgress) TableName() string {
    return "ai_user_word_progress"
}

// LearningSession 学习会话模型
type LearningSession struct {
	ID            int64      `json:"id" gorm:"type:bigint;primaryKey;autoIncrement;comment:会话ID"`
	UserID        int64      `json:"user_id" gorm:"type:bigint;not null;index;comment:用户ID"`
	BookID        string     `json:"book_id" gorm:"type:varchar(36);not null;index;comment:词汇书ID"`
	DailyGoal     int        `json:"daily_goal" gorm:"type:int;default:20;comment:每日学习目标"`
	NewWordsCount int        `json:"new_words_count" gorm:"type:int;default:0;comment:新学单词数"`
	ReviewCount   int        `json:"review_count" gorm:"type:int;default:0;comment:复习单词数"`
	MasteredCount int        `json:"mastered_count" gorm:"type:int;default:0;comment:掌握单词数"`
	StartedAt     time.Time  `json:"started_at" gorm:"type:timestamp;comment:开始时间"`
	CompletedAt   *time.Time `json:"completed_at" gorm:"type:timestamp;comment:完成时间"`
	CreatedAt     time.Time  `json:"created_at" gorm:"type:timestamp;default:CURRENT_TIMESTAMP;comment:创建时间"`
	UpdatedAt     time.Time  `json:"updated_at" gorm:"type:timestamp;default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;comment:更新时间"`
}

func (LearningSession) TableName() string {
    return "ai_learning_sessions"
}

// UserVocabularyBookProgress 用户词汇书学习进度
type UserVocabularyBookProgress struct {
	ID                     int64      `gorm:"primaryKey;autoIncrement" json:"id"`
	UserID                 int64      `gorm:"not null;index:idx_user_book" json:"user_id"`
	BookID                 string     `gorm:"type:varchar(36);not null;index:idx_user_book" json:"book_id"`
	LearnedWords           int        `gorm:"default:0" json:"learned_words"`
	MasteredWords          int        `gorm:"default:0" json:"mastered_words"`
	ProgressPercentage     float64    `gorm:"type:decimal(5,2);default:0.00" json:"progress_percentage"`
	StreakDays             int        `gorm:"default:0" json:"streak_days"`
	TotalStudyDays         int        `gorm:"default:0" json:"total_study_days"`
	AverageDailyWords      float64    `gorm:"type:decimal(5,2);default:0.00" json:"average_daily_words"`
	EstimatedCompletionDate *time.Time `json:"estimated_completion_date"`
	IsCompleted            bool       `gorm:"default:false" json:"is_completed"`
	CompletedAt            *time.Time `json:"completed_at"`
	StartedAt              time.Time  `gorm:"not null" json:"started_at"`
	LastStudiedAt          time.Time  `json:"last_studied_at"`
	CreatedAt              time.Time  `gorm:"autoCreateTime" json:"created_at"`
	UpdatedAt              time.Time  `gorm:"autoUpdateTime" json:"updated_at"`
}

func (UserVocabularyBookProgress) TableName() string {
    return "user_vocabulary_book_progress"
}