-- Create visible top-level "????" parent and move archived hidden menus under it.
-- Replaces hidden archive root 32333 as the grouping parent for maintenance UI.

-- Ensure parent exists (idempotent; menu_name via Python post_process if re-run)
INSERT INTO tenant_sys_menu
(menu_id, menu_name, parent_id, order_num, path, component, query,
 is_frame, is_cache, menu_type, visible, status, perms, icon,
 create_by, create_time, remark)
SELECT 35300, '????', 0, 17, 'other', NULL, NULL,
       1, 0, 'M', '0', '0', NULL, 'more',
       'admin', NOW(), '[organize:other-parent]'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM tenant_sys_menu WHERE menu_id = 35300);

UPDATE tenant_sys_menu
SET menu_name = '????', parent_id = 0, order_num = 17, path = 'other',
    menu_type = 'M', visible = '0', status = '0', icon = 'more'
WHERE menu_id = 35300;

-- Move former platform archive children -> ????
UPDATE tenant_sys_menu SET parent_id = 35300 WHERE parent_id = 32333;

-- Keep legacy platform root hidden and empty (historical menu_id)
UPDATE tenant_sys_menu
SET parent_id = 0, visible = '1', status = '0', order_num = 99
WHERE menu_id = 32333;

-- Show entire subtree under  (maintenance bucket; was hidden before grouping)
UPDATE tenant_sys_menu
SET visible = '0', status = '0'
WHERE menu_id IN (
  WITH RECURSIVE other_tree AS (
    SELECT menu_id FROM tenant_sys_menu WHERE parent_id = 35300
    UNION ALL
    SELECT m.menu_id
    FROM tenant_sys_menu m
    INNER JOIN other_tree t ON m.parent_id = t.menu_id
  )
  SELECT menu_id FROM other_tree
);
