-- ===========================================
-- 班级操行分管理系统 - v1.7 → v1.8 升级脚本
-- 字符集: utf8mb4
--
-- 变更内容:
--   1. conduct_records 表添加 related_type 列
--   2. conduct_records 表添加 semester_id 列 + 外键
--   3. attendance_records 表添加 semester_id 列 + 外键
--
-- 兼容性: 使用存储过程实现幂等，phpMyAdmin 可直接执行
-- ===========================================

-- ===========================================
-- 升级步骤 1: conduct_records 添加 related_type 列
-- ===========================================

DROP PROCEDURE IF EXISTS `upgrade_step`;
DELIMITER $$
CREATE PROCEDURE `upgrade_step`()
BEGIN
    IF NOT EXISTS (
        SELECT * FROM INFORMATION_SCHEMA.COLUMNS 
        WHERE TABLE_SCHEMA = DATABASE() 
          AND TABLE_NAME = 'conduct_records' 
          AND COLUMN_NAME = 'related_type'
    ) THEN
        ALTER TABLE `conduct_records` 
        ADD COLUMN `related_type` ENUM('manual', 'homework', 'attendance') DEFAULT 'manual' 
        AFTER `recorder_name`;
    END IF;
END$$
DELIMITER ;

CALL `upgrade_step`();
DROP PROCEDURE IF EXISTS `upgrade_step`;

-- ===========================================
-- 升级步骤 2: conduct_records 添加 semester_id 列
-- ===========================================

DROP PROCEDURE IF EXISTS `upgrade_step`;
DELIMITER $$
CREATE PROCEDURE `upgrade_step`()
BEGIN
    IF NOT EXISTS (
        SELECT * FROM INFORMATION_SCHEMA.COLUMNS 
        WHERE TABLE_SCHEMA = DATABASE() 
          AND TABLE_NAME = 'conduct_records' 
          AND COLUMN_NAME = 'semester_id'
    ) THEN
        ALTER TABLE `conduct_records` 
        ADD COLUMN `semester_id` INT DEFAULT NULL COMMENT '所属学期ID' 
        AFTER `revoked_at`;
    END IF;
END$$
DELIMITER ;

CALL `upgrade_step`();
DROP PROCEDURE IF EXISTS `upgrade_step`;

-- ===========================================
-- 升级步骤 3: conduct_records 添加 semester_id 外键
-- ===========================================

DROP PROCEDURE IF EXISTS `upgrade_step`;
DELIMITER $$
CREATE PROCEDURE `upgrade_step`()
BEGIN
    IF NOT EXISTS (
        SELECT * FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE 
        WHERE TABLE_SCHEMA = DATABASE() 
          AND TABLE_NAME = 'conduct_records' 
          AND COLUMN_NAME = 'semester_id' 
          AND REFERENCED_TABLE_NAME IS NOT NULL
    ) THEN
        ALTER TABLE `conduct_records` 
        ADD FOREIGN KEY (`semester_id`) REFERENCES `semesters`(`semester_id`) ON DELETE SET NULL;
    END IF;
END$$
DELIMITER ;

CALL `upgrade_step`();
DROP PROCEDURE IF EXISTS `upgrade_step`;

-- ===========================================
-- 升级步骤 4: attendance_records 添加 semester_id 列
-- ===========================================

DROP PROCEDURE IF EXISTS `upgrade_step`;
DELIMITER $$
CREATE PROCEDURE `upgrade_step`()
BEGIN
    IF NOT EXISTS (
        SELECT * FROM INFORMATION_SCHEMA.COLUMNS 
        WHERE TABLE_SCHEMA = DATABASE() 
          AND TABLE_NAME = 'attendance_records' 
          AND COLUMN_NAME = 'semester_id'
    ) THEN
        ALTER TABLE `attendance_records` 
        ADD COLUMN `semester_id` INT DEFAULT NULL COMMENT '所属学期ID' 
        AFTER `deduction_record_id`;
    END IF;
END$$
DELIMITER ;

CALL `upgrade_step`();
DROP PROCEDURE IF EXISTS `upgrade_step`;

-- ===========================================
-- 升级步骤 5: attendance_records 添加 semester_id 外键
-- ===========================================

DROP PROCEDURE IF EXISTS `upgrade_step`;
DELIMITER $$
CREATE PROCEDURE `upgrade_step`()
BEGIN
    IF NOT EXISTS (
        SELECT * FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE 
        WHERE TABLE_SCHEMA = DATABASE() 
          AND TABLE_NAME = 'attendance_records' 
          AND COLUMN_NAME = 'semester_id' 
          AND REFERENCED_TABLE_NAME IS NOT NULL
    ) THEN
        ALTER TABLE `attendance_records` 
        ADD FOREIGN KEY (`semester_id`) REFERENCES `semesters`(`semester_id`) ON DELETE SET NULL;
    END IF;
END$$
DELIMITER ;

CALL `upgrade_step`();
DROP PROCEDURE IF EXISTS `upgrade_step`;
