CREATE TABLE IF NOT EXISTS `tb_agent_category` (
    `id` BIGINT NOT NULL COMMENT 'ID',
    `tenant_id` BIGINT NULL COMMENT '租户ID',
    `category_name` VARCHAR(128) NOT NULL COMMENT '分类名称',
    `sort_no` INT DEFAULT 0 COMMENT '排序',
    `status` INT DEFAULT 1 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`),
    KEY `idx_agent_category_tenant` (`tenant_id`, `status`, `sort_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Agent 分类';

CREATE TABLE IF NOT EXISTS `tb_agent` (
    `id` BIGINT NOT NULL COMMENT 'ID',
    `tenant_id` BIGINT NULL COMMENT '租户ID',
    `dept_id` BIGINT NULL COMMENT '部门ID',
    `name` VARCHAR(128) NOT NULL COMMENT 'Agent 名称',
    `description` VARCHAR(1024) NULL COMMENT '描述',
    `avatar` VARCHAR(512) NULL COMMENT '头像',
    `category_id` BIGINT NULL COMMENT '分类ID',
    `model_id` BIGINT NULL COMMENT '模型ID',
    `model_config_json` JSON NULL COMMENT '模型配置',
    `generation_config_json` JSON NULL COMMENT '生成参数配置',
    `prompt_config_json` JSON NULL COMMENT '提示词配置',
    `memory_config_json` JSON NULL COMMENT '记忆配置',
    `execution_config_json` JSON NULL COMMENT '运行配置',
    `status` INT DEFAULT 1 COMMENT '数据状态',
    `visibility_scope` VARCHAR(32) NULL COMMENT '可见范围',
    `publish_status` VARCHAR(32) DEFAULT 'DRAFT' COMMENT '发布状态',
    `current_approval_instance_id` BIGINT NULL COMMENT '当前审批实例ID',
    `published_snapshot_json` JSON NULL COMMENT '已发布快照',
    `published_at` DATETIME NULL COMMENT '发布时间',
    `published_by` BIGINT NULL 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`),
    KEY `idx_agent_tenant_category` (`tenant_id`, `category_id`, `status`),
    KEY `idx_agent_publish_status` (`publish_status`),
    KEY `idx_agent_created_by` (`created_by`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Agent';

CREATE TABLE IF NOT EXISTS `tb_agent_tool_binding` (
    `id` BIGINT NOT NULL COMMENT 'ID',
    `tenant_id` BIGINT NULL COMMENT '租户ID',
    `agent_id` BIGINT NOT NULL COMMENT 'Agent ID',
    `tool_type` VARCHAR(32) NOT NULL COMMENT '工具类型',
    `target_id` BIGINT NOT NULL COMMENT '目标资源ID',
    `tool_name` VARCHAR(128) NULL COMMENT '工具名称',
    `enabled` TINYINT(1) DEFAULT 1 COMMENT '是否启用',
    `hitl_enabled` TINYINT(1) DEFAULT 0 COMMENT '是否启用工具执行前确认',
    `hitl_config_json` JSON NULL COMMENT 'HITL 配置',
    `options_json` JSON NULL COMMENT '选项',
    `sort_no` INT 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`),
    KEY `idx_agent_tool_agent` (`agent_id`, `enabled`, `sort_no`),
    KEY `idx_agent_tool_target` (`tool_type`, `target_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Agent 工具绑定';

CREATE TABLE IF NOT EXISTS `tb_agent_knowledge_binding` (
    `id` BIGINT NOT NULL COMMENT 'ID',
    `tenant_id` BIGINT NULL COMMENT '租户ID',
    `agent_id` BIGINT NOT NULL COMMENT 'Agent ID',
    `knowledge_id` BIGINT NOT NULL COMMENT '知识库ID',
    `retrieval_mode` VARCHAR(32) NULL COMMENT '检索模式',
    `enabled` TINYINT(1) DEFAULT 1 COMMENT '是否启用',
    `options_json` JSON NULL COMMENT '选项',
    `sort_no` INT 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`),
    KEY `idx_agent_knowledge_agent` (`agent_id`, `enabled`, `sort_no`),
    KEY `idx_agent_knowledge_target` (`knowledge_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Agent 知识库绑定';
