SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

-- Remove publish audit children before their parent rows. The joins start from the exact template through
-- idx_admin_policy_instances_template, then use the existing job/item indexes; no audit-table full scan is required.
-- These are policy configuration/audit tables only; balances, wallet transactions, Daily Task definitions,
-- and Host salary policies are outside this migration.
DELETE item
FROM admin_policy_publish_items AS item
INNER JOIN admin_policy_publish_jobs AS job ON job.job_id = item.job_id
INNER JOIN admin_policy_instances AS instance
  ON instance.instance_id = job.instance_id
 AND instance.template_code = 'fami_guild_revenue_policy'
WHERE job.template_code = 'fami_guild_revenue_policy';

DELETE job
FROM admin_policy_publish_jobs AS job
INNER JOIN admin_policy_instances AS instance
  ON instance.instance_id = job.instance_id
 AND instance.template_code = 'fami_guild_revenue_policy'
WHERE job.template_code = 'fami_guild_revenue_policy';

-- template_code is the exact removed capability identity; no product-name branch is added to runtime code.
DELETE FROM admin_policy_instances
WHERE template_code = 'fami_guild_revenue_policy';

DELETE FROM admin_policy_template_versions
WHERE template_code = 'fami_guild_revenue_policy';

DELETE FROM admin_policy_templates
WHERE template_code = 'fami_guild_revenue_policy';

-- Keep the three POINT financial fields. Team salary, coin-seller and invite configuration have independent owners;
-- their duplicate PolicyConfig sections are removed so Admin cannot continue presenting inert settings.
UPDATE admin_policy_template_versions
SET rule_json = JSON_REMOVE(
  rule_json,
  '$.allow_self_brushing',
  '$.google_coin_per_usd',
  '$.host.point_ratio_percent',
  '$.host.use_gift_type_ratio',
  '$.host.affects_room_heat',
	'$.agency',
	'$.agent',
	'$.bd',
	'$.manager',
	'$.coin_seller',
	'$.game_invite',
  '$.tasks'
);

UPDATE admin_policy_templates
SET rule_json = JSON_REMOVE(
  rule_json,
  '$.allow_self_brushing',
  '$.google_coin_per_usd',
  '$.host.point_ratio_percent',
  '$.host.use_gift_type_ratio',
  '$.host.affects_room_heat',
	'$.agency',
	'$.agent',
	'$.bd',
	'$.manager',
	'$.coin_seller',
	'$.game_invite',
  '$.tasks'
);

-- 菜单和权限 code 都有唯一索引；以下点更新只修正文案，不重命名权限码、路由或存量 template_code。
UPDATE admin_menus
SET title = 'POINT 钱包政策',
    updated_at_ms = CAST(UNIX_TIMESTAMP(CURRENT_TIMESTAMP(3)) * 1000 AS UNSIGNED)
WHERE code = 'policy-template';

UPDATE admin_permissions
SET name = CASE code
      WHEN 'policy-template:view' THEN 'POINT 钱包政策查看'
      WHEN 'policy-template:create' THEN 'POINT 钱包政策创建'
      WHEN 'policy-instance:create' THEN 'POINT 钱包政策实例创建'
      WHEN 'policy-instance:update' THEN 'POINT 钱包政策实例更新'
      WHEN 'policy-instance:publish' THEN 'POINT 钱包政策实例发布'
      ELSE name
    END,
    description = CASE code
      WHEN 'policy-template:view' THEN '允许查看 POINT 钱包政策和实例'
      WHEN 'policy-template:create' THEN '允许创建 POINT 钱包政策'
      WHEN 'policy-instance:create' THEN '允许创建 POINT 钱包政策实例'
      WHEN 'policy-instance:update' THEN '允许更新 POINT 钱包政策实例状态'
      WHEN 'policy-instance:publish' THEN '允许发布 POINT 钱包政策实例到运行侧'
      ELSE description
    END,
    updated_at_ms = CAST(UNIX_TIMESTAMP(CURRENT_TIMESTAMP(3)) * 1000 AS UNSIGNED)
WHERE code IN (
  'policy-template:view',
  'policy-template:create',
  'policy-instance:create',
  'policy-instance:update',
  'policy-instance:publish'
);
