SET NAMES utf8mb4;

-- MySQL DDL 不具备整段事务回滚能力，使用存在性守卫保证部分成功后可以安全重跑。
SET @add_skill_published_tool_bindings = (
    SELECT IF(COUNT(1) = 0,
              'ALTER TABLE `tb_skill` ADD COLUMN `published_tool_bindings_json` JSON NULL COMMENT ''已发布平台 Tool 冻结快照'' AFTER `published_snapshot_json`',
              'SELECT 1')
    FROM information_schema.columns
    WHERE table_schema = DATABASE()
      AND table_name = 'tb_skill'
      AND column_name = 'published_tool_bindings_json');
PREPARE add_skill_published_tool_bindings_stmt FROM @add_skill_published_tool_bindings;
EXECUTE add_skill_published_tool_bindings_stmt;
DEALLOCATE PREPARE add_skill_published_tool_bindings_stmt;

CREATE TABLE IF NOT EXISTS `tb_skill_tool_binding` (
    `id` BIGINT NOT NULL COMMENT 'ID',
    `tenant_id` BIGINT NOT NULL COMMENT '租户ID',
    `skill_id` BIGINT NOT NULL COMMENT 'Skill ID',
    `tool_type` VARCHAR(32) NOT NULL COMMENT 'WORKFLOW / PLUGIN / MCP',
    `target_id` BIGINT NOT NULL COMMENT '目标资源ID',
    `hitl_enabled` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否调用前确认',
    `mcp_tool_count` INT NULL COMMENT 'MCP Tool 数量',
    `mcp_tool_manifest_hash` VARCHAR(64) NULL COMMENT 'MCP Tool 清单 hash',
    `sort_no` INT NOT NULL DEFAULT 0 COMMENT '排序',
    `created` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `created_by` BIGINT NULL COMMENT '创建人',
    `modified` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
    `modified_by` BIGINT NULL COMMENT '修改人',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_skill_tool_binding` (`tenant_id`, `skill_id`, `tool_type`, `target_id`),
    KEY `idx_skill_tool_binding_list` (`tenant_id`, `skill_id`, `sort_no`, `id`),
    KEY `idx_skill_tool_binding_target` (`tenant_id`, `tool_type`, `target_id`, `skill_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Skill 平台 Tool 草稿绑定';

CREATE TABLE IF NOT EXISTS `tb_agent_skill_binding` (
    `id` BIGINT NOT NULL COMMENT 'ID',
    `tenant_id` BIGINT NOT NULL COMMENT '租户ID',
    `agent_id` BIGINT NOT NULL COMMENT 'Agent ID',
    `skill_id` BIGINT NOT NULL COMMENT 'Skill ID',
    `sort_no` INT NOT NULL DEFAULT 0 COMMENT '排序',
    `created` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `created_by` BIGINT NULL COMMENT '创建人',
    `modified` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
    `modified_by` BIGINT NULL COMMENT '修改人',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_agent_skill_binding` (`tenant_id`, `agent_id`, `skill_id`),
    KEY `idx_agent_skill_binding_list` (`tenant_id`, `agent_id`, `sort_no`, `id`),
    KEY `idx_agent_skill_binding_target` (`tenant_id`, `skill_id`, `agent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Agent Skill 草稿绑定';

-- 首期统一知识库和 Skill 在菜单与聊天状态中的语义图标。
UPDATE `tb_sys_menu`
SET `menu_icon` = 'lucide:book-open-text',
    `modified` = `modified`,
    `modified_by` = `modified_by`
WHERE `id` = 259170422338478080
  AND `menu_url` = '/ai/documentCollection';

UPDATE `tb_sys_menu`
SET `menu_icon` = 'lucide:notebook-tabs',
    `modified` = `modified`,
    `modified_by` = `modified_by`
WHERE `id` = 367400000000000001
  AND `menu_url` = '/ai/skill';
