CREATE TABLE IF NOT EXISTS `tb_skill_category` (
    `id` BIGINT NOT NULL COMMENT 'ID',
    `tenant_id` BIGINT NULL COMMENT '租户ID',
    `parent_id` BIGINT NULL COMMENT '父分类ID',
    `category_name` VARCHAR(128) NOT NULL COMMENT '分类名称',
    `level_no` INT DEFAULT 1 COMMENT '层级',
    `ancestors` VARCHAR(512) 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_skill_category_tenant_parent` (`tenant_id`, `parent_id`, `status`, `sort_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Skill 分类';

CREATE TABLE IF NOT EXISTS `tb_skill` (
    `id` BIGINT NOT NULL COMMENT 'ID',
    `tenant_id` BIGINT NULL COMMENT '租户ID',
    `dept_id` BIGINT NULL COMMENT '部门ID',
    `category_id` BIGINT NULL COMMENT '分类ID',
    `name` VARCHAR(128) NOT NULL COMMENT 'Skill 名称',
    `display_name` VARCHAR(128) NULL COMMENT '展示名称',
    `description` VARCHAR(1024) NULL COMMENT '描述',
    `metadata_json` JSON NULL COMMENT '元数据',
    `skill_content` MEDIUMTEXT NULL COMMENT 'SKILL.md 内容',
    `enabled` TINYINT(1) DEFAULT 1 COMMENT '是否启用',
    `visibility_scope` VARCHAR(32) NULL COMMENT '可见范围',
    `source_type` VARCHAR(32) NULL COMMENT '来源类型',
    `package_hash` VARCHAR(128) NULL COMMENT '包 hash',
    `reference_count` INT DEFAULT 0 COMMENT '引用文档数量',
    `script_count` INT DEFAULT 0 COMMENT '脚本数量',
    `asset_count` INT DEFAULT 0 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`),
    UNIQUE KEY `uk_skill_tenant_name` (`tenant_id`, `name`),
    KEY `idx_skill_tenant_category` (`tenant_id`, `category_id`, `enabled`),
    KEY `idx_skill_publish_status` (`publish_status`),
    KEY `idx_skill_created_by` (`created_by`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Skill';

CREATE TABLE IF NOT EXISTS `tb_skill_reference` (
    `id` BIGINT NOT NULL COMMENT 'ID',
    `tenant_id` BIGINT NULL COMMENT '租户ID',
    `skill_id` BIGINT NOT NULL COMMENT 'Skill ID',
    `path` VARCHAR(512) NOT NULL COMMENT '逻辑路径',
    `name` VARCHAR(255) NULL COMMENT '文件名',
    `content` MEDIUMTEXT NULL COMMENT '内容',
    `content_hash` VARCHAR(128) NULL COMMENT '内容 hash',
    `size` BIGINT DEFAULT 0 COMMENT '大小',
    `metadata_json` JSON NULL COMMENT '元数据',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_skill_reference_path` (`skill_id`, `path`),
    KEY `idx_skill_reference_skill` (`skill_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Skill reference';

CREATE TABLE IF NOT EXISTS `tb_skill_script` (
    `id` BIGINT NOT NULL COMMENT 'ID',
    `tenant_id` BIGINT NULL COMMENT '租户ID',
    `skill_id` BIGINT NOT NULL COMMENT 'Skill ID',
    `path` VARCHAR(512) NOT NULL COMMENT '逻辑路径',
    `language` VARCHAR(32) NULL COMMENT '脚本语言',
    `content` MEDIUMTEXT NULL COMMENT '内容',
    `content_hash` VARCHAR(128) NULL COMMENT '内容 hash',
    `size` BIGINT DEFAULT 0 COMMENT '大小',
    `metadata_json` JSON NULL COMMENT '元数据',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_skill_script_path` (`skill_id`, `path`),
    KEY `idx_skill_script_skill` (`skill_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Skill script';

CREATE TABLE IF NOT EXISTS `tb_skill_asset` (
    `id` BIGINT NOT NULL COMMENT 'ID',
    `tenant_id` BIGINT NULL COMMENT '租户ID',
    `skill_id` BIGINT NOT NULL COMMENT 'Skill ID',
    `path` VARCHAR(512) NOT NULL COMMENT '逻辑路径',
    `name` VARCHAR(255) NULL COMMENT '文件名',
    `media_type` VARCHAR(128) NULL COMMENT '媒体类型',
    `content_ref` VARCHAR(128) NOT NULL COMMENT '内容引用',
    `content_hash` VARCHAR(128) NULL COMMENT '内容 hash',
    `size` BIGINT DEFAULT 0 COMMENT '大小',
    `metadata_json` JSON NULL COMMENT '元数据',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_skill_asset_path` (`skill_id`, `path`),
    KEY `idx_skill_asset_skill` (`skill_id`),
    KEY `idx_skill_asset_content_ref` (`content_ref`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Skill asset';

CREATE TABLE IF NOT EXISTS `tb_skill_asset_content` (
    `content_ref` VARCHAR(128) NOT NULL COMMENT '内容引用',
    `content_hash` VARCHAR(128) NOT NULL COMMENT '内容 hash',
    `file_path` VARCHAR(1024) NOT NULL COMMENT '文件路径',
    `media_type` VARCHAR(128) NULL COMMENT '媒体类型',
    `size` BIGINT DEFAULT 0 COMMENT '大小',
    `ref_count` INT DEFAULT 0 COMMENT '引用数',
    `created` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `modified` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
    PRIMARY KEY (`content_ref`),
    KEY `idx_skill_asset_content_hash` (`content_hash`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Skill asset 内容索引';
