-- 2026-04-14
-- Complementa o menu admin "Relatorios" com todos os relatorios criados
-- e mantem Dashboards como filho direto do mesmo grupo.
-- Idempotente.

START TRANSACTION;

-- 1) Garante o pai Relatorios
INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Relatorios',
  '#',
  'adm',
  0,
  90,
  35,
  '_self',
  'ti ti-report-search',
  'menu.adm.relatorios',
  'adm_relatorios_root',
  NULL
WHERE NOT EXISTS (
  SELECT 1
  FROM wpjy_menus_items
  WHERE menu = 35
    AND area = 'adm'
    AND slug = 'menu.adm.relatorios'
);

-- 2) Permissoes de menu dos filhos
INSERT INTO wpjy_permissoes (slug, nome, descricao, id_tela, tipo)
SELECT 'menu.adm.relatorios.central', 'Menu - Central de Relatorios', 'Item de menu para a Central de Relatorios', NULL, 'menu'
WHERE NOT EXISTS (SELECT 1 FROM wpjy_permissoes WHERE slug = 'menu.adm.relatorios.central');

INSERT INTO wpjy_permissoes (slug, nome, descricao, id_tela, tipo)
SELECT 'menu.adm.relatorios.dashboards', 'Menu - Dashboards', 'Item de menu para a Central de Dashboards', NULL, 'menu'
WHERE NOT EXISTS (SELECT 1 FROM wpjy_permissoes WHERE slug = 'menu.adm.relatorios.dashboards');

INSERT INTO wpjy_permissoes (slug, nome, descricao, id_tela, tipo)
SELECT 'menu.adm.relatorios.agenda_performance', 'Menu - Agenda Performance', 'Item de menu para o relatorio Agenda - Performance', NULL, 'menu'
WHERE NOT EXISTS (SELECT 1 FROM wpjy_permissoes WHERE slug = 'menu.adm.relatorios.agenda_performance');

INSERT INTO wpjy_permissoes (slug, nome, descricao, id_tela, tipo)
SELECT 'menu.adm.relatorios.documentos_pendencias', 'Menu - Documentos Pendencias', 'Item de menu para o relatorio Documentos - Pendencias', NULL, 'menu'
WHERE NOT EXISTS (SELECT 1 FROM wpjy_permissoes WHERE slug = 'menu.adm.relatorios.documentos_pendencias');

INSERT INTO wpjy_permissoes (slug, nome, descricao, id_tela, tipo)
SELECT 'menu.adm.relatorios.financeiro_assinaturas', 'Menu - Financeiro Assinaturas', 'Item de menu para o relatorio Financeiro - Assinaturas', NULL, 'menu'
WHERE NOT EXISTS (SELECT 1 FROM wpjy_permissoes WHERE slug = 'menu.adm.relatorios.financeiro_assinaturas');

INSERT INTO wpjy_permissoes (slug, nome, descricao, id_tela, tipo)
SELECT 'menu.adm.relatorios.financeiro_inadimplencia', 'Menu - Financeiro Inadimplencia', 'Item de menu para o relatorio Financeiro - Inadimplencia', NULL, 'menu'
WHERE NOT EXISTS (SELECT 1 FROM wpjy_permissoes WHERE slug = 'menu.adm.relatorios.financeiro_inadimplencia');

INSERT INTO wpjy_permissoes (slug, nome, descricao, id_tela, tipo)
SELECT 'menu.adm.relatorios.financeiro_recebiveis', 'Menu - Financeiro Recebiveis', 'Item de menu para o relatorio Financeiro - Recebiveis', NULL, 'menu'
WHERE NOT EXISTS (SELECT 1 FROM wpjy_permissoes WHERE slug = 'menu.adm.relatorios.financeiro_recebiveis');

INSERT INTO wpjy_permissoes (slug, nome, descricao, id_tela, tipo)
SELECT 'menu.adm.relatorios.financeiro_repasses', 'Menu - Financeiro Repasses', 'Item de menu para o relatorio Financeiro - Repasses', NULL, 'menu'
WHERE NOT EXISTS (SELECT 1 FROM wpjy_permissoes WHERE slug = 'menu.adm.relatorios.financeiro_repasses');

INSERT INTO wpjy_permissoes (slug, nome, descricao, id_tela, tipo)
SELECT 'menu.adm.relatorios.leads_conversao', 'Menu - Leads Conversao', 'Item de menu para o relatorio Leads - Conversao', NULL, 'menu'
WHERE NOT EXISTS (SELECT 1 FROM wpjy_permissoes WHERE slug = 'menu.adm.relatorios.leads_conversao');

INSERT INTO wpjy_permissoes (slug, nome, descricao, id_tela, tipo)
SELECT 'menu.adm.relatorios.metas_progresso', 'Menu - Metas Progresso', 'Item de menu para o relatorio Metas - Progresso', NULL, 'menu'
WHERE NOT EXISTS (SELECT 1 FROM wpjy_permissoes WHERE slug = 'menu.adm.relatorios.metas_progresso');

INSERT INTO wpjy_permissoes (slug, nome, descricao, id_tela, tipo)
SELECT 'menu.adm.relatorios.partners_atendimento', 'Menu - Partners Atendimento', 'Item de menu para o relatorio Partners - Atendimento', NULL, 'menu'
WHERE NOT EXISTS (SELECT 1 FROM wpjy_permissoes WHERE slug = 'menu.adm.relatorios.partners_atendimento');

INSERT INTO wpjy_permissoes (slug, nome, descricao, id_tela, tipo)
SELECT 'menu.adm.relatorios.people_workload', 'Menu - People Workload', 'Item de menu para o relatorio People - Workload', NULL, 'menu'
WHERE NOT EXISTS (SELECT 1 FROM wpjy_permissoes WHERE slug = 'menu.adm.relatorios.people_workload');

INSERT INTO wpjy_permissoes (slug, nome, descricao, id_tela, tipo)
SELECT 'menu.adm.relatorios.tarefas_risco', 'Menu - Tarefas Risco', 'Item de menu para o relatorio Tarefas - Em Risco', NULL, 'menu'
WHERE NOT EXISTS (SELECT 1 FROM wpjy_permissoes WHERE slug = 'menu.adm.relatorios.tarefas_risco');

INSERT INTO wpjy_permissoes (slug, nome, descricao, id_tela, tipo)
SELECT 'menu.adm.relatorios.tickets_backlog', 'Menu - Tickets Backlog', 'Item de menu para o relatorio Tickets - Backlog', NULL, 'menu'
WHERE NOT EXISTS (SELECT 1 FROM wpjy_permissoes WHERE slug = 'menu.adm.relatorios.tickets_backlog');

-- 3) Filhos diretos de Relatorios
INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT 'Central de Relatorios', 'relatorios', 'adm', root.id, 1, 35, '_self', 'ti ti-report-search', 'menu.adm.relatorios.central', 'adm_relatorios_central', NULL
FROM wpjy_menus_items root
WHERE root.menu = 35
  AND root.area = 'adm'
  AND root.slug = 'menu.adm.relatorios'
  AND NOT EXISTS (
    SELECT 1 FROM wpjy_menus_items mi
    WHERE mi.menu = 35 AND mi.area = 'adm' AND mi.slug = 'menu.adm.relatorios.central'
  );

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT 'Dashboards', 'dashboards', 'adm', root.id, 2, 35, '_self', 'ti ti-layout-dashboard', 'menu.adm.relatorios.dashboards', 'adm_relatorios_dashboards', NULL
FROM wpjy_menus_items root
WHERE root.menu = 35
  AND root.area = 'adm'
  AND root.slug = 'menu.adm.relatorios'
  AND NOT EXISTS (
    SELECT 1 FROM wpjy_menus_items mi
    WHERE mi.menu = 35 AND mi.area = 'adm' AND mi.slug = 'menu.adm.relatorios.dashboards'
  );

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT 'Agenda - Performance', 'relatorio-agenda-performance', 'adm', root.id, 10, 35, '_self', 'ti ti-calendar-stats', 'menu.adm.relatorios.agenda_performance', 'adm_relatorio_agenda_performance', NULL
FROM wpjy_menus_items root
WHERE root.menu = 35 AND root.area = 'adm' AND root.slug = 'menu.adm.relatorios'
  AND NOT EXISTS (SELECT 1 FROM wpjy_menus_items mi WHERE mi.menu = 35 AND mi.area = 'adm' AND mi.slug = 'menu.adm.relatorios.agenda_performance');

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT 'Documentos - Pendencias', 'relatorio-documentos-pendencias', 'adm', root.id, 11, 35, '_self', 'ti ti-file-alert', 'menu.adm.relatorios.documentos_pendencias', 'adm_relatorio_documentos_pendencias', NULL
FROM wpjy_menus_items root
WHERE root.menu = 35 AND root.area = 'adm' AND root.slug = 'menu.adm.relatorios'
  AND NOT EXISTS (SELECT 1 FROM wpjy_menus_items mi WHERE mi.menu = 35 AND mi.area = 'adm' AND mi.slug = 'menu.adm.relatorios.documentos_pendencias');

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT 'Financeiro - Assinaturas', 'relatorio-financeiro-assinaturas', 'adm', root.id, 12, 35, '_self', 'ti ti-id-badge-2', 'menu.adm.relatorios.financeiro_assinaturas', 'adm_relatorio_financeiro_assinaturas', NULL
FROM wpjy_menus_items root
WHERE root.menu = 35 AND root.area = 'adm' AND root.slug = 'menu.adm.relatorios'
  AND NOT EXISTS (SELECT 1 FROM wpjy_menus_items mi WHERE mi.menu = 35 AND mi.area = 'adm' AND mi.slug = 'menu.adm.relatorios.financeiro_assinaturas');

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT 'Financeiro - Inadimplencia', 'relatorio-financeiro-inadimplencia', 'adm', root.id, 13, 35, '_self', 'ti ti-alert-circle', 'menu.adm.relatorios.financeiro_inadimplencia', 'adm_relatorio_financeiro_inadimplencia', NULL
FROM wpjy_menus_items root
WHERE root.menu = 35 AND root.area = 'adm' AND root.slug = 'menu.adm.relatorios'
  AND NOT EXISTS (SELECT 1 FROM wpjy_menus_items mi WHERE mi.menu = 35 AND mi.area = 'adm' AND mi.slug = 'menu.adm.relatorios.financeiro_inadimplencia');

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT 'Financeiro - Recebiveis', 'relatorio-financeiro-recebiveis', 'adm', root.id, 14, 35, '_self', 'ti ti-receipt-2', 'menu.adm.relatorios.financeiro_recebiveis', 'adm_relatorio_financeiro_recebiveis', NULL
FROM wpjy_menus_items root
WHERE root.menu = 35 AND root.area = 'adm' AND root.slug = 'menu.adm.relatorios'
  AND NOT EXISTS (SELECT 1 FROM wpjy_menus_items mi WHERE mi.menu = 35 AND mi.area = 'adm' AND mi.slug = 'menu.adm.relatorios.financeiro_recebiveis');

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT 'Financeiro - Repasses', 'relatorio-financeiro-repasses', 'adm', root.id, 15, 35, '_self', 'ti ti-wallet', 'menu.adm.relatorios.financeiro_repasses', 'adm_relatorio_financeiro_repasses', NULL
FROM wpjy_menus_items root
WHERE root.menu = 35 AND root.area = 'adm' AND root.slug = 'menu.adm.relatorios'
  AND NOT EXISTS (SELECT 1 FROM wpjy_menus_items mi WHERE mi.menu = 35 AND mi.area = 'adm' AND mi.slug = 'menu.adm.relatorios.financeiro_repasses');

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT 'Leads - Conversao', 'relatorio-leads-conversao', 'adm', root.id, 16, 35, '_self', 'ti ti-chart-funnel', 'menu.adm.relatorios.leads_conversao', 'adm_relatorio_leads_conversao', NULL
FROM wpjy_menus_items root
WHERE root.menu = 35 AND root.area = 'adm' AND root.slug = 'menu.adm.relatorios'
  AND NOT EXISTS (SELECT 1 FROM wpjy_menus_items mi WHERE mi.menu = 35 AND mi.area = 'adm' AND mi.slug = 'menu.adm.relatorios.leads_conversao');

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT 'Metas - Progresso', 'relatorio-metas-progresso', 'adm', root.id, 17, 35, '_self', 'ti ti-target-arrow', 'menu.adm.relatorios.metas_progresso', 'adm_relatorio_metas_progresso', NULL
FROM wpjy_menus_items root
WHERE root.menu = 35 AND root.area = 'adm' AND root.slug = 'menu.adm.relatorios'
  AND NOT EXISTS (SELECT 1 FROM wpjy_menus_items mi WHERE mi.menu = 35 AND mi.area = 'adm' AND mi.slug = 'menu.adm.relatorios.metas_progresso');

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT 'Partners - Atendimento', 'relatorio-partners-atendimento', 'adm', root.id, 18, 35, '_self', 'ti ti-lifebuoy', 'menu.adm.relatorios.partners_atendimento', 'adm_relatorio_partners_atendimento', NULL
FROM wpjy_menus_items root
WHERE root.menu = 35 AND root.area = 'adm' AND root.slug = 'menu.adm.relatorios'
  AND NOT EXISTS (SELECT 1 FROM wpjy_menus_items mi WHERE mi.menu = 35 AND mi.area = 'adm' AND mi.slug = 'menu.adm.relatorios.partners_atendimento');

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT 'People - Workload', 'relatorio-people-workload', 'adm', root.id, 19, 35, '_self', 'ti ti-users-group', 'menu.adm.relatorios.people_workload', 'adm_relatorio_people_workload', NULL
FROM wpjy_menus_items root
WHERE root.menu = 35 AND root.area = 'adm' AND root.slug = 'menu.adm.relatorios'
  AND NOT EXISTS (SELECT 1 FROM wpjy_menus_items mi WHERE mi.menu = 35 AND mi.area = 'adm' AND mi.slug = 'menu.adm.relatorios.people_workload');

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT 'Tarefas - Em risco', 'relatorio-tarefas-risco', 'adm', root.id, 20, 35, '_self', 'ti ti-alert-octagon', 'menu.adm.relatorios.tarefas_risco', 'adm_relatorio_tarefas_risco', NULL
FROM wpjy_menus_items root
WHERE root.menu = 35 AND root.area = 'adm' AND root.slug = 'menu.adm.relatorios'
  AND NOT EXISTS (SELECT 1 FROM wpjy_menus_items mi WHERE mi.menu = 35 AND mi.area = 'adm' AND mi.slug = 'menu.adm.relatorios.tarefas_risco');

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT 'Tickets - Backlog', 'relatorio-tickets-backlog', 'adm', root.id, 21, 35, '_self', 'ti ti-ticket', 'menu.adm.relatorios.tickets_backlog', 'adm_relatorio_tickets_backlog', NULL
FROM wpjy_menus_items root
WHERE root.menu = 35 AND root.area = 'adm' AND root.slug = 'menu.adm.relatorios'
  AND NOT EXISTS (SELECT 1 FROM wpjy_menus_items mi WHERE mi.menu = 35 AND mi.area = 'adm' AND mi.slug = 'menu.adm.relatorios.tickets_backlog');

-- 4) Libera os slugs de menu para super admin
INSERT INTO wpjy_grupo_permissao (id_grupo, id_permissao)
SELECT g.id, p.id
FROM wpjy_grupos_permissoes g
JOIN wpjy_permissoes p
  ON p.slug IN (
    'menu.adm.relatorios.central',
    'menu.adm.relatorios.dashboards',
    'menu.adm.relatorios.agenda_performance',
    'menu.adm.relatorios.documentos_pendencias',
    'menu.adm.relatorios.financeiro_assinaturas',
    'menu.adm.relatorios.financeiro_inadimplencia',
    'menu.adm.relatorios.financeiro_recebiveis',
    'menu.adm.relatorios.financeiro_repasses',
    'menu.adm.relatorios.leads_conversao',
    'menu.adm.relatorios.metas_progresso',
    'menu.adm.relatorios.partners_atendimento',
    'menu.adm.relatorios.people_workload',
    'menu.adm.relatorios.tarefas_risco',
    'menu.adm.relatorios.tickets_backlog'
  )
WHERE g.slug = 'super_admin'
  AND NOT EXISTS (
    SELECT 1
    FROM wpjy_grupo_permissao gp
    WHERE gp.id_grupo = g.id
      AND gp.id_permissao = p.id
  );

COMMIT;
