-- 用户规则表 v2：新增结构化触发条件和推送动作列（GORM AutoMigrate 会自动加列，此脚本用于填充现有规则的结构化数据）

ALTER TABLE user_rules ADD COLUMN IF NOT EXISTS trigger_conditions JSON DEFAULT NULL;
ALTER TABLE user_rules ADD COLUMN IF NOT EXISTS action_type VARCHAR(50) DEFAULT 'popup';
ALTER TABLE user_rules ADD COLUMN IF NOT EXISTS action_config JSON DEFAULT NULL;

-- 根据现有 trigger 字段回填结构化条件（最佳实践预设）
-- #10: 注册完成 → 填写头像
UPDATE user_rules SET trigger_conditions = '["after_login","fill_profile"]', action_type = 'popup'
WHERE sort = 10 AND trigger_conditions IS NULL;

-- #20: 完成匹配 → 补充个人资料
UPDATE user_rules SET trigger_conditions = '["after_match","fill_profile"]', action_type = 'popup'
WHERE sort = 20 AND trigger_conditions IS NULL;

-- #30: 首次浏览章节 → 绑定手机号
UPDATE user_rules SET trigger_conditions = '["view_chapter","bind_phone"]', action_type = 'popup'
WHERE sort = 30 AND trigger_conditions IS NULL;

-- #40: 付款 ¥1980 → 填写完整信息
UPDATE user_rules SET trigger_conditions = '["purchase_fullbook","fill_profile","add_wechat"]', action_type = 'popup'
WHERE sort = 40 AND trigger_conditions IS NULL;

-- #50: 加入派对房 → 填写项目介绍
UPDATE user_rules SET trigger_conditions = '["after_match"]', action_type = 'navigate'
WHERE sort = 50 AND trigger_conditions IS NULL;

-- #60: 浏览 5 个章节 → 分享推广
UPDATE user_rules SET trigger_conditions = '["browse_5_chapters","share_action"]', action_type = 'popup'
WHERE sort = 60 AND trigger_conditions IS NULL;

-- #70: 绑定微信 → 开启分销
UPDATE user_rules SET trigger_conditions = '["add_wechat","referral_bind"]', action_type = 'navigate'
WHERE sort = 70 AND trigger_conditions IS NULL;

-- #80: 收益达到
UPDATE user_rules SET trigger_conditions = '["withdraw_request"]', action_type = 'popup'
WHERE sort = 80 AND trigger_conditions IS NULL;

-- #90: 完善信息 → 进入流量池
UPDATE user_rules SET trigger_conditions = '["fill_profile","add_wechat","bind_phone"]', action_type = 'popup'
WHERE sort = 90 AND trigger_conditions IS NULL;
