-- ============================================================================
-- 龙虾引擎模型升级补丁
-- 修复: P0 canvasData缺失, P0 Task字段缺失, P2 NodeExecutionLog缺失列,
--       P1 ConversationSummary自动摘要字段缺失
-- 日期: 2026-06-05
-- ============================================================================

-- 1. lobster_node_execution_log 增加 node_code 和 quality_score 列
ALTER TABLE `lobster_node_execution_log`
    ADD COLUMN `node_code` varchar(100) DEFAULT NULL COMMENT '节点编码' AFTER `workflow_id`,
    ADD COLUMN `quality_score` int DEFAULT NULL COMMENT '质量评分(0-100)' AFTER `retry_count`;

-- 2. company_workflow_lobster_task 增加 external_user_id 列（如尚未存在）
ALTER TABLE `company_workflow_lobster_task`
    ADD COLUMN IF NOT EXISTS `external_user_id` varchar(255) DEFAULT NULL COMMENT '外部联系人ID' AFTER `binding_id`;

-- 3. 为 lobster_node_execution_log 增加按节点统计的索引
CREATE INDEX IF NOT EXISTS `idx_exec_log_company_node` ON `lobster_node_execution_log` (`company_id`, `node_code`);

-- 4. lobster_conversation_summary 增加自动摘要扩展列
ALTER TABLE `lobster_conversation_summary`
    ADD COLUMN IF NOT EXISTS `external_user_id` varchar(128) DEFAULT NULL COMMENT '外部联系人ID' AFTER `message_count`,
    ADD COLUMN IF NOT EXISTS `summary_text` text DEFAULT NULL COMMENT '自动摘要原始文本' AFTER `external_user_id`,
    ADD COLUMN IF NOT EXISTS `chat_msg_count` int DEFAULT NULL COMMENT '聊天消息计数' AFTER `summary_text`,
    ADD COLUMN IF NOT EXISTS `key_intents` text DEFAULT NULL COMMENT '关键意图JSON' AFTER `chat_msg_count`,
    ADD COLUMN IF NOT EXISTS `key_variables` text DEFAULT NULL COMMENT '关键变量JSON' AFTER `key_intents`,
    ADD COLUMN IF NOT EXISTS `stage` varchar(64) DEFAULT NULL COMMENT '当前阶段' AFTER `key_variables`,
    ADD COLUMN IF NOT EXISTS `next_action_hint` text DEFAULT NULL COMMENT '下一步行动提示' AFTER `stage`;

-- 5. lobster_conversation_summary 增加索引
CREATE INDEX IF NOT EXISTS `idx_conv_summary_external_user` ON `lobster_conversation_summary` (`company_id`, `external_user_id`);
