SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

-- 归档回执属于 Wallet owner 库；单文件迁移必须显式固定数据库边界。
USE hyapp_wallet;

-- wallet_outbox archive-only 回执表。本迁移不修改、不删除 wallet_outbox，对热表无重写和锁表风险。
CREATE TABLE IF NOT EXISTS wallet_outbox_archive_receipts (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码',
  batch_id CHAR(64) NOT NULL COMMENT '归档批次内容指纹',
  state VARCHAR(32) NOT NULL COMMENT '只有 VERIFIED 可作为后续归档游标，不代表已恢复演练',
  data_object_key VARCHAR(512) NOT NULL,
  manifest_object_key VARCHAR(512) NOT NULL,
  success_object_key VARCHAR(512) NOT NULL,
  schema_version INT NOT NULL,
  row_count INT NOT NULL,
  first_updated_at_ms BIGINT NOT NULL,
  first_event_id VARCHAR(128) NOT NULL,
  last_updated_at_ms BIGINT NOT NULL,
  last_event_id VARCHAR(128) NOT NULL,
  min_created_at_ms BIGINT NOT NULL,
  max_created_at_ms BIGINT NOT NULL,
  available_delta_total VARCHAR(80) NOT NULL,
  frozen_delta_total VARCHAR(80) NOT NULL,
  ndjson_sha256 CHAR(64) NOT NULL,
  gzip_sha256 CHAR(64) NOT NULL,
  data_crc64 VARCHAR(32) NOT NULL,
  manifest_crc64 VARCHAR(32) NOT NULL,
  success_crc64 VARCHAR(32) NOT NULL,
  manifest_sha256 CHAR(64) NOT NULL,
  uncompressed_bytes BIGINT NOT NULL,
  compressed_bytes BIGINT NOT NULL,
  verified_at_ms BIGINT NOT NULL,
  created_at_ms BIGINT NOT NULL,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, batch_id),
  KEY idx_wallet_outbox_archive_cursor (app_code, state, last_updated_at_ms, last_event_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='wallet outbox 外部归档读回验证回执';
