-- 2026-04-15
-- Adiciona a tela de Leads na area da empresa
-- Objetivos:
-- 1) criar permissao de menu legado
-- 2) inserir item no menu da area empresa
-- 3) cadastrar tela/namespace/mapeamento de arquivo
-- 4) liberar namespace no kit.empresa
-- 5) propagar permissao de menu para grupos vinculados ao kit.empresa
-- 6) fallback: liberar tambem para grupos principais da empresa em contratos com kit.empresa ativo

SET @kit_slug_empresa = CONVERT('kit.empresa' USING utf8mb4) COLLATE utf8mb4_0900_ai_ci;
SET @menu_root_slug = CONVERT('menu.area-da-empresa.35.18' USING utf8mb4) COLLATE utf8mb4_0900_ai_ci;

SET @kit_empresa_id = (
    SELECT id
      FROM wpjy_perm_kits
     WHERE slug = @kit_slug_empresa
     LIMIT 1
);

INSERT INTO wpjy_permissoes (slug, nome, descricao, id_tela, tipo)
SELECT 'empresa-leads', 'Empresa - Leads', 'Permissao de menu de leads da empresa', NULL, 'menu'
WHERE NOT EXISTS (SELECT 1 FROM wpjy_permissoes WHERE slug = 'empresa-leads');

SET @perm_empresa_leads_id = (
    SELECT id
      FROM wpjy_permissoes
     WHERE slug = 'empresa-leads'
     LIMIT 1
);

SET @menu_root_id = (
    SELECT id
      FROM wpjy_menus_items
     WHERE menu = 35
       AND slug = @menu_root_slug
     ORDER BY id ASC
     LIMIT 1
);

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT 'Leads', 'leads', 'empresa', @menu_root_id, 8, 35, '_self', 'solar:chart-square-line-duotone', 'empresa-leads', 'empresa_leads', NULL
WHERE @menu_root_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1
      FROM wpjy_menus_items
     WHERE menu = 35
       AND area = 'empresa'
       AND link = 'leads'
  );

INSERT INTO wpjy_telas (arquivo, nome, descricao, icone, ativa, ajuda)
SELECT 'empresa/leads', 'Empresa - Leads', 'Leads da empresa e de suas unidades operacionais', 'ti ti-chart-funnel', 1, NULL
WHERE NOT EXISTS (SELECT 1 FROM wpjy_telas WHERE arquivo = 'empresa/leads');

INSERT INTO wpjy_perm_namespaces (ns, label, descricao, escopo, ativo)
SELECT 'empresa.leads', 'Empresa: Leads', 'Tela de leads da empresa', 'contrato', 1
WHERE NOT EXISTS (SELECT 1 FROM wpjy_perm_namespaces WHERE ns = 'empresa.leads');

INSERT INTO wpjy_perm_tela_namespace (padrao_arquivo, ns, sensivel)
SELECT 'pages/empresa/leads.php', 'empresa.leads', 0
WHERE NOT EXISTS (
  SELECT 1
    FROM wpjy_perm_tela_namespace
   WHERE padrao_arquivo = 'pages/empresa/leads.php'
     AND ns = 'empresa.leads'
);

INSERT INTO wpjy_perm_kit_namespace (kit_id, namespace_id, effect)
SELECT @kit_empresa_id, n.id, 'allow'
  FROM wpjy_perm_namespaces n
 WHERE @kit_empresa_id IS NOT NULL
   AND n.ns = 'empresa.leads'
   AND NOT EXISTS (
     SELECT 1
       FROM wpjy_perm_kit_namespace kn
      WHERE kn.kit_id = @kit_empresa_id
        AND kn.namespace_id = n.id
   );

INSERT INTO wpjy_perm_tela_kit_map (id_tela, kit_id, prioridade)
SELECT t.id, @kit_empresa_id, 100
  FROM wpjy_telas t
 WHERE @kit_empresa_id IS NOT NULL
   AND t.arquivo = 'empresa/leads'
   AND NOT EXISTS (
     SELECT 1
       FROM wpjy_perm_tela_kit_map tk
      WHERE tk.id_tela = t.id
        AND tk.kit_id = @kit_empresa_id
   );

INSERT INTO wpjy_grupo_permissao (id_grupo, id_permissao)
SELECT gk.group_id, @perm_empresa_leads_id
  FROM wpjy_perm_group_kit gk
 WHERE @perm_empresa_leads_id IS NOT NULL
   AND gk.kit_id = @kit_empresa_id
   AND NOT EXISTS (
     SELECT 1
       FROM wpjy_grupo_permissao gp
      WHERE gp.id_grupo = gk.group_id
        AND gp.id_permissao = @perm_empresa_leads_id
   );

INSERT INTO wpjy_grupo_permissao (id_grupo, id_permissao)
SELECT g.id, @perm_empresa_leads_id
  FROM wpjy_grupos_permissoes g
  JOIN wpjy_perm_contract_kit ck
    ON ck.id_contrato = g.id_contrato
   AND ck.kit_id = @kit_empresa_id
   AND ck.ativo = 1
 WHERE @perm_empresa_leads_id IS NOT NULL
   AND @kit_empresa_id IS NOT NULL
   AND g.slug IN ('empresa', 'empresas')
   AND NOT EXISTS (
     SELECT 1
       FROM wpjy_grupo_permissao gp
      WHERE gp.id_grupo = g.id
        AND gp.id_permissao = @perm_empresa_leads_id
   );

SELECT
  @kit_empresa_id AS kit_empresa_id,
  @menu_root_id AS menu_root_id,
  @perm_empresa_leads_id AS perm_empresa_leads_id;
