CREATE TABLE IF NOT EXISTS `chat_round`
(
    `id`                            bigint UNSIGNED NOT NULL COMMENT '轮次ID',
    `session_id`                    bigint UNSIGNED NOT NULL COMMENT '会话ID',
    `round_no`                      int NOT NULL COMMENT '轮次序号',
    `user_message_id`               bigint UNSIGNED NULL DEFAULT NULL COMMENT '用户消息ID',
    `selected_assistant_message_id` bigint UNSIGNED NULL DEFAULT NULL COMMENT '当前选中的助手答案消息ID',
    `selected_variant_index`        int NOT NULL DEFAULT 0 COMMENT '当前选中的答案版本序号',
    `variant_count`                 int NOT NULL DEFAULT 0 COMMENT '答案版本总数',
    `status`                        varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT 'READY' COMMENT '轮次状态',
    `created`                       datetime NOT NULL COMMENT '创建时间',
    `modified`                      datetime NOT NULL COMMENT '修改时间',
    PRIMARY KEY (`id`) USING BTREE,
    UNIQUE KEY `uk_chat_round_session_round_no` (`session_id`, `round_no`) USING BTREE,
    KEY `idx_chat_round_session_modified` (`session_id`, `modified`, `id`) USING BTREE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '聊天轮次热数据表';

SET @chat_log_template_alter = (
    SELECT CASE
               WHEN COUNT(1) = 0 THEN 'SELECT 1'
               ELSE CONCAT(
                       'ALTER TABLE `chat_log_template` ',
                       GROUP_CONCAT(stmt ORDER BY ord SEPARATOR ', ')
                    )
           END
    FROM (
             SELECT 1 AS ord,
                    'ADD COLUMN `round_id` bigint UNSIGNED NULL DEFAULT NULL COMMENT ''轮次ID'' AFTER `assistant_id`' AS stmt
             WHERE NOT EXISTS (
                 SELECT 1
                 FROM information_schema.columns
                 WHERE table_schema = DATABASE()
                   AND table_name = 'chat_log_template'
                   AND column_name = 'round_id'
             )
             UNION ALL
             SELECT 2 AS ord,
                    'ADD COLUMN `round_no` int NULL DEFAULT NULL COMMENT ''轮次序号'' AFTER `round_id`' AS stmt
             WHERE NOT EXISTS (
                 SELECT 1
                 FROM information_schema.columns
                 WHERE table_schema = DATABASE()
                   AND table_name = 'chat_log_template'
                   AND column_name = 'round_no'
             )
             UNION ALL
             SELECT 3 AS ord,
                    'ADD COLUMN `message_kind` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT ''消息类型'' AFTER `sender_role`' AS stmt
             WHERE NOT EXISTS (
                 SELECT 1
                 FROM information_schema.columns
                 WHERE table_schema = DATABASE()
                   AND table_name = 'chat_log_template'
                   AND column_name = 'message_kind'
             )
             UNION ALL
             SELECT 4 AS ord,
                    'ADD COLUMN `variant_index` int NULL DEFAULT NULL COMMENT ''答案版本序号'' AFTER `message_kind`' AS stmt
             WHERE NOT EXISTS (
                 SELECT 1
                 FROM information_schema.columns
                 WHERE table_schema = DATABASE()
                   AND table_name = 'chat_log_template'
                   AND column_name = 'variant_index'
             )
         ) changes
);

PREPARE stmt_chat_log_template_alter FROM @chat_log_template_alter;
EXECUTE stmt_chat_log_template_alter;
DEALLOCATE PREPARE stmt_chat_log_template_alter;

DROP PROCEDURE IF EXISTS migrate_chat_round_log_columns;

DELIMITER $$

CREATE PROCEDURE migrate_chat_round_log_columns()
BEGIN
    DECLARE done INT DEFAULT 0;
    DECLARE v_table_name varchar(128);
    DECLARE v_sql LONGTEXT;
    DECLARE table_cursor CURSOR FOR
        SELECT table_name
        FROM information_schema.tables
        WHERE table_schema = DATABASE()
          AND table_name LIKE 'chat_log\\_%'
          AND table_name <> 'chat_log_template';
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

    OPEN table_cursor;

    table_loop:
    LOOP
        FETCH table_cursor INTO v_table_name;
        IF done = 1 THEN
            LEAVE table_loop;
        END IF;

        SET v_sql = (
            SELECT CASE
                       WHEN COUNT(1) = 0 THEN 'SELECT 1'
                       ELSE CONCAT(
                               'ALTER TABLE `', v_table_name, '` ',
                               GROUP_CONCAT(stmt ORDER BY ord SEPARATOR ', ')
                            )
                   END
            FROM (
                     SELECT 1 AS ord,
                            'ADD COLUMN `round_id` bigint UNSIGNED NULL DEFAULT NULL COMMENT ''轮次ID'' AFTER `assistant_id`' AS stmt
                     WHERE NOT EXISTS (
                         SELECT 1
                         FROM information_schema.columns
                         WHERE table_schema = DATABASE()
                           AND table_name = v_table_name
                           AND column_name = 'round_id'
                     )
                     UNION ALL
                     SELECT 2 AS ord,
                            'ADD COLUMN `round_no` int NULL DEFAULT NULL COMMENT ''轮次序号'' AFTER `round_id`' AS stmt
                     WHERE NOT EXISTS (
                         SELECT 1
                         FROM information_schema.columns
                         WHERE table_schema = DATABASE()
                           AND table_name = v_table_name
                           AND column_name = 'round_no'
                     )
                     UNION ALL
                     SELECT 3 AS ord,
                            'ADD COLUMN `message_kind` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT ''消息类型'' AFTER `sender_role`' AS stmt
                     WHERE NOT EXISTS (
                         SELECT 1
                         FROM information_schema.columns
                         WHERE table_schema = DATABASE()
                           AND table_name = v_table_name
                           AND column_name = 'message_kind'
                     )
                     UNION ALL
                     SELECT 4 AS ord,
                            'ADD COLUMN `variant_index` int NULL DEFAULT NULL COMMENT ''答案版本序号'' AFTER `message_kind`' AS stmt
                     WHERE NOT EXISTS (
                         SELECT 1
                         FROM information_schema.columns
                         WHERE table_schema = DATABASE()
                           AND table_name = v_table_name
                           AND column_name = 'variant_index'
                     )
                 ) changes
        );

        SET @chat_round_log_table_alter = v_sql;
        PREPARE stmt_chat_round_log_alter FROM @chat_round_log_table_alter;
        EXECUTE stmt_chat_round_log_alter;
        DEALLOCATE PREPARE stmt_chat_round_log_alter;
    END LOOP;

    CLOSE table_cursor;
END $$

DELIMITER ;

CALL migrate_chat_round_log_columns();
DROP PROCEDURE IF EXISTS migrate_chat_round_log_columns;
