-- ============================================================================
-- P2 真机门·四态脚手架（W-CLEAN 清库后重建一款已发布游戏供 engineBundle 灌库桩附着）
-- 缘由：seed 脚本 p2_seed_engine_bundle.mjs 原假设已存在已发布种子(9001 四态齐备)，
--       但 staging DB 已被 W-CLEAN 清空(0 version/0 package/0 feed_rank)，故须先建四态脚手架。
--       （mini-desktop 2026-06-13 P2 真机门实测可用，gate 报告逮出后落库归仓。）
-- 四态：game_project.status=4(PUBLISHED) ∧ game_version.status=3 ∧ game_runtime_package.status=1 ∧ game_feed_rank 行
-- ID：gameId=9201 / versionId=92010 / pkgId=92010 / feedRankId=92010（高位区，零碰撞）
-- engineBundle 由后续 p2_seed_engine_bundle.mjs 9201 覆写 package_json（本脚本先建空 package_json 占位行）
-- 幂等：先 DELETE 同 ID 行再 INSERT（纯重建，可重复执行）
-- tenant：staging application-staging.yaml tenant.enable=false → tenant_id=0 即可
-- 用法：docker exec -i game-staging-mysql mysql --default-character-set=utf8mb4 -uroot -p"$MYSQL_ROOT_PASSWORD" < 本文件
-- ============================================================================
USE `ruoyi-vue-pro`;
-- charset 防御（与 seed 脚本同口径）：强制 utf8mb4，避免中文 title 被客户端默认 latin1 双编码
SET NAMES utf8mb4;

START TRANSACTION;

-- 清旧（幂等重建）
DELETE FROM game_feed_rank        WHERE game_id = 9201;
DELETE FROM game_runtime_package  WHERE game_id = 9201;
DELETE FROM game_version          WHERE game_id = 9201;
DELETE FROM game_project          WHERE id      = 9201;

-- ① project：已发布(status=4)，current_version_id 指向 92010，launch_zone_id 留 NULL(→feed zone 0)
INSERT INTO game_project
  (id, creator_user_id, title, summary, cover_url, tags, template_id, age_rating,
   status, current_version_id, launch_zone_id, featured, play_count, like_count,
   creator, create_time, updater, update_time, deleted, tenant_id)
VALUES
  (9201, 1, '王蓝莓小卖部(引擎真渲)', 'P2 通用宿主真机门：ref 引擎 bundle 经 bootGameHost 真渲入 feed',
   'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==',
   'P2,engine,wanglanmei', 'wanglanmei-ref', 'all',
   4, 92010, NULL, b'0', 0, 0,
   'p2-scaffold', NOW(), 'p2-scaffold', NOW(), b'0', 0);

-- ② version：已发布(status=3)，package_url 空(走 DB-manifest 端点)，checksum 先占位(seed 脚本覆写运行包不动版本)
INSERT INTO game_version
  (id, game_id, version_no, gen_task_id, package_url, bundle_size, checksum,
   status, creator, create_time, updater, update_time, deleted, tenant_id)
VALUES
  (92010, 9201, 1, NULL, '', 0, '',
   3, 'p2-scaffold', NOW(), 'p2-scaffold', NOW(), b'0', 0);

-- ③ runtime_package：先建 status=1 行 + 空 package_json 占位（engineBundle 由 seed 脚本 UPDATE 覆写本行）
--    sandbox_attr/allow_origins 取既有发布默认；package_url 空=DB-manifest 内嵌路
INSERT INTO game_runtime_package
  (id, game_id, version_id, template_id, package_url, entry, runtime_version, preload_policy,
   bundle_size, checksum, sandbox_attr, allow_origins, status,
   creator, create_time, updater, update_time, deleted, tenant_id, package_json)
VALUES
  (92010, 9201, 92010, 'wanglanmei-ref', '', 'index.html', '1.0.0', 'eager',
   0, '', 'allow-scripts', '', 1,
   'p2-scaffold', NOW(), 'p2-scaffold', NOW(), b'0', 0, NULL);

-- ④ feed_rank：zone_id=0(混合流) + status=1 入流 + sort_score=0
INSERT INTO game_feed_rank
  (id, game_id, version_id, zone_id, quality_score, boost, sort_score, pinned, status,
   creator, create_time, updater, update_time, deleted, tenant_id)
VALUES
  (92010, 9201, 92010, 0, 0.0000, 0.0000, 0.0000, 0, 1,
   'p2-scaffold', NOW(), 'p2-scaffold', NOW(), b'0', 0);

COMMIT;

-- 回读四态
SELECT 'project' k, id, status, current_version_id FROM game_project WHERE id=9201
UNION ALL SELECT 'version', id, status, game_id FROM game_version WHERE id=92010
UNION ALL SELECT 'runtime_pkg', id, status, version_id FROM game_runtime_package WHERE id=92010
UNION ALL SELECT 'feed_rank', id, status, zone_id FROM game_feed_rank WHERE id=92010;
