CREATE TABLE IF NOT EXISTS `tb_agent_document_attachment` (
    `id` BIGINT NOT NULL COMMENT 'ID',
    `attachment_id` VARCHAR(64) NOT NULL COMMENT '稳定附件ID',
    `upload_id` VARCHAR(64) NOT NULL COMMENT '临时上传ID',
    `tenant_id` BIGINT NOT NULL COMMENT '租户ID',
    `user_id` BIGINT NOT NULL COMMENT '上传用户ID',
    `agent_id` BIGINT NOT NULL COMMENT 'Agent ID',
    `mode` VARCHAR(16) NOT NULL COMMENT '聊天模式',
    `session_id` VARCHAR(128) NOT NULL COMMENT '聊天会话ID',
    `message_id` VARCHAR(64) NULL COMMENT '绑定消息ID',
    `original_name` VARCHAR(255) NOT NULL COMMENT '原始文件名',
    `extension` VARCHAR(16) NOT NULL COMMENT '扩展名',
    `mime_type` VARCHAR(128) NOT NULL COMMENT 'MIME类型',
    `file_size` BIGINT NOT NULL COMMENT '文件字节数',
    `file_sha256` CHAR(64) NULL COMMENT '文件SHA-256',
    `object_key` VARCHAR(512) NOT NULL COMMENT '私有原文件对象键',
    `status` VARCHAR(32) NOT NULL COMMENT '附件状态',
    `current_snapshot_id` VARCHAR(64) NULL COMMENT '当前读取快照ID',
    `error_code` VARCHAR(64) NULL COMMENT '错误码',
    `error_message` VARCHAR(1024) NULL COMMENT '可执行错误消息',
    `expires_at` DATETIME NULL COMMENT '临时附件过期时间',
    `version` BIGINT 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_document_attachment_id` (`attachment_id`),
    UNIQUE KEY `uk_agent_document_upload_id` (`upload_id`),
    UNIQUE KEY `uk_agent_document_message_attachment` (`message_id`, `attachment_id`),
    KEY `idx_agent_document_owner_upload` (`tenant_id`, `user_id`, `upload_id`),
    KEY `idx_agent_document_scope` (`tenant_id`, `user_id`, `agent_id`, `mode`, `session_id`),
    KEY `idx_agent_document_status_modified` (`status`, `modified`),
    KEY `idx_agent_document_expiry_status` (`expires_at`, `status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Agent 文档附件状态账本';

CREATE TABLE IF NOT EXISTS `tb_agent_document_snapshot` (
    `id` BIGINT NOT NULL COMMENT 'ID',
    `read_snapshot_id` VARCHAR(64) NOT NULL COMMENT '读取快照ID',
    `tenant_id` BIGINT NOT NULL COMMENT '租户ID',
    `attachment_id` VARCHAR(64) NOT NULL COMMENT '稳定附件ID',
    `file_sha256` CHAR(64) NOT NULL COMMENT '文件SHA-256',
    `reader_version` VARCHAR(32) NOT NULL COMMENT '读取器版本',
    `read_policy_version` VARCHAR(32) NOT NULL COMMENT '读取策略版本',
    `snapshot_object_key` VARCHAR(512) NOT NULL COMMENT '私有快照对象键',
    `char_count` INT NOT NULL COMMENT '字符数',
    `token_estimate` INT NOT NULL COMMENT 'Token估算',
    `segment_count` INT NOT NULL COMMENT '片段数',
    `status` VARCHAR(32) NOT NULL COMMENT '快照状态',
    `error_code` VARCHAR(64) 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_agent_document_snapshot_id` (`read_snapshot_id`),
    UNIQUE KEY `uk_agent_document_snapshot_version`
        (`attachment_id`, `file_sha256`, `reader_version`, `read_policy_version`),
    KEY `idx_agent_document_snapshot_attachment` (`attachment_id`),
    KEY `idx_agent_document_snapshot_status_modified` (`status`, `modified`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Agent 文档不可变读取快照';
