-- Muse 2.0.0 S4 单人版菜单收口回滚脚本。
-- 与 disable 脚本使用同一选择器，恢复被隐藏的多用户与 yudao 娱乐面菜单。
-- 幂等：重复执行只会把同一批 system_menu 置为可见/启用。

WITH RECURSIVE target_roots AS (
    SELECT id
    FROM system_menu
    WHERE deleted = FALSE
      AND (
        path IN ('/member', 'member', '/pay', 'pay', '/mp', 'mp', '/bpm', 'bpm', '/ai', 'ai')
        OR COALESCE(component, '') LIKE 'member/%'
        OR COALESCE(component, '') LIKE 'pay/%'
        OR COALESCE(component, '') LIKE 'mp/%'
        OR COALESCE(component, '') LIKE 'bpm/%'
        OR COALESCE(component, '') LIKE 'ai/%'
        OR COALESCE(permission, '') LIKE 'member:%'
        OR COALESCE(permission, '') LIKE 'pay:%'
        OR COALESCE(permission, '') LIKE 'mp:%'
        OR COALESCE(permission, '') LIKE 'bpm:%'
        OR COALESCE(permission, '') LIKE 'ai:%'
        OR (
            name IN ('用户管理', '角色管理', '租户管理', '租户套餐', '部门管理', '岗位管理')
            AND (
                COALESCE(component, '') LIKE 'system/%'
                OR COALESCE(permission, '') LIKE 'system:%'
                OR path IN ('user', 'role', 'tenant', 'tenant-package', 'dept', 'post')
            )
        )
      )
),
target_tree AS (
    SELECT id
    FROM target_roots
    UNION ALL
    SELECT child.id
    FROM system_menu child
    JOIN target_tree parent ON child.parent_id = parent.id
    WHERE child.deleted = FALSE
)
UPDATE system_menu
SET status = 0,
    visible = TRUE,
    updater = 'solo-s4-menu-restore',
    update_time = CURRENT_TIMESTAMP
WHERE id IN (SELECT id FROM target_tree)
  AND deleted = FALSE
  AND NOT (
    path = 'menu'
    OR COALESCE(component, '') = 'system/menu/index'
    OR COALESCE(permission, '') LIKE 'system:menu:%'
  );
