-- MySQL 建表脚本
-- 用于 go-trustlog 数据库持久化模块
-- MySQL 8.0+ / MariaDB 10+ 版本

-- ============================================
-- 1. operation 表 - 操作记录表
-- ============================================
CREATE TABLE IF NOT EXISTS operation (
    op_id              VARCHAR(32) NOT NULL PRIMARY KEY,
    op_actor           VARCHAR(64),
    doid               VARCHAR(512),
    producer_id        VARCHAR(32),
    request_body_hash  VARCHAR(128),
    response_body_hash VARCHAR(128),
    sign               VARCHAR(512),
    op_source          VARCHAR(10),
    op_type            VARCHAR(30),
    do_prefix          VARCHAR(128),
    do_repository      VARCHAR(64),
    client_ip          VARCHAR(32) COMMENT '客户端IP（可空，仅落库，不存证）',
    server_ip          VARCHAR(32) COMMENT '服务端IP（可空，仅落库，不存证）',
    trustlog_status    VARCHAR(32) COMMENT '存证状态：NOT_TRUSTLOGGED / TRUSTLOGGED',
    timestamp          DATETIME,
    created_at         DATETIME DEFAULT CURRENT_TIMESTAMP,
    
    INDEX idx_operation_timestamp (timestamp),
    INDEX idx_operation_status (trustlog_status),
    INDEX idx_operation_doid (doid(255))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='操作记录表';

-- ============================================
-- 2. trustlog_cursor 表 - 游标表（任务发现队列）
-- ============================================
CREATE TABLE IF NOT EXISTS trustlog_cursor (
    cursor_key       VARCHAR(64) NOT NULL PRIMARY KEY COMMENT '游标键（如：operation_scan）',
    cursor_value     VARCHAR(128) NOT NULL COMMENT '游标值（最后处理的时间戳，RFC3339Nano格式）',
    last_updated_at  DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后更新时间',
    
    INDEX idx_cursor_updated_at (last_updated_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='游标表，记录扫描位置（Cursor + Retry 双层模式）';

-- ============================================
-- 3. trustlog_retry 表 - 重试表
-- ============================================
CREATE TABLE IF NOT EXISTS trustlog_retry (
    op_id              VARCHAR(32) NOT NULL PRIMARY KEY,
    retry_count        INT DEFAULT 0 COMMENT '重试次数',
    retry_status       VARCHAR(32) DEFAULT 'PENDING' COMMENT '重试状态：PENDING / RETRYING / DEAD_LETTER',
    last_retry_at      DATETIME COMMENT '上次重试时间',
    next_retry_at      DATETIME COMMENT '下次重试时间（用于指数退避）',
    error_message      TEXT COMMENT '错误信息',
    created_at         DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at         DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    
    INDEX idx_retry_status (retry_status),
    INDEX idx_retry_next_retry_at (next_retry_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='重试表，用于管理失败的存证操作';

-- ============================================
-- 验证查询
-- ============================================

-- 查询所有表
SHOW TABLES LIKE 'operation%';
SHOW TABLES LIKE 'trustlog_%';

-- 查询 operation 表结构
DESCRIBE operation;

-- 查询所有索引
SHOW INDEX FROM operation;
SHOW INDEX FROM trustlog_cursor;
SHOW INDEX FROM trustlog_retry;

-- 查询表注释
SELECT 
    TABLE_NAME,
    TABLE_COMMENT
FROM 
    INFORMATION_SCHEMA.TABLES
WHERE 
    TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME IN ('operation', 'trustlog_cursor', 'trustlog_retry');

