-- 2026-04-16
-- Reorganizacao inteligente do menu administrativo em hubs e subniveis
-- Objetivos:
-- 1) encurtar a mini nav do admin sem trocar os slugs dos links finais
-- 2) concentrar as raizes administrativas em hubs mais legiveis
-- 3) quebrar grupos muito longos em secoes intermediarias
-- 4) preservar compatibilidade com ACL, slugs e rotas existentes

START TRANSACTION;

SET @menu_id = 35;

-- ---------------------------------------------------------------------------
-- 1) Hubs de primeiro nivel do admin
-- ---------------------------------------------------------------------------

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Trabalho',
  '#',
  NULL,
  0,
  10,
  @menu_id,
  '_self',
  'lucide:briefcase',
  'menu.adm.hub.trabalho',
  'adm_hub_trabalho',
  NULL
WHERE NOT EXISTS (
  SELECT 1
    FROM wpjy_menus_items
   WHERE menu = @menu_id
     AND slug = 'menu.adm.hub.trabalho'
);

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Operacao',
  '#',
  NULL,
  0,
  11,
  @menu_id,
  '_self',
  'solar:settings-linear',
  'menu.adm.hub.operacao',
  'adm_hub_operacao',
  NULL
WHERE NOT EXISTS (
  SELECT 1
    FROM wpjy_menus_items
   WHERE menu = @menu_id
     AND slug = 'menu.adm.hub.operacao'
);

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Negocio',
  '#',
  NULL,
  0,
  12,
  @menu_id,
  '_self',
  'lucide:briefcase-business',
  'menu.adm.hub.negocio',
  'adm_hub_negocio',
  NULL
WHERE NOT EXISTS (
  SELECT 1
    FROM wpjy_menus_items
   WHERE menu = @menu_id
     AND slug = 'menu.adm.hub.negocio'
);

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Inteligencia',
  '#',
  NULL,
  0,
  13,
  @menu_id,
  '_self',
  'solar:chart-line-duotone',
  'menu.adm.hub.inteligencia',
  'adm_hub_inteligencia',
  NULL
WHERE NOT EXISTS (
  SELECT 1
    FROM wpjy_menus_items
   WHERE menu = @menu_id
     AND slug = 'menu.adm.hub.inteligencia'
);

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Plataforma',
  '#',
  NULL,
  0,
  14,
  @menu_id,
  '_self',
  'lucide:settings',
  'menu.adm.hub.plataforma',
  'adm_hub_plataforma',
  NULL
WHERE NOT EXISTS (
  SELECT 1
    FROM wpjy_menus_items
   WHERE menu = @menu_id
     AND slug = 'menu.adm.hub.plataforma'
);

SET @hub_trabalho = (
  SELECT id
    FROM wpjy_menus_items
   WHERE menu = @menu_id
     AND slug = 'menu.adm.hub.trabalho'
   ORDER BY id ASC
   LIMIT 1
);

SET @hub_operacao = (
  SELECT id
    FROM wpjy_menus_items
   WHERE menu = @menu_id
     AND slug = 'menu.adm.hub.operacao'
   ORDER BY id ASC
   LIMIT 1
);

SET @hub_negocio = (
  SELECT id
    FROM wpjy_menus_items
   WHERE menu = @menu_id
     AND slug = 'menu.adm.hub.negocio'
   ORDER BY id ASC
   LIMIT 1
);

SET @hub_inteligencia = (
  SELECT id
    FROM wpjy_menus_items
   WHERE menu = @menu_id
     AND slug = 'menu.adm.hub.inteligencia'
   ORDER BY id ASC
   LIMIT 1
);

SET @hub_plataforma = (
  SELECT id
    FROM wpjy_menus_items
   WHERE menu = @menu_id
     AND slug = 'menu.adm.hub.plataforma'
   ORDER BY id ASC
   LIMIT 1
);

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

UPDATE wpjy_menus_items
   SET parent = @hub_operacao,
       sort = CASE id
         WHEN 670 THEN 0
         WHEN 555 THEN 1
         WHEN 566 THEN 2
         WHEN 740 THEN 3
         ELSE sort
       END
 WHERE @hub_operacao IS NOT NULL
   AND menu = @menu_id
   AND id IN (670, 555, 566, 740);

UPDATE wpjy_menus_items
   SET parent = @hub_negocio,
       sort = CASE id
         WHEN 551 THEN 0
         WHEN 553 THEN 1
         WHEN 554 THEN 2
         WHEN 633 THEN 3
         ELSE sort
       END
 WHERE @hub_negocio IS NOT NULL
   AND menu = @menu_id
   AND id IN (551, 553, 554, 633);

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

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

-- ---------------------------------------------------------------------------
-- 2) Secoes internas de Permissoes & Capacidades (552)
-- ---------------------------------------------------------------------------

SET @root_552 = 552;

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Governanca de acesso',
  '#',
  'adm',
  @root_552,
  0,
  @menu_id,
  '_self',
  'lucide:key-round',
  'menu.adm.acessos.governanca',
  'adm_acessos_governanca',
  NULL
WHERE @root_552 IS NOT NULL
  AND NOT EXISTS (
    SELECT 1
      FROM wpjy_menus_items
     WHERE menu = @menu_id
       AND slug = 'menu.adm.acessos.governanca'
  );

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Diagnostico e auditoria',
  '#',
  'adm',
  @root_552,
  1,
  @menu_id,
  '_self',
  'lucide:shield-check',
  'menu.adm.acessos.diagnostico',
  'adm_acessos_diagnostico',
  NULL
WHERE @root_552 IS NOT NULL
  AND NOT EXISTS (
    SELECT 1
      FROM wpjy_menus_items
     WHERE menu = @menu_id
       AND slug = 'menu.adm.acessos.diagnostico'
  );

SET @sec_552_governanca = (
  SELECT id FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.acessos.governanca' LIMIT 1
);

SET @sec_552_diagnostico = (
  SELECT id FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.acessos.diagnostico' LIMIT 1
);

UPDATE wpjy_menus_items
   SET parent = @sec_552_governanca,
       sort = CASE id
         WHEN 598 THEN 0
         WHEN 497 THEN 1
         WHEN 438 THEN 2
         WHEN 493 THEN 3
         WHEN 492 THEN 4
         WHEN 363 THEN 5
         WHEN 473 THEN 6
         WHEN 362 THEN 7
         WHEN 360 THEN 8
         ELSE sort
       END
 WHERE @sec_552_governanca IS NOT NULL
   AND menu = @menu_id
   AND id IN (598, 497, 438, 493, 492, 363, 473, 362, 360);

UPDATE wpjy_menus_items
   SET parent = @sec_552_diagnostico,
       sort = CASE id
         WHEN 474 THEN 0
         WHEN 475 THEN 1
         WHEN 476 THEN 2
         WHEN 477 THEN 3
         WHEN 478 THEN 4
         WHEN 479 THEN 5
         WHEN 480 THEN 6
         WHEN 481 THEN 7
         WHEN 482 THEN 8
         WHEN 483 THEN 9
         WHEN 484 THEN 10
         WHEN 485 THEN 11
         WHEN 486 THEN 12
         ELSE sort
       END
 WHERE @sec_552_diagnostico IS NOT NULL
   AND menu = @menu_id
   AND id IN (474, 475, 476, 477, 478, 479, 480, 481, 482, 483, 484, 485, 486);

-- ---------------------------------------------------------------------------
-- 3) Secoes internas de Financeiro (553)
-- ---------------------------------------------------------------------------

SET @root_553 = 553;

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Cobranca e receita',
  '#',
  'adm',
  @root_553,
  0,
  @menu_id,
  '_self',
  'solar:wallet-money-line-duotone',
  'menu.adm.financeiro.cobranca',
  'adm_financeiro_cobranca',
  NULL
WHERE @root_553 IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.financeiro.cobranca'
  );

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Operacoes financeiras',
  '#',
  'adm',
  @root_553,
  1,
  @menu_id,
  '_self',
  'lucide:receipt',
  'menu.adm.financeiro.operacoes',
  'adm_financeiro_operacoes',
  NULL
WHERE @root_553 IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.financeiro.operacoes'
  );

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Gateways e conciliacao',
  '#',
  'adm',
  @root_553,
  2,
  @menu_id,
  '_self',
  'solar:card-transfer-line-duotone',
  'menu.adm.financeiro.gateways',
  'adm_financeiro_gateways',
  NULL
WHERE @root_553 IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.financeiro.gateways'
  );

SET @sec_553_cobranca = (
  SELECT id FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.financeiro.cobranca' LIMIT 1
);

SET @sec_553_operacoes = (
  SELECT id FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.financeiro.operacoes' LIMIT 1
);

SET @sec_553_gateways = (
  SELECT id FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.financeiro.gateways' LIMIT 1
);

UPDATE wpjy_menus_items
   SET parent = @sec_553_cobranca,
       sort = CASE id
         WHEN 340 THEN 0
         WHEN 341 THEN 1
         WHEN 434 THEN 2
         WHEN 440 THEN 3
         WHEN 455 THEN 4
         WHEN 469 THEN 5
         WHEN 495 THEN 6
         ELSE sort
       END
 WHERE @sec_553_cobranca IS NOT NULL
   AND menu = @menu_id
   AND id IN (340, 341, 434, 440, 455, 469, 495);

UPDATE wpjy_menus_items
   SET parent = @sec_553_operacoes,
       sort = CASE id
         WHEN 382 THEN 0
         WHEN 343 THEN 1
         WHEN 358 THEN 2
         WHEN 471 THEN 3
         WHEN 472 THEN 4
         ELSE sort
       END
 WHERE @sec_553_operacoes IS NOT NULL
   AND menu = @menu_id
   AND id IN (382, 343, 358, 471, 472);

UPDATE wpjy_menus_items
   SET parent = @sec_553_gateways,
       sort = CASE id
         WHEN 342 THEN 0
         WHEN 435 THEN 1
         WHEN 436 THEN 2
         WHEN 367 THEN 3
         WHEN 491 THEN 4
         ELSE sort
       END
 WHERE @sec_553_gateways IS NOT NULL
   AND menu = @menu_id
   AND id IN (342, 435, 436, 367, 491);

-- ---------------------------------------------------------------------------
-- 4) Secoes internas de Relatorios & KPIs legados (556)
-- ---------------------------------------------------------------------------

SET @root_556 = 556;

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Comercial',
  '#',
  'adm',
  @root_556,
  0,
  @menu_id,
  '_self',
  'solar:chart-square-line-duotone',
  'menu.adm.relatorios_legacy.comercial',
  'adm_relatorios_legacy_comercial',
  NULL
WHERE @root_556 IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.relatorios_legacy.comercial'
  );

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Carteira e financeiro',
  '#',
  'adm',
  @root_556,
  1,
  @menu_id,
  '_self',
  'solar:wallet-money-line-duotone',
  'menu.adm.relatorios_legacy.carteira',
  'adm_relatorios_legacy_carteira',
  NULL
WHERE @root_556 IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.relatorios_legacy.carteira'
  );

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Paineis e indicadores',
  '#',
  'adm',
  @root_556,
  2,
  @menu_id,
  '_self',
  'solar:chart-line-duotone',
  'menu.adm.relatorios_legacy.kpis',
  'adm_relatorios_legacy_kpis',
  NULL
WHERE @root_556 IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.relatorios_legacy.kpis'
  );

SET @sec_556_comercial = (
  SELECT id FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.relatorios_legacy.comercial' LIMIT 1
);

SET @sec_556_carteira = (
  SELECT id FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.relatorios_legacy.carteira' LIMIT 1
);

SET @sec_556_kpis = (
  SELECT id FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.relatorios_legacy.kpis' LIMIT 1
);

UPDATE wpjy_menus_items
   SET parent = @sec_556_comercial,
       sort = CASE id
         WHEN 326 THEN 0
         WHEN 327 THEN 1
         WHEN 330 THEN 2
         WHEN 335 THEN 3
         WHEN 336 THEN 4
         WHEN 408 THEN 5
         ELSE sort
       END
 WHERE @sec_556_comercial IS NOT NULL
   AND menu = @menu_id
   AND id IN (326, 327, 330, 335, 336, 408);

UPDATE wpjy_menus_items
   SET parent = @sec_556_carteira,
       sort = CASE id
         WHEN 328 THEN 0
         WHEN 329 THEN 1
         WHEN 331 THEN 2
         WHEN 332 THEN 3
         WHEN 333 THEN 4
         WHEN 334 THEN 5
         ELSE sort
       END
 WHERE @sec_556_carteira IS NOT NULL
   AND menu = @menu_id
   AND id IN (328, 329, 331, 332, 333, 334);

UPDATE wpjy_menus_items
   SET parent = @sec_556_kpis,
       sort = CASE id
         WHEN 431 THEN 0
         WHEN 442 THEN 1
         ELSE sort
       END
 WHERE @sec_556_kpis IS NOT NULL
   AND menu = @menu_id
   AND id IN (431, 442);

-- ---------------------------------------------------------------------------
-- 5) Secoes internas de Configuracoes (559)
-- ---------------------------------------------------------------------------

SET @root_559 = 559;

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Cadastros base',
  '#',
  'adm',
  @root_559,
  0,
  @menu_id,
  '_self',
  'lucide:settings',
  'menu.adm.config.base',
  'adm_config_base',
  NULL
WHERE @root_559 IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.config.base'
  );

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Documentos e compliance',
  '#',
  'adm',
  @root_559,
  1,
  @menu_id,
  '_self',
  'lucide:files',
  'menu.adm.config.documentos',
  'adm_config_documentos',
  NULL
WHERE @root_559 IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.config.documentos'
  );

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Conteudo e comunicacao',
  '#',
  'adm',
  @root_559,
  2,
  @menu_id,
  '_self',
  'lucide:book',
  'menu.adm.config.conteudo',
  'adm_config_conteudo',
  NULL
WHERE @root_559 IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.config.conteudo'
  );

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Leads e disponibilidade',
  '#',
  'adm',
  @root_559,
  3,
  @menu_id,
  '_self',
  'solar:tag-horizontal-line-duotone',
  'menu.adm.config.leads',
  'adm_config_leads',
  NULL
WHERE @root_559 IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.config.leads'
  );

SET @sec_559_base = (
  SELECT id FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.config.base' LIMIT 1
);

SET @sec_559_documentos = (
  SELECT id FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.config.documentos' LIMIT 1
);

SET @sec_559_conteudo = (
  SELECT id FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.config.conteudo' LIMIT 1
);

SET @sec_559_leads = (
  SELECT id FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.config.leads' LIMIT 1
);

UPDATE wpjy_menus_items
   SET parent = @sec_559_base,
       sort = CASE id
         WHEN 428 THEN 0
         WHEN 350 THEN 1
         WHEN 301 THEN 2
         WHEN 353 THEN 3
         WHEN 354 THEN 4
         WHEN 623 THEN 5
         ELSE sort
       END
 WHERE @sec_559_base IS NOT NULL
   AND menu = @menu_id
   AND id IN (428, 350, 301, 353, 354, 623);

UPDATE wpjy_menus_items
   SET parent = @sec_559_documentos,
       sort = CASE id
         WHEN 375 THEN 0
         WHEN 383 THEN 1
         WHEN 384 THEN 2
         WHEN 385 THEN 3
         WHEN 655 THEN 4
         ELSE sort
       END
 WHERE @sec_559_documentos IS NOT NULL
   AND menu = @menu_id
   AND id IN (375, 383, 384, 385, 655);

UPDATE wpjy_menus_items
   SET parent = @sec_559_conteudo,
       sort = CASE id
         WHEN 351 THEN 0
         WHEN 374 THEN 1
         WHEN 429 THEN 2
         WHEN 430 THEN 3
         WHEN 653 THEN 4
         ELSE sort
       END
 WHERE @sec_559_conteudo IS NOT NULL
   AND menu = @menu_id
   AND id IN (351, 374, 429, 430, 653);

UPDATE wpjy_menus_items
   SET parent = @sec_559_leads,
       sort = CASE id
         WHEN 572 THEN 0
         WHEN 573 THEN 1
         WHEN 574 THEN 2
         WHEN 687 THEN 3
         ELSE sort
       END
 WHERE @sec_559_leads IS NOT NULL
   AND menu = @menu_id
   AND id IN (572, 573, 574, 687);

-- ---------------------------------------------------------------------------
-- 6) Secoes internas de Operacao Tecnica (560)
-- ---------------------------------------------------------------------------

SET @root_560 = 560;

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Estrutura tecnica',
  '#',
  'adm',
  @root_560,
  0,
  @menu_id,
  '_self',
  'lucide:shield-check',
  'menu.adm.tecnico.estrutura',
  'adm_tecnico_estrutura',
  NULL
WHERE @root_560 IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.tecnico.estrutura'
  );

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Engenharia e entrega',
  '#',
  'adm',
  @root_560,
  1,
  @menu_id,
  '_self',
  'lucide:github',
  'menu.adm.tecnico.engenharia',
  'adm_tecnico_engenharia',
  NULL
WHERE @root_560 IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.tecnico.engenharia'
  );

SET @sec_560_estrutura = (
  SELECT id FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.tecnico.estrutura' LIMIT 1
);

SET @sec_560_engenharia = (
  SELECT id FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.tecnico.engenharia' LIMIT 1
);

UPDATE wpjy_menus_items
   SET parent = @sec_560_estrutura,
       sort = CASE id
         WHEN 356 THEN 0
         WHEN 444 THEN 1
         WHEN 450 THEN 2
         WHEN 449 THEN 3
         ELSE sort
       END
 WHERE @sec_560_estrutura IS NOT NULL
   AND menu = @menu_id
   AND id IN (356, 444, 450, 449);

UPDATE wpjy_menus_items
   SET parent = @sec_560_engenharia,
       sort = CASE id
         WHEN 576 THEN 0
         WHEN 575 THEN 1
         WHEN 577 THEN 2
         WHEN 578 THEN 3
         WHEN 580 THEN 4
         WHEN 581 THEN 5
         WHEN 586 THEN 6
         WHEN 596 THEN 7
         WHEN 638 THEN 8
         WHEN 639 THEN 9
         WHEN 640 THEN 10
         WHEN 650 THEN 11
         WHEN 651 THEN 12
         WHEN 661 THEN 13
         ELSE sort
       END
 WHERE @sec_560_engenharia IS NOT NULL
   AND menu = @menu_id
   AND id IN (576, 575, 577, 578, 580, 581, 586, 596, 638, 639, 640, 650, 651, 661);

-- ---------------------------------------------------------------------------
-- 7) Secoes internas de Parceiros (566)
-- ---------------------------------------------------------------------------

SET @root_566 = 566;

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Base de parceiros',
  '#',
  'adm',
  @root_566,
  0,
  @menu_id,
  '_self',
  'lucide:handshake',
  'menu.adm.parceiros.base',
  'adm_parceiros_base',
  NULL
WHERE @root_566 IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.parceiros.base'
  );

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Catalogos profissionais',
  '#',
  'adm',
  @root_566,
  1,
  @menu_id,
  '_self',
  'lucide:stethoscope',
  'menu.adm.parceiros.catalogos',
  'adm_parceiros_catalogos',
  NULL
WHERE @root_566 IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.parceiros.catalogos'
  );

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Relacionamentos e operacao',
  '#',
  'adm',
  @root_566,
  2,
  @menu_id,
  '_self',
  'solar:user-check-rounded-line-duotone',
  'menu.adm.parceiros.relacionamentos',
  'adm_parceiros_relacionamentos',
  NULL
WHERE @root_566 IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.parceiros.relacionamentos'
  );

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Leads',
  '#',
  'adm',
  @root_566,
  3,
  @menu_id,
  '_self',
  'solar:target-arrow-line-duotone',
  'menu.adm.parceiros.leads_hub',
  'adm_parceiros_leads_hub',
  NULL
WHERE @root_566 IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.parceiros.leads_hub'
  );

SET @sec_566_base = (
  SELECT id FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.parceiros.base' LIMIT 1
);

SET @sec_566_catalogos = (
  SELECT id FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.parceiros.catalogos' LIMIT 1
);

SET @sec_566_relacionamentos = (
  SELECT id FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.parceiros.relacionamentos' LIMIT 1
);

SET @sec_566_leads = (
  SELECT id FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.parceiros.leads_hub' LIMIT 1
);

UPDATE wpjy_menus_items
   SET parent = @sec_566_base,
       sort = CASE id
         WHEN 302 THEN 0
         WHEN 467 THEN 1
         WHEN 503 THEN 2
         WHEN 656 THEN 3
         WHEN 657 THEN 4
         WHEN 458 THEN 5
         WHEN 462 THEN 6
         WHEN 626 THEN 7
         ELSE sort
       END
 WHERE @sec_566_base IS NOT NULL
   AND menu = @menu_id
   AND id IN (302, 467, 503, 656, 657, 458, 462, 626);

UPDATE wpjy_menus_items
   SET parent = @sec_566_catalogos,
       sort = CASE id
         WHEN 457 THEN 0
         WHEN 456 THEN 1
         WHEN 459 THEN 2
         WHEN 464 THEN 3
         WHEN 373 THEN 4
         WHEN 466 THEN 5
         WHEN 386 THEN 6
         ELSE sort
       END
 WHERE @sec_566_catalogos IS NOT NULL
   AND menu = @menu_id
   AND id IN (457, 456, 459, 464, 373, 466, 386);

UPDATE wpjy_menus_items
   SET parent = @sec_566_relacionamentos,
       sort = CASE id
         WHEN 659 THEN 0
         WHEN 660 THEN 1
         WHEN 460 THEN 2
         WHEN 488 THEN 3
         WHEN 489 THEN 4
         WHEN 490 THEN 5
         WHEN 463 THEN 6
         WHEN 461 THEN 7
         WHEN 465 THEN 8
         ELSE sort
       END
 WHERE @sec_566_relacionamentos IS NOT NULL
   AND menu = @menu_id
   AND id IN (659, 660, 460, 488, 489, 490, 463, 461, 465);

UPDATE wpjy_menus_items
   SET parent = @sec_566_leads,
       sort = CASE id
         WHEN 372 THEN 0
         WHEN 369 THEN 1
         WHEN 370 THEN 2
         WHEN 371 THEN 3
         ELSE sort
       END
 WHERE @sec_566_leads IS NOT NULL
   AND menu = @menu_id
   AND id IN (372, 369, 370, 371);

-- ---------------------------------------------------------------------------
-- 8) Secoes internas de Pessoas & Operacao (670)
-- ---------------------------------------------------------------------------

SET @root_670 = 670;

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Pessoas',
  '#',
  'adm',
  @root_670,
  0,
  @menu_id,
  '_self',
  'lucide:users-round',
  'menu.adm.pessoas_operacao.pessoas',
  'adm_pessoas_operacao_pessoas',
  NULL
WHERE @root_670 IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.pessoas_operacao.pessoas'
  );

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Times e estrutura',
  '#',
  'adm',
  @root_670,
  1,
  @menu_id,
  '_self',
  'solar:users-group-rounded-line-duotone',
  'menu.adm.pessoas_operacao.estrutura',
  'adm_pessoas_operacao_estrutura',
  NULL
WHERE @root_670 IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.pessoas_operacao.estrutura'
  );

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Relacionamento e CRM',
  '#',
  'adm',
  @root_670,
  2,
  @menu_id,
  '_self',
  'solar:target-arrow-line-duotone',
  'menu.adm.pessoas_operacao.crm',
  'adm_pessoas_operacao_crm',
  NULL
WHERE @root_670 IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.pessoas_operacao.crm'
  );

SET @sec_670_pessoas = (
  SELECT id FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.pessoas_operacao.pessoas' LIMIT 1
);

SET @sec_670_estrutura = (
  SELECT id FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.pessoas_operacao.estrutura' LIMIT 1
);

SET @sec_670_crm = (
  SELECT id FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.pessoas_operacao.crm' LIMIT 1
);

UPDATE wpjy_menus_items
   SET parent = @sec_670_pessoas,
       sort = CASE id
         WHEN 498 THEN 0
         WHEN 627 THEN 1
         WHEN 625 THEN 2
         WHEN 624 THEN 3
         ELSE sort
       END
 WHERE @sec_670_pessoas IS NOT NULL
   AND menu = @menu_id
   AND id IN (498, 627, 625, 624);

UPDATE wpjy_menus_items
   SET parent = @sec_670_estrutura,
       sort = CASE id
         WHEN 501 THEN 0
         WHEN 502 THEN 1
         WHEN 646 THEN 2
         WHEN 647 THEN 3
         WHEN 658 THEN 4
         ELSE sort
       END
 WHERE @sec_670_estrutura IS NOT NULL
   AND menu = @menu_id
   AND id IN (501, 502, 646, 647, 658);

UPDATE wpjy_menus_items
   SET parent = @sec_670_crm,
       sort = CASE id
         WHEN 417 THEN 0
         WHEN 443 THEN 1
         WHEN 317 THEN 2
         WHEN 318 THEN 3
         WHEN 319 THEN 4
         WHEN 380 THEN 5
         ELSE sort
       END
 WHERE @sec_670_crm IS NOT NULL
   AND menu = @menu_id
   AND id IN (417, 443, 317, 318, 319, 380);

-- ---------------------------------------------------------------------------
-- 9) Secoes internas de Central de Relatorios (697)
-- ---------------------------------------------------------------------------

SET @root_697 = 697;

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Centrais',
  '#',
  'adm',
  @root_697,
  0,
  @menu_id,
  '_self',
  'ti ti-layout-dashboard',
  'menu.adm.relatorios.hub.centrais',
  'adm_relatorios_hub_centrais',
  NULL
WHERE @root_697 IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.relatorios.hub.centrais'
  );

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Financeiro',
  '#',
  'adm',
  @root_697,
  1,
  @menu_id,
  '_self',
  'ti ti-wallet',
  'menu.adm.relatorios.hub.financeiro',
  'adm_relatorios_hub_financeiro',
  NULL
WHERE @root_697 IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.relatorios.hub.financeiro'
  );

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Operacao',
  '#',
  'adm',
  @root_697,
  2,
  @menu_id,
  '_self',
  'ti ti-calendar-stats',
  'menu.adm.relatorios.hub.operacao',
  'adm_relatorios_hub_operacao',
  NULL
WHERE @root_697 IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.relatorios.hub.operacao'
  );

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Crescimento',
  '#',
  'adm',
  @root_697,
  3,
  @menu_id,
  '_self',
  'ti ti-chart-funnel',
  'menu.adm.relatorios.hub.crescimento',
  'adm_relatorios_hub_crescimento',
  NULL
WHERE @root_697 IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.relatorios.hub.crescimento'
  );

SET @sec_697_centrais = (
  SELECT id FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.relatorios.hub.centrais' LIMIT 1
);

SET @sec_697_financeiro = (
  SELECT id FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.relatorios.hub.financeiro' LIMIT 1
);

SET @sec_697_operacao = (
  SELECT id FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.relatorios.hub.operacao' LIMIT 1
);

SET @sec_697_crescimento = (
  SELECT id FROM wpjy_menus_items WHERE menu = @menu_id AND slug = 'menu.adm.relatorios.hub.crescimento' LIMIT 1
);

UPDATE wpjy_menus_items
   SET parent = @sec_697_centrais,
       sort = CASE id
         WHEN 698 THEN 0
         WHEN 699 THEN 1
         ELSE sort
       END
 WHERE @sec_697_centrais IS NOT NULL
   AND menu = @menu_id
   AND id IN (698, 699);

UPDATE wpjy_menus_items
   SET parent = @sec_697_financeiro,
       sort = CASE id
         WHEN 702 THEN 0
         WHEN 703 THEN 1
         WHEN 704 THEN 2
         WHEN 705 THEN 3
         ELSE sort
       END
 WHERE @sec_697_financeiro IS NOT NULL
   AND menu = @menu_id
   AND id IN (702, 703, 704, 705);

UPDATE wpjy_menus_items
   SET parent = @sec_697_operacao,
       sort = CASE id
         WHEN 700 THEN 0
         WHEN 701 THEN 1
         WHEN 709 THEN 2
         WHEN 710 THEN 3
         WHEN 711 THEN 4
         ELSE sort
       END
 WHERE @sec_697_operacao IS NOT NULL
   AND menu = @menu_id
   AND id IN (700, 701, 709, 710, 711);

UPDATE wpjy_menus_items
   SET parent = @sec_697_crescimento,
       sort = CASE id
         WHEN 706 THEN 0
         WHEN 707 THEN 1
         WHEN 708 THEN 2
         ELSE sort
       END
 WHERE @sec_697_crescimento IS NOT NULL
   AND menu = @menu_id
   AND id IN (706, 707, 708);

COMMIT;

-- ---------------------------------------------------------------------------
-- 10) Validacao rapida
-- ---------------------------------------------------------------------------

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

SELECT id, label, parent, sort, slug
  FROM wpjy_menus_items
 WHERE menu = @menu_id
   AND slug IN (
     'menu.adm.hub.trabalho',
     'menu.adm.hub.operacao',
     'menu.adm.hub.negocio',
     'menu.adm.hub.inteligencia',
     'menu.adm.hub.plataforma',
     'menu.adm.acessos.governanca',
     'menu.adm.acessos.diagnostico',
     'menu.adm.financeiro.cobranca',
     'menu.adm.financeiro.operacoes',
     'menu.adm.financeiro.gateways',
     'menu.adm.relatorios.hub.centrais',
     'menu.adm.relatorios.hub.financeiro',
     'menu.adm.relatorios.hub.operacao',
     'menu.adm.relatorios.hub.crescimento',
     'menu.adm.parceiros.base',
     'menu.adm.parceiros.catalogos',
     'menu.adm.parceiros.relacionamentos',
     'menu.adm.parceiros.leads_hub'
   )
 ORDER BY parent, sort, id;
