-- 2026-04-14
-- Cria a entrada principal "Relatorios" no menu admin
-- e adiciona os filhos "Central de Relatorios" e "Dashboards".
-- Tambem garante telas e permissoes base para os acessos.
-- Idempotente.

START TRANSACTION;

-- 1) Telas
INSERT INTO wpjy_telas (arquivo, nome, descricao, icone, ativa, ajuda)
SELECT 'relatorios.php', 'Central de Relatorios', 'Central administrativa de relatorios tabulares', 'ti ti-report-search', 1, NULL
WHERE NOT EXISTS (
  SELECT 1 FROM wpjy_telas WHERE arquivo = 'relatorios.php'
);

INSERT INTO wpjy_telas (arquivo, nome, descricao, icone, ativa, ajuda)
SELECT 'dashboards.php', 'Dashboards Dinamicos', 'Central administrativa de dashboards dinamicos', 'ti ti-layout-dashboard', 1, NULL
WHERE NOT EXISTS (
  SELECT 1 FROM wpjy_telas WHERE arquivo = 'dashboards.php'
);

-- 2) Permissoes de tela
INSERT INTO wpjy_permissoes (slug, nome, descricao, id_tela, tipo)
SELECT 'admin.relatorios.view', 'Central de Relatorios', 'Acesso a central de relatorios administrativos', t.id, 'tela'
FROM wpjy_telas t
WHERE t.arquivo = 'relatorios.php'
  AND NOT EXISTS (
    SELECT 1 FROM wpjy_permissoes WHERE slug = 'admin.relatorios.view'
  );

INSERT INTO wpjy_permissoes (slug, nome, descricao, id_tela, tipo)
SELECT 'admin.dashboards.view', 'Dashboards Dinamicos', 'Acesso a central de dashboards administrativos', t.id, 'tela'
FROM wpjy_telas t
WHERE t.arquivo = 'dashboards.php'
  AND NOT EXISTS (
    SELECT 1 FROM wpjy_permissoes WHERE slug = 'admin.dashboards.view'
  );

-- 3) Permissoes de menu
INSERT INTO wpjy_permissoes (slug, nome, descricao, id_tela, tipo)
SELECT 'menu.adm.relatorios', 'Menu - Relatorios', 'Grupo principal de Relatorios e Dashboards no menu admin', NULL, 'menu'
WHERE NOT EXISTS (
  SELECT 1 FROM wpjy_permissoes WHERE slug = 'menu.adm.relatorios'
);

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'
);

-- 4) Item principal no menu admin
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'
);

-- 5) Filho: Central 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'
  );

-- 6) Filho: Dashboards
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'
  );

-- 7) Libera 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 (
    'admin.relatorios.view',
    'admin.dashboards.view',
    'menu.adm.relatorios',
    'menu.adm.relatorios.central',
    'menu.adm.relatorios.dashboards'
  )
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;
