-- 2026-04-16
-- Normalizacao final do TIIX HEALTH no menu
-- Objetivos:
-- 1) manter somente um root global de TIIX HEALTH em parent = 0
-- 2) remover TIIX HEALTH duplicado dentro de Empresa e Unidade
-- 3) consolidar um unico ramo de Agendamentos por contexto abaixo do root global
-- 4) deixar os presets no terceiro nivel com labels curtas

START TRANSACTION;

SET @menu_id = 35;

SET @tiix_root_id = (
  SELECT id
    FROM wpjy_menus_items
   WHERE menu = @menu_id
     AND slug = 'menu.tiixhealth.operacional'
   ORDER BY id ASC
   LIMIT 1
);

SET @empresa_nested_root_id = (
  SELECT id
    FROM wpjy_menus_items
   WHERE menu = @menu_id
     AND area = 'empresa'
     AND slug = 'menu.empresa.tiix-health'
   ORDER BY id ASC
   LIMIT 1
);

SET @unidade_nested_root_id = (
  SELECT id
    FROM wpjy_menus_items
   WHERE menu = @menu_id
     AND area = 'unidade'
     AND slug = 'menu.unidade.tiix-health'
   ORDER BY id ASC
   LIMIT 1
);

UPDATE wpjy_menus_items
   SET parent = 0,
       area = NULL,
       sort = 5,
       label = 'TIIX HEALTH',
       link = '#',
       icon = 'lucide:heart-pulse'
 WHERE id = @tiix_root_id;

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Agendamentos',
  '#',
  'empresa',
  @tiix_root_id,
  5,
  @menu_id,
  '_self',
  'ti ti-calendar-event',
  'menu.empresa.agendamentos_root_top',
  'empresa_agendamentos_root_top',
  NULL
WHERE @tiix_root_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1
      FROM wpjy_menus_items
     WHERE menu = @menu_id
       AND area = 'empresa'
       AND slug = 'menu.empresa.agendamentos_root_top'
  );

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Agendamentos',
  '#',
  'unidade',
  @tiix_root_id,
  26,
  @menu_id,
  '_self',
  'ti ti-calendar-event',
  'menu.unidade.agendamentos_root_top',
  'unidade_agendamentos_root_top',
  NULL
WHERE @tiix_root_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1
      FROM wpjy_menus_items
     WHERE menu = @menu_id
       AND area = 'unidade'
       AND slug = 'menu.unidade.agendamentos_root_top'
  );

SET @empresa_ag_root_id = (
  SELECT id
    FROM wpjy_menus_items
   WHERE menu = @menu_id
     AND area = 'empresa'
     AND slug = 'menu.empresa.agendamentos_root_top'
   ORDER BY id ASC
   LIMIT 1
);

SET @unidade_ag_root_id = (
  SELECT id
    FROM wpjy_menus_items
   WHERE menu = @menu_id
     AND area = 'unidade'
     AND slug = 'menu.unidade.agendamentos_root_top'
   ORDER BY id ASC
   LIMIT 1
);

UPDATE wpjy_menus_items
   SET parent = @tiix_root_id,
       sort = 5,
       label = 'Agendamentos',
       link = '#',
       icon = 'ti ti-calendar-event'
 WHERE id = @empresa_ag_root_id;

UPDATE wpjy_menus_items
   SET parent = @tiix_root_id,
       sort = 26,
       label = 'Agendamentos',
       link = '#',
       icon = 'ti ti-calendar-event'
 WHERE id = @unidade_ag_root_id;

UPDATE wpjy_menus_items
   SET parent = @tiix_root_id,
       sort = CASE
         WHEN area = 'empresa' AND link = 'profissionais' THEN 0
         WHEN area = 'empresa' AND link = 'medicos' THEN 1
         WHEN area = 'empresa' AND link = 'nutricionistas' THEN 2
         WHEN area = 'empresa' AND link = 'pacientes' THEN 3
         WHEN area = 'empresa' AND link = 'disponibilidade' THEN 4
         WHEN area = 'empresa' AND slug = 'menu.empresa.agendamentos_root_top' THEN 5
         WHEN area = 'empresa' AND link = 'agendamento-manual' THEN 6
         WHEN area = 'empresa' AND link = 'pre-agendamentos' THEN 7
         WHEN area = 'unidade' AND link = 'agenda' THEN 20
         WHEN area = 'unidade' AND link = 'profissionais' THEN 21
         WHEN area = 'unidade' AND link = 'medicos' THEN 22
         WHEN area = 'unidade' AND link = 'nutricionistas' THEN 23
         WHEN area = 'unidade' AND link = 'pacientes' THEN 24
         WHEN area = 'unidade' AND link = 'disponibilidade' THEN 25
         WHEN area = 'unidade' AND slug = 'menu.unidade.agendamentos_root_top' THEN 26
         WHEN area = 'unidade' AND link = 'agendamento-manual' THEN 27
         WHEN area = 'unidade' AND link = 'pre-agendamentos' THEN 28
         ELSE sort
       END
 WHERE @tiix_root_id IS NOT NULL
   AND menu = @menu_id
   AND (
        (area = 'empresa' AND link IN ('profissionais', 'medicos', 'nutricionistas', 'pacientes', 'disponibilidade', 'agendamento-manual', 'pre-agendamentos'))
        OR
        (area = 'unidade' AND link IN ('agenda', 'profissionais', 'medicos', 'nutricionistas', 'pacientes', 'disponibilidade', 'agendamento-manual', 'pre-agendamentos'))
        OR
        id IN (@empresa_ag_root_id, @unidade_ag_root_id)
   );

UPDATE wpjy_menus_items
   SET parent = @empresa_ag_root_id,
       label = CASE
         WHEN slug = 'empresa-agendamentos' THEN 'Todos'
         WHEN slug = 'menu.empresa.agendamentos_pendentes' THEN 'Pendentes'
         WHEN slug = 'menu.empresa.agendamentos_comparecimento' THEN 'Comparecimento'
         WHEN slug = 'menu.empresa.agendamentos_finalizacao' THEN 'Finalizacao'
         WHEN slug = 'menu.empresa.agendamentos_finalizados' THEN 'Finalizados'
         WHEN slug = 'menu.empresa.agendamentos_cancelamentos' THEN 'Cancelamentos'
         WHEN slug = 'menu.empresa.agendamentos_faltas' THEN 'Faltas'
         ELSE label
       END,
       sort = CASE
         WHEN slug = 'empresa-agendamentos' THEN 0
         WHEN slug = 'menu.empresa.agendamentos_pendentes' THEN 1
         WHEN slug = 'menu.empresa.agendamentos_comparecimento' THEN 2
         WHEN slug = 'menu.empresa.agendamentos_finalizacao' THEN 3
         WHEN slug = 'menu.empresa.agendamentos_finalizados' THEN 4
         WHEN slug = 'menu.empresa.agendamentos_cancelamentos' THEN 5
         WHEN slug = 'menu.empresa.agendamentos_faltas' THEN 6
         ELSE sort
       END
 WHERE @empresa_ag_root_id IS NOT NULL
   AND menu = @menu_id
   AND area = 'empresa'
   AND slug IN (
     'empresa-agendamentos',
     'menu.empresa.agendamentos_pendentes',
     'menu.empresa.agendamentos_comparecimento',
     'menu.empresa.agendamentos_finalizacao',
     'menu.empresa.agendamentos_finalizados',
     'menu.empresa.agendamentos_cancelamentos',
     'menu.empresa.agendamentos_faltas'
   );

UPDATE wpjy_menus_items
   SET parent = @unidade_ag_root_id,
       label = CASE
         WHEN slug = 'menu.unidade.agendamentos' THEN 'Todos'
         WHEN slug = 'menu.unidade.agendamentos_pendentes' THEN 'Pendentes'
         WHEN slug = 'menu.unidade.agendamentos_comparecimento' THEN 'Comparecimento'
         WHEN slug = 'menu.unidade.agendamentos_finalizacao' THEN 'Finalizacao'
         WHEN slug = 'menu.unidade.agendamentos_finalizados' THEN 'Finalizados'
         WHEN slug = 'menu.unidade.agendamentos_cancelamentos' THEN 'Cancelamentos'
         WHEN slug = 'menu.unidade.agendamentos_faltas' THEN 'Faltas'
         ELSE label
       END,
       sort = CASE
         WHEN slug = 'menu.unidade.agendamentos' THEN 0
         WHEN slug = 'menu.unidade.agendamentos_pendentes' THEN 1
         WHEN slug = 'menu.unidade.agendamentos_comparecimento' THEN 2
         WHEN slug = 'menu.unidade.agendamentos_finalizacao' THEN 3
         WHEN slug = 'menu.unidade.agendamentos_finalizados' THEN 4
         WHEN slug = 'menu.unidade.agendamentos_cancelamentos' THEN 5
         WHEN slug = 'menu.unidade.agendamentos_faltas' THEN 6
         ELSE sort
       END
 WHERE @unidade_ag_root_id IS NOT NULL
   AND menu = @menu_id
   AND area = 'unidade'
   AND slug IN (
     'menu.unidade.agendamentos',
     'menu.unidade.agendamentos_pendentes',
     'menu.unidade.agendamentos_comparecimento',
     'menu.unidade.agendamentos_finalizacao',
     'menu.unidade.agendamentos_finalizados',
     'menu.unidade.agendamentos_cancelamentos',
     'menu.unidade.agendamentos_faltas'
   );

UPDATE wpjy_menus_items
   SET parent = 542,
       sort = 90
 WHERE id = @empresa_nested_root_id;

UPDATE wpjy_menus_items
   SET parent = 714,
       sort = 90
 WHERE id = @unidade_nested_root_id;

DELETE FROM wpjy_menus_items
 WHERE menu = @menu_id
   AND id IN (@empresa_nested_root_id, @unidade_nested_root_id)
   AND id IS NOT NULL;

DELETE FROM wpjy_menus_items
 WHERE menu = @menu_id
   AND area = 'unidade'
   AND slug = 'menu.unidade.agendamentos_root'
   AND id <> @unidade_ag_root_id;

DELETE FROM wpjy_menus_items
 WHERE menu = @menu_id
   AND area = 'empresa'
   AND slug = 'menu.empresa.agendamentos_root'
   AND id <> @empresa_ag_root_id;

COMMIT;

SELECT id, label, link, area, parent, sort, slug, parametro_url
  FROM wpjy_menus_items
 WHERE id = @tiix_root_id
    OR parent = @tiix_root_id
    OR parent IN (@empresa_ag_root_id, @unidade_ag_root_id)
 ORDER BY parent, area, sort, id;
