-- ============================================================================
-- P3 canary 3a·四态脚手架（pong 引擎游戏入 feed 真玩）—— adapt 自 p2_scaffold_4state.sql
-- 缘由：P3 把 L1 便宜模型生成的 pong 游戏经后端入 feed、真人浏览器真玩（护城河可演示）。
--       复用 P2 灌库路，新建一款已发布游戏 gameId=9301 供 p3_seed_pong.mjs 覆写其运行包 engineBundle=pong bundle。
--       （pong bundle 与 P2 ref bundle 同构：iife 全局 __GameBundle，含 bootGameHost/GameHostFactory，引擎真入包。）
-- 四态：game_project.status=4(PUBLISHED) ∧ game_version.status=3 ∧ game_runtime_package.status=1 ∧ game_feed_rank 行
-- ID：gameId=9301 / versionId=93010 / pkgId=93010 / feedRankId=93010（高位区，与 9201/9202 零碰撞）
-- engineBundle 由后续 p3_seed_pong.mjs 9301 覆写 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 = 9301;
DELETE FROM game_runtime_package  WHERE game_id = 9301;
DELETE FROM game_version          WHERE game_id = 9301;
DELETE FROM game_project          WHERE id      = 9301;

-- ① project：已发布(status=4)，current_version_id 指向 93010，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
  (9301, 1, '弹球对战(Pong·引擎真渲)', 'P3 canary：便宜模型生成的 pong 经 bootGameHost 引擎真渲入 feed 真玩',
   'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==',
   'P3,engine,pong,wg1-gen', 'pong', 'all',
   4, 93010, NULL, b'0', 0, 0,
   'p3-scaffold-pong', NOW(), 'p3-scaffold-pong', 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
  (93010, 9301, 1, NULL, '', 0, '',
   3, 'p3-scaffold-pong', NOW(), 'p3-scaffold-pong', 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
  (93010, 9301, 93010, 'pong', '', 'index.html', '1.0.0', 'eager',
   0, '', 'allow-scripts', '', 1,
   'p3-scaffold-pong', NOW(), 'p3-scaffold-pong', NOW(), b'0', 0, NULL);

-- ④ feed_rank：zone_id=0(混合流) + status=1 入流 + sort_score 给正值(置流首便于 CDP 真玩定位)
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
  (93010, 9301, 93010, 0, 90.0000, 10.0000, 100.0000, 1, 1,
   'p3-scaffold-pong', NOW(), 'p3-scaffold-pong', NOW(), b'0', 0);

COMMIT;

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