-- Restore directory (M) paths broken by post_process path dedup
-- Run as part of organize pipeline AFTER subtree SQL

UPDATE tenant_sys_menu SET path = 'index'       WHERE menu_id = 32644;
UPDATE tenant_sys_menu SET path = 'qw'          WHERE menu_id = 32361;
UPDATE tenant_sys_menu SET path = 'wx'          WHERE menu_id = 32380;
UPDATE tenant_sys_menu SET path = 'crm'         WHERE menu_id = 32347;
UPDATE tenant_sys_menu SET path = 'member'      WHERE menu_id = 32357;
UPDATE tenant_sys_menu SET path = 'his'         WHERE menu_id = 32351;
UPDATE tenant_sys_menu SET path = 'store'       WHERE menu_id = 32369;
UPDATE tenant_sys_menu SET path = 'live'        WHERE menu_id = 32353;
UPDATE tenant_sys_menu SET path = 'course'      WHERE menu_id = 32345;
UPDATE tenant_sys_menu SET path = 'fastGpt'     WHERE menu_id = 32348;
UPDATE tenant_sys_menu SET path = 'lobster'     WHERE menu_id = 32355;
UPDATE tenant_sys_menu SET path = 'ad'          WHERE menu_id = 32331;
UPDATE tenant_sys_menu SET path = 'system'      WHERE menu_id = 32372;
UPDATE tenant_sys_menu SET path = 'bill'        WHERE menu_id = 32339;
UPDATE tenant_sys_menu SET path = 'calendar'    WHERE menu_id = 32341;
UPDATE tenant_sys_menu SET path = 'statistics'  WHERE menu_id = 32368;
UPDATE tenant_sys_menu SET path = 'watch'       WHERE menu_id = 32379;

-- system secondary groups
UPDATE tenant_sys_menu SET path = 'sysOrg'      WHERE menu_id = 35100;
UPDATE tenant_sys_menu SET path = 'sysPerm'     WHERE menu_id = 35101;
UPDATE tenant_sys_menu SET path = 'sysVoice'    WHERE menu_id = 35102;
UPDATE tenant_sys_menu SET path = 'sysLog'      WHERE menu_id = 35105;
UPDATE tenant_sys_menu SET path = 'sysConfig'   WHERE menu_id = 35106;

-- qw secondary groups
UPDATE tenant_sys_menu SET path = 'qwMsg'       WHERE menu_id = 35001;
UPDATE tenant_sys_menu SET path = 'qwCustomer'  WHERE menu_id = 35002;
UPDATE tenant_sys_menu SET path = 'qwGroup'     WHERE menu_id = 35003;
UPDATE tenant_sys_menu SET path = 'qwMoments'   WHERE menu_id = 35004;
UPDATE tenant_sys_menu SET path = 'qwDrainage'  WHERE menu_id = 35005;
UPDATE tenant_sys_menu SET path = 'qwTag'       WHERE menu_id = 35006;
UPDATE tenant_sys_menu SET path = 'qwSetting'   WHERE menu_id = 35007;

-- store secondary groups
UPDATE tenant_sys_menu SET path = 'storeOrder'   WHERE menu_id = 35040;
UPDATE tenant_sys_menu SET path = 'storeProduct' WHERE menu_id = 35041;
UPDATE tenant_sys_menu SET path = 'storeOps'     WHERE menu_id = 35042;

-- wx secondary groups
UPDATE tenant_sys_menu SET path = 'wxAccount'  WHERE menu_id = 35010;
UPDATE tenant_sys_menu SET path = 'wxDialog'   WHERE menu_id = 35011;
UPDATE tenant_sys_menu SET path = 'wxUser'     WHERE menu_id = 35012;
UPDATE tenant_sys_menu SET path = 'wxUserGroup' WHERE menu_id = 35013;

-- crm / live / course / ai / bill / ad secondary groups
UPDATE tenant_sys_menu SET path = 'crmCustomer'  WHERE menu_id = 35020;
UPDATE tenant_sys_menu SET path = 'liveOps'      WHERE menu_id = 35050;
UPDATE tenant_sys_menu SET path = 'courseContent' WHERE menu_id = 35060;
UPDATE tenant_sys_menu SET path = 'aiChat'       WHERE menu_id = 35070;
UPDATE tenant_sys_menu SET path = 'lobsterFlow'  WHERE menu_id = 35080;
UPDATE tenant_sys_menu SET path = 'adOps'        WHERE menu_id = 35090;
UPDATE tenant_sys_menu SET path = 'billRecharge' WHERE menu_id = 35111;
UPDATE tenant_sys_menu SET path = 'billDeduct'   WHERE menu_id = 35112;
UPDATE tenant_sys_menu SET path = 'billProfit'   WHERE menu_id = 35113;
UPDATE tenant_sys_menu SET path = 'billMoneyLog' WHERE menu_id = 35114;

-- hide company root (by menu_id, path may have been corrupted)
UPDATE tenant_sys_menu SET visible = '1', status = '0' WHERE menu_id = 32344;

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 hidden top-level modules under other (35300)
UPDATE tenant_sys_menu SET parent_id = 35300
WHERE parent_id = 0 AND visible = '1' AND menu_type = 'M'
  AND menu_id NOT IN (32333, 35300);

-- hide index dashboard template entry (not a tenant top-nav module)
UPDATE tenant_sys_menu SET visible = '1' WHERE menu_id = 32644;

-- archive hidden platform/legacy menus under ???? (35300)
UPDATE tenant_sys_menu SET parent_id = 35300
WHERE parent_id = 32372 AND visible = '1' AND menu_type <> 'F';

UPDATE tenant_sys_menu SET parent_id = 35300
WHERE parent_id = 32361 AND visible = '1' AND menu_type <> 'F'
  AND menu_id NOT IN (35001, 35002, 35003, 35004, 35005, 35006, 35007);

UPDATE tenant_sys_menu SET parent_id = 35300
WHERE parent_id IN (32347, 32348, 32345, 32353, 32369, 32380, 32339, 32331, 32355, 32368, 32379, 32351, 32357, 32341)
  AND visible = '1' AND menu_type <> 'F'
  AND menu_id NOT IN (
    35001,35002,35003,35004,35005,35006,35007,
    35010,35011,35012,35013,
    35020,35021,35023,
    35040,35041,35042,
    35050,35051,35052,35053,
    35060,35061,35062,35063,
    35070,35071,35072,35073,35074,35075,
    35080,35090,
    35100,35101,35102,35105,35106,
    35111,35112,35113,35114
  );
