-- 初始化数据库
CREATE DATABASE IF NOT EXISTS memora DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE memora;

-- 标准单词表
CREATE TABLE IF NOT EXISTS words (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    word VARCHAR(100) NOT NULL UNIQUE COMMENT '单词',
    phonetic_uk VARCHAR(255) COMMENT '英式音标',
    phonetic_us VARCHAR(255) COMMENT '美式音标',
    audio_uk VARCHAR(500) COMMENT '英式音频文件路径',
    audio_us VARCHAR(500) COMMENT '美式音频文件路径',
    part_of_speech VARCHAR(50) COMMENT '词性',
    definition TEXT COMMENT '标准释义',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_word (word)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 记忆记录表
CREATE TABLE IF NOT EXISTS memory_records (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    word_id BIGINT NOT NULL COMMENT '关联words表',
    user_id BIGINT DEFAULT 1 COMMENT '用户ID',
    correct_count INT DEFAULT 0 COMMENT '正确次数',
    total_count INT DEFAULT 0 COMMENT '总复习次数',
    mastery_level INT DEFAULT 0 COMMENT '掌握程度 0-5',
    last_reviewed_at TIMESTAMP NULL COMMENT '上次复习时间',
    next_review_at TIMESTAMP NULL COMMENT '下次复习时间',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_word_id (word_id),
    INDEX idx_user_id (user_id),
    INDEX idx_next_review (next_review_at),
    FOREIGN KEY (word_id) REFERENCES words(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
