-- Task center archive GC metadata only. These tables are deliberately small and do not alter the
-- 35M-row outbox; the heavy Game King secondary index remains the independently scheduled DDL in 058.
CREATE TABLE IF NOT EXISTS `task_center_archive_manifest` (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT 'manifest主键',
  `object_type` varchar(32) NOT NULL COMMENT 'ARCHIVE或GC_RECOVERY',
  `run_id` varchar(64) NOT NULL DEFAULT '' COMMENT 'GC运行ID，普通归档为空',
  `cos_key` varchar(512) NOT NULL COMMENT 'COS对象Key',
  `parent_cos_key` varchar(512) NOT NULL DEFAULT '' COMMENT '恢复包对应的原始归档Key',
  `sha256` char(64) NOT NULL COMMENT '压缩对象SHA256',
  `etag` varchar(128) NOT NULL DEFAULT '' COMMENT 'COS ETag',
  `compressed_size` bigint NOT NULL COMMENT '压缩对象字节数',
  `row_count` int NOT NULL COMMENT '对象JSONL行数',
  `min_outbox_id` bigint DEFAULT NULL COMMENT '对象最小outbox ID；旧对象无法完整推导时为空',
  `max_outbox_id` bigint DEFAULT NULL COMMENT '对象最大outbox ID；旧对象无法完整推导时为空',
  `min_occurred_at` datetime(3) DEFAULT NULL COMMENT '对象最早业务时间',
  `max_occurred_at` datetime(3) DEFAULT NULL COMMENT '对象最晚业务时间',
  `verification_status` varchar(32) NOT NULL COMMENT 'VERIFIED或FAILED',
  `verified_at` datetime(3) DEFAULT NULL COMMENT '最后一次完整回读校验时间',
  `create_time` datetime(3) NOT NULL COMMENT '创建时间',
  `update_time` datetime(3) NOT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_task_center_archive_manifest_cos_key` (`cos_key`),
  KEY `idx_task_center_archive_manifest_run` (`run_id`, `object_type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='任务中心COS归档与GC恢复包校验清单';

-- id 固定为 1：所有 API/consumer 实例锁定同一行取得数据库租约，避免多节点并发清理。
CREATE TABLE IF NOT EXISTS `task_center_archive_gc_run` (
  `id` tinyint unsigned NOT NULL COMMENT '固定为1的全局控制行',
  `run_id` varchar(64) NOT NULL DEFAULT '' COMMENT '本次固定cutoff运行ID',
  `status` varchar(32) NOT NULL DEFAULT 'IDLE' COMMENT 'IDLE/DRY_RUN/RUNNING/PAUSED/COMPLETED/FAILED',
  `cutoff_time` datetime(3) DEFAULT NULL COMMENT '预检时冻结的清理截止时间',
  `batch_size` int NOT NULL DEFAULT '200' COMMENT '单tick删除上限，代码限制200到500',
  `scanned_rows` bigint NOT NULL DEFAULT '0',
  `eligible_rows` bigint NOT NULL DEFAULT '0',
  `protected_rows` bigint NOT NULL DEFAULT '0',
  `deleted_rows` bigint NOT NULL DEFAULT '0',
  `recovery_object_count` bigint NOT NULL DEFAULT '0',
  `cursor_create_time` datetime(3) DEFAULT NULL COMMENT '最后成功删除行的create_time，仅展示不用于跳过数据',
  `cursor_id` bigint NOT NULL DEFAULT '0' COMMENT '最后成功删除行ID，仅展示不用于跳过数据',
  `lease_owner` varchar(160) NOT NULL DEFAULT '' COMMENT '当前tick实例',
  `lease_until` datetime(3) DEFAULT NULL COMMENT '短租约到期时间',
  `last_error` varchar(1000) NOT NULL DEFAULT '',
  `create_time` datetime(3) NOT NULL,
  `update_time` datetime(3) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='任务中心归档GC全局控制与租约';

INSERT INTO `task_center_archive_gc_run`
  (`id`, `status`, `batch_size`, `create_time`, `update_time`)
VALUES
  (1, 'IDLE', 200, NOW(3), NOW(3))
ON DUPLICATE KEY UPDATE `id` = VALUES(`id`);

-- 补数水位让服务重启后仍从已确认游标继续；GC即使完成一批也不能依赖调用方内存判断历史是否补齐。
CREATE TABLE IF NOT EXISTS `yumi_game_king_backfill_state` (
  `activity_id` bigint NOT NULL COMMENT '活动ID',
  `last_outbox_id` bigint NOT NULL DEFAULT '0' COMMENT '最后确认的outbox ID',
  `last_occurred_at` datetime(3) DEFAULT NULL COMMENT '与ID组成稳定复合游标',
  `scanned_rows` bigint NOT NULL DEFAULT '0',
  `accepted_rows` bigint NOT NULL DEFAULT '0',
  `completed` tinyint(1) NOT NULL DEFAULT '0' COMMENT '最近一次扫描是否到达当前末尾',
  `create_time` datetime(3) NOT NULL,
  `update_time` datetime(3) NOT NULL,
  PRIMARY KEY (`activity_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Yumi游戏王持久化补数水位';
