-- 2026-04-16
-- Portais e blocos TIIX no primeiro nivel do menu admin
-- Objetivos:
-- 1) manter os portais como itens reais de primeiro nivel
-- 2) posicionar TIIX HEALTH e TIIXSTORE logo apos os portais
-- 3) deixar os hubs administrativos na sequencia
-- 4) preservar slugs e ACL existentes

START TRANSACTION;

SET @menu_id = 35;

UPDATE wpjy_menus_items
   SET parent = 0,
       sort = CASE id
         WHEN 531 THEN 0
         WHEN 542 THEN 1
         WHEN 714 THEN 2
         WHEN 387 THEN 3
         WHEN 504 THEN 4
         WHEN 740 THEN 5
         WHEN 633 THEN 6
         WHEN 599 THEN 10
         WHEN 670 THEN 11
         WHEN 551 THEN 12
         WHEN 697 THEN 13
         WHEN 552 THEN 14
         ELSE sort
       END
 WHERE menu = @menu_id
   AND id IN (531, 542, 714, 387, 504, 740, 633, 599, 670, 551, 697, 552);

UPDATE wpjy_menus_items
   SET parent = 599,
       sort = CASE id
         WHEN 618 THEN 1
         WHEN 619 THEN 2
         WHEN 558 THEN 3
         ELSE sort
       END
 WHERE menu = @menu_id
   AND id IN (618, 619, 558);

UPDATE wpjy_menus_items
   SET parent = 670,
       sort = CASE id
         WHEN 555 THEN 1
         WHEN 566 THEN 2
         ELSE sort
       END
 WHERE menu = @menu_id
   AND id IN (555, 566);

UPDATE wpjy_menus_items
   SET parent = 551,
       sort = CASE id
         WHEN 553 THEN 1
         WHEN 554 THEN 2
         ELSE sort
       END
 WHERE menu = @menu_id
   AND id IN (553, 554);

UPDATE wpjy_menus_items
   SET parent = 697,
       sort = CASE id
         WHEN 556 THEN 1
         WHEN 669 THEN 2
         WHEN 696 THEN 3
         ELSE sort
       END
 WHERE menu = @menu_id
   AND id IN (556, 669, 696);

UPDATE wpjy_menus_items
   SET parent = 552,
       sort = CASE id
         WHEN 557 THEN 1
         WHEN 559 THEN 2
         WHEN 560 THEN 3
         ELSE sort
       END
 WHERE menu = @menu_id
   AND id IN (557, 559, 560);

DELETE FROM wpjy_menus_items
 WHERE menu = @menu_id
   AND slug = 'menu.adm.hub.portais'
   AND NOT EXISTS (
     SELECT 1
       FROM (
         SELECT parent
           FROM wpjy_menus_items
       ) filhos
      WHERE filhos.parent = wpjy_menus_items.id
   );

COMMIT;

SELECT id, label, parent, sort, slug
  FROM wpjy_menus_items
 WHERE menu = @menu_id
   AND parent = 0
 ORDER BY sort, id;
