-- =============================================================================
-- 迁移：移除旧版指数（RECALL / INTIMACY）及 ML last-touch 备用参数
-- 日期: 2026-02-13
-- AI_CHANGELOG [2026-02-13] 新建迁移：DROP recall/intimacy 表，DELETE 旧版参数/分位点/调度任务
-- 原因: 旧版 RecallIndexTask / IntimacyIndexTask 已被 WBI+NCI / RelationIndexTask 替代
--       ML 仅使用人工台账，不再需要 last-touch 备用路径
-- =============================================================================

BEGIN;

-- 1. 删除旧版指数参数
DELETE FROM billiards_dws.cfg_index_parameters
WHERE index_type IN ('RECALL', 'INTIMACY');

-- 2. 删除 ML 已废弃参数
DELETE FROM billiards_dws.cfg_index_parameters
WHERE index_type = 'ML'
  AND param_name IN ('source_mode', 'recharge_attribute_hours');

-- 3. 删除旧版指数分位点历史
DELETE FROM billiards_dws.cfg_index_percentile_history
WHERE index_type IN ('RECALL', 'INTIMACY');

-- 4. 删除调度器中的旧版任务
DELETE FROM etl_admin.etl_task
WHERE task_code IN ('DWS_RECALL_INDEX', 'DWS_INTIMACY_INDEX');

-- 5. 删除旧版 recall 表（数据已由 WBI+NCI 替代）
DROP TABLE IF EXISTS billiards_dws.dws_member_recall_index CASCADE;

-- 6. 删除旧版 intimacy 表（数据已由 RelationIndexTask 替代）
DROP TABLE IF EXISTS billiards_dws.dws_member_assistant_intimacy CASCADE;

COMMIT;

-- =============================================================================
-- 验证
-- =============================================================================
DO $
DECLARE
    recall_params INTEGER;
    intimacy_params INTEGER;
    ml_legacy INTEGER;
    recall_table BOOLEAN;
    intimacy_table BOOLEAN;
BEGIN
    SELECT COUNT(*) INTO recall_params
    FROM billiards_dws.cfg_index_parameters WHERE index_type = 'RECALL';

    SELECT COUNT(*) INTO intimacy_params
    FROM billiards_dws.cfg_index_parameters WHERE index_type = 'INTIMACY';

    SELECT COUNT(*) INTO ml_legacy
    FROM billiards_dws.cfg_index_parameters
    WHERE index_type = 'ML' AND param_name IN ('source_mode', 'recharge_attribute_hours');

    SELECT EXISTS(
        SELECT 1 FROM information_schema.tables
        WHERE table_schema = 'billiards_dws' AND table_name = 'dws_member_recall_index'
    ) INTO recall_table;

    SELECT EXISTS(
        SELECT 1 FROM information_schema.tables
        WHERE table_schema = 'billiards_dws' AND table_name = 'dws_member_assistant_intimacy'
    ) INTO intimacy_table;

    RAISE NOTICE 'RECALL 参数残留: % (应为 0)', recall_params;
    RAISE NOTICE 'INTIMACY 参数残留: % (应为 0)', intimacy_params;
    RAISE NOTICE 'ML 废弃参数残留: % (应为 0)', ml_legacy;
    RAISE NOTICE 'recall 表存在: % (应为 false)', recall_table;
    RAISE NOTICE 'intimacy 表存在: % (应为 false)', intimacy_table;
END $;
