-- 创建考试相关表

-- 1. 创建题目表
CREATE TABLE IF NOT EXISTS `questions` (
    `id` INT NOT NULL AUTO_INCREMENT,
    `course_id` INT NOT NULL,
    `question_type` VARCHAR(20) NOT NULL COMMENT '题目类型: single_choice, multiple_choice, true_false, fill_blank, essay',
    `title` TEXT NOT NULL COMMENT '题目标题',
    `content` TEXT NULL COMMENT '题目内容',
    `options` JSON NULL COMMENT '选项（JSON格式）',
    `correct_answer` TEXT NOT NULL COMMENT '正确答案',
    `explanation` TEXT NULL COMMENT '答案解释',
    `score` FLOAT DEFAULT 10.0 COMMENT '分值',
    `difficulty` VARCHAR(10) DEFAULT 'medium' COMMENT '难度等级: easy, medium, hard',
    `tags` JSON NULL COMMENT '标签（JSON格式）',
    `usage_count` INT DEFAULT 0 COMMENT '使用次数',
    `correct_count` INT DEFAULT 0 COMMENT '答对次数',
    `is_active` BOOLEAN DEFAULT TRUE COMMENT '是否启用',
    `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `idx_course_id` (`course_id`),
    KEY `idx_question_type` (`question_type`),
    KEY `idx_difficulty` (`difficulty`),
    KEY `idx_is_active` (`is_active`),
    CONSTRAINT `fk_questions_course` FOREIGN KEY (`course_id`) REFERENCES `courses` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 2. 创建考试记录表
CREATE TABLE IF NOT EXISTS `exams` (
    `id` INT NOT NULL AUTO_INCREMENT,
    `user_id` BIGINT NOT NULL,
    `course_id` INT NOT NULL,
    `exam_name` VARCHAR(255) NOT NULL COMMENT '考试名称',
    `question_count` INT DEFAULT 10 COMMENT '题目数量',
    `total_score` FLOAT DEFAULT 100.0 COMMENT '总分',
    `pass_score` FLOAT DEFAULT 60.0 COMMENT '及格分',
    `start_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '开始时间',
    `end_time` DATETIME NULL COMMENT '结束时间',
    `duration_minutes` INT DEFAULT 60 COMMENT '考试时长（分钟）',
    `score` FLOAT NULL COMMENT '得分',
    `is_passed` BOOLEAN NULL COMMENT '是否通过',
    `status` VARCHAR(20) DEFAULT 'started' COMMENT '状态: started, submitted, timeout',
    `questions` JSON NULL COMMENT '题目数据（JSON格式）',
    `answers` JSON NULL COMMENT '答案数据（JSON格式）',
    `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `idx_user_id` (`user_id`),
    KEY `idx_course_id` (`course_id`),
    KEY `idx_status` (`status`),
    CONSTRAINT `fk_exams_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
    CONSTRAINT `fk_exams_course` FOREIGN KEY (`course_id`) REFERENCES `courses` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 3. 创建考试结果详情表
CREATE TABLE IF NOT EXISTS `exam_results` (
    `id` INT NOT NULL AUTO_INCREMENT,
    `exam_id` INT NOT NULL,
    `question_id` INT NOT NULL,
    `user_answer` TEXT NULL COMMENT '用户答案',
    `is_correct` BOOLEAN DEFAULT FALSE COMMENT '是否正确',
    `score` FLOAT DEFAULT 0.0 COMMENT '得分',
    `answer_time` INT NULL COMMENT '答题时长（秒）',
    `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `idx_exam_id` (`exam_id`),
    KEY `idx_question_id` (`question_id`),
    CONSTRAINT `fk_exam_results_exam` FOREIGN KEY (`exam_id`) REFERENCES `exams` (`id`) ON DELETE CASCADE,
    CONSTRAINT `fk_exam_results_question` FOREIGN KEY (`question_id`) REFERENCES `questions` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 4. 插入测试题目数据
INSERT INTO `questions` (`course_id`, `question_type`, `title`, `content`, `options`, `correct_answer`, `explanation`, `score`, `difficulty`, `tags`) VALUES
(1, 'single_choice', 'Python中哪个关键字用于定义函数？', NULL, '{"A": "def", "B": "function", "C": "fun", "D": "define"}', 'A', 'Python使用def关键字来定义函数', 10.0, 'easy', '["python", "基础", "函数"]'),
(1, 'single_choice', 'Python中列表和元组的主要区别是什么？', NULL, '{"A": "列表是有序的，元组是无序的", "B": "列表可变，元组不可变", "C": "列表只能存储数字，元组可以存储任何类型", "D": "没有区别"}', 'B', '列表是可变的（mutable），而元组是不可变的（immutable）', 10.0, 'medium', '["python", "数据结构"]'),
(1, 'single_choice', '以下哪个不是Python的内置数据类型？', NULL, '{"A": "list", "B": "dict", "C": "array", "D": "tuple"}', 'C', 'array不是Python的内置数据类型，需要导入array模块', 10.0, 'medium', '["python", "数据类型"]'),
(1, 'true_false', 'Python是一种编译型语言', NULL, NULL, 'false', 'Python是一种解释型语言，不需要编译成机器码', 10.0, 'easy', '["python", "基础"]'),
(1, 'true_false', 'Python支持多重继承', NULL, NULL, 'true', 'Python支持多重继承，一个类可以继承多个父类', 10.0, 'medium', '["python", "面向对象"]');

-- 5. 插入更多测试题目（如果需要）
INSERT INTO `questions` (`course_id`, `question_type`, `title`, `content`, `options`, `correct_answer`, `explanation`, `score`, `difficulty`, `tags`) VALUES
(1, 'single_choice', 'Python中的装饰器是什么？', NULL, '{"A": "一种设计模式", "B": "用于修改函数或类行为的函数", "C": "一种数据结构", "D": "一种循环结构"}', 'B', '装饰器是一个接受函数作为参数并返回新函数的函数', 15.0, 'hard', '["python", "高级特性", "装饰器"]'),
(1, 'single_choice', '以下哪个方法用于向列表末尾添加元素？', NULL, '{"A": "add()", "B": "insert()", "C": "append()", "D": "extend()"}', 'C', 'append()方法用于向列表末尾添加单个元素', 10.0, 'easy', '["python", "列表", "方法"]'),
(1, 'multiple_choice', 'Python中哪些是可变数据类型？', '请选择所有正确答案', '{"A": "list", "B": "tuple", "C": "dict", "D": "str", "E": "set"}', '["A", "C", "E"]', 'list、dict和set是可变数据类型，而tuple和str是不可变的', 15.0, 'medium', '["python", "数据类型"]'),
(1, 'fill_blank', 'Python中使用____关键字定义类', NULL, NULL, 'class', '使用class关键字定义类', 10.0, 'easy', '["python", "面向对象"]'),
(1, 'essay', '请解释Python中的GIL（全局解释器锁）是什么，以及它对多线程编程的影响', NULL, NULL, 'GIL是Python解释器的一个机制，同一时刻只允许一个线程执行Python字节码。这对CPU密集型的多线程程序性能有负面影响，但对I/O密集型程序影响较小。', 'GIL确保了Python对象的线程安全，但限制了多线程的并行性能', 20.0, 'hard', '["python", "高级", "并发"]');
