-- ===========================================
-- 班级操行分管理系统 - v1.1 → v1.2 升级脚本
-- 字符集: utf8mb4
--
-- 变更内容:
--   1. 创建 assignments（作业表）
--   2. 创建 homework_submissions（作业提交记录表）
--
-- 兼容性: 使用 IF NOT EXISTS 实现幂等，phpMyAdmin 可直接执行
-- ===========================================

-- 升级步骤 1: 创建作业表
CREATE TABLE IF NOT EXISTS `assignments` (
    `assignment_id` INT PRIMARY KEY AUTO_INCREMENT,
    `subject_id` INT NOT NULL,
    `title` VARCHAR(100) NOT NULL,
    `description` TEXT,
    `deadline` DATE NOT NULL,
    `created_by` INT NOT NULL,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`subject_id`) REFERENCES `subjects`(`subject_id`),
    FOREIGN KEY (`created_by`) REFERENCES `users`(`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 升级步骤 2: 创建作业提交记录表
CREATE TABLE IF NOT EXISTS `homework_submissions` (
    `submission_id` INT PRIMARY KEY AUTO_INCREMENT,
    `assignment_id` INT NOT NULL,
    `student_id` INT NOT NULL,
    `status` ENUM('submitted', 'not_submitted', 'late') DEFAULT 'not_submitted',
    `submit_time` DATETIME DEFAULT NULL,
    `comments` TEXT,
    `deduction_applied` TINYINT DEFAULT 0,
    `deduction_record_id` BIGINT DEFAULT NULL,
    `updated_by` INT DEFAULT NULL,
    `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (`assignment_id`) REFERENCES `assignments`(`assignment_id`) ON DELETE CASCADE,
    FOREIGN KEY (`student_id`) REFERENCES `students`(`student_id`) ON DELETE CASCADE,
    FOREIGN KEY (`deduction_record_id`) REFERENCES `conduct_records`(`record_id`) ON DELETE SET NULL,
    UNIQUE KEY `uk_assignment_student` (`assignment_id`, `student_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
