SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

SET @now_ms = CAST(UNIX_TIMESTAMP(UTC_TIMESTAMP(3)) * 1000 AS UNSIGNED);

-- 收益策略模板是 Lalu/Huwaa/Fami 共用的产品配置入口。默认文档放在 merge 左侧，既能创建 agency 对象，
-- 又不会覆盖已存在的配置；旧实例重发仍保持 0 分成和 charge_amount 守恒口径。
UPDATE admin_policy_templates
SET rule_json = JSON_MERGE_PATCH(
      CAST('{"agency":{"point_ratio_percent":0,"share_base":"charge_amount"}}' AS JSON),
      rule_json
    ),
    updated_at_ms = @now_ms
WHERE template_code = 'first_google70000_coin_seller_92000_100000_v1'
  AND template_version = 'v1'
  AND (JSON_EXTRACT(rule_json, '$.agency.point_ratio_percent') IS NULL
    OR JSON_EXTRACT(rule_json, '$.agency.share_base') IS NULL);

UPDATE admin_policy_template_versions
SET rule_json = JSON_MERGE_PATCH(
      CAST('{"agency":{"point_ratio_percent":0,"share_base":"charge_amount"}}' AS JSON),
      rule_json
    ),
    updated_at_ms = @now_ms
WHERE template_code = 'first_google70000_coin_seller_92000_100000_v1'
  AND template_version = 'v1'
  AND (JSON_EXTRACT(rule_json, '$.agency.point_ratio_percent') IS NULL
    OR JSON_EXTRACT(rule_json, '$.agency.share_base') IS NULL);

-- Fami 的礼物类型先决定主播实际 POINT 收益，再由平台按该收益额外给 Agency owner 20%。内层 JSON_SET
-- 先确保 host/agency 父对象存在，外层再写嵌套字段，避免 MySQL 对不存在父路径静默忽略，同时保留其他规则。
INSERT INTO admin_policy_templates (
  template_code, template_version, name, status, rule_json, description,
  created_by_admin_id, updated_by_admin_id, created_at_ms, updated_at_ms
)
SELECT
  'fami_guild_revenue_policy', 'v1', 'Fami 公会收益政策', 'active',
  JSON_SET(
    JSON_SET(
      rule_json,
      '$.host', COALESCE(JSON_EXTRACT(rule_json, '$.host'), JSON_OBJECT()),
      '$.agency', COALESCE(JSON_EXTRACT(rule_json, '$.agency'), JSON_OBJECT())
    ),
    '$.host.point_ratio_percent', 100,
    '$.host.use_gift_type_ratio', JSON_EXTRACT('true', '$'),
    '$.agency.point_ratio_percent', 20,
    '$.agency.share_base', 'host_income'
  ),
  'Fami 主播 POINT 收益政策；Agency 按主播实际 POINT 收益由平台额外发放 20%，以发布后的运行快照为准。',
  0, 0, @now_ms, @now_ms
FROM admin_policy_templates
WHERE template_code = 'first_google70000_coin_seller_92000_100000_v1' AND template_version = 'v1'
ON DUPLICATE KEY UPDATE
  name = VALUES(name),
  status = VALUES(status),
  description = VALUES(description),
  updated_at_ms = @now_ms;

INSERT INTO admin_policy_template_versions (
  template_code, template_version, status, rule_json,
  created_by_admin_id, updated_by_admin_id, created_at_ms, updated_at_ms
)
SELECT
  template_code, template_version, status, rule_json,
  created_by_admin_id, updated_by_admin_id, created_at_ms, updated_at_ms
FROM admin_policy_templates
WHERE template_code = 'fami_guild_revenue_policy' AND template_version = 'v1'
ON DUPLICATE KEY UPDATE
  status = VALUES(status),
  rule_json = VALUES(rule_json),
  updated_at_ms = @now_ms;

INSERT INTO admin_policy_instances (
  app_code, instance_code, template_code, template_version, region_scope, status,
  effective_from_ms, effective_to_ms, publish_status, created_by_admin_id, updated_by_admin_id,
  created_at_ms, updated_at_ms
) VALUES (
  'fami', 'fami_guild_revenue_policy', 'fami_guild_revenue_policy', 'v1',
  'all_active_regions', 'active', @now_ms, 0, 'draft', 0, 0, @now_ms, @now_ms
)
ON DUPLICATE KEY UPDATE
  template_code = VALUES(template_code),
  template_version = VALUES(template_version),
  region_scope = VALUES(region_scope),
  status = VALUES(status),
  effective_to_ms = VALUES(effective_to_ms),
  updated_at_ms = @now_ms;
