-- 习题与作业模块初始化数据占位
-- 按需补充题库、题目、试卷、作业模板等种子数据

-- 批改与反馈模块初始化数据占位
-- 按需补充错因标签、统一答案批改结果、复习计划策略等种子数据

-- 艾宾浩斯复习阶段默认策略（E1-E6：1/2/4/7/15/30天）
WITH stage_defaults(stage_code, stage_order, interval_days, retry_interval_days, pass_threshold, max_retry_count) AS (
    VALUES
        ('E1', 1, 1, 1, 60.00, 3),
        ('E2', 2, 2, 1, 65.00, 3),
        ('E3', 3, 4, 1, 70.00, 2),
        ('E4', 4, 7, 2, 75.00, 2),
        ('E5', 5, 15, 2, 80.00, 2),
        ('E6', 6, 30, 3, 85.00, 1)
)
INSERT INTO question.gd_review_stage_policy (
    policy_id,
    stage_code,
    stage_order,
    interval_days,
    retry_interval_days,
    pass_threshold,
    max_retry_count,
    enabled,
    tenant_id,
    created_at,
    updated_at
)
SELECT
    'RSP_' || SUBSTRING(MD5(t.tenant_id || ':' || s.stage_code) FROM 1 FOR 24) AS policy_id,
    s.stage_code,
    s.stage_order,
    s.interval_days,
    s.retry_interval_days,
    s.pass_threshold,
    s.max_retry_count,
    TRUE,
    t.tenant_id,
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
FROM upms.tb_sys_tenant t
CROSS JOIN stage_defaults s
ON CONFLICT (tenant_id, stage_code)
DO UPDATE SET
    stage_order = EXCLUDED.stage_order,
    interval_days = EXCLUDED.interval_days,
    retry_interval_days = EXCLUDED.retry_interval_days,
    pass_threshold = EXCLUDED.pass_threshold,
    max_retry_count = EXCLUDED.max_retry_count,
    enabled = EXCLUDED.enabled,
    updated_at = CURRENT_TIMESTAMP;
