SET @skill_content_storage_locator_ddl = (
    SELECT IF(
        EXISTS(
            SELECT 1
            FROM information_schema.columns
            WHERE table_schema = DATABASE()
              AND table_name = 'tb_skill_content'
              AND column_name = 'storage_locator'
        ),
        'SELECT 1',
        'ALTER TABLE `tb_skill_content` ADD COLUMN `storage_locator` VARCHAR(2048) NULL COMMENT ''稳定存储定位符'' AFTER `file_path`'
    )
);

PREPARE skill_content_storage_locator_stmt FROM @skill_content_storage_locator_ddl;
EXECUTE skill_content_storage_locator_stmt;
DEALLOCATE PREPARE skill_content_storage_locator_stmt;

CREATE TABLE IF NOT EXISTS `tb_skill_content_write_intent` (
    `content_ref` VARCHAR(128) NOT NULL COMMENT '内容引用',
    `reservation_token` VARCHAR(128) NOT NULL COMMENT '写入预留令牌',
    `content_hash` VARCHAR(128) NOT NULL COMMENT '内容hash',
    `storage_locator` VARCHAR(2048) NOT NULL COMMENT '稳定存储定位符',
    `media_type` VARCHAR(128) NULL COMMENT '媒体类型',
    `size` BIGINT NOT NULL DEFAULT 0 COMMENT '字节数',
    `state` VARCHAR(16) NOT NULL COMMENT 'PENDING/WRITING/CLEANING',
    `created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `modified` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
    PRIMARY KEY (`content_ref`),
    KEY `idx_skill_content_write_intent_state_modified` (`state`, `modified`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Skill 内容写入意图';
