-- 2026-04-17
-- Pagina de changelogs autorais para colaboradores no menu Dev
-- Objetivos:
-- 1) cadastrar a tela /adm/dev-changelogs
-- 2) liberar a permissao de acesso para colaboradores internos
-- 3) incluir o item no menu tecnico sem duplicar registros

START TRANSACTION;

SET @menu_id = 35;
SET @page_file = 'dev-changelogs.php';
SET @perm_slug = 'devwork.changelogs.view';

INSERT INTO wpjy_telas (arquivo, nome, descricao, icone, ativa, ajuda)
SELECT
  @page_file,
  'Changelogs da Equipe',
  'Changelogs autorais da equipe',
  'ti ti-notebook',
  1,
  NULL
WHERE NOT EXISTS (
  SELECT 1
    FROM wpjy_telas
   WHERE arquivo COLLATE utf8mb4_unicode_ci = @page_file COLLATE utf8mb4_unicode_ci
);

INSERT INTO wpjy_permissoes (slug, nome, descricao, id_tela, tipo)
SELECT
  @perm_slug,
  'Changelogs da Equipe',
  'Acesso a tela de changelogs autorais da equipe tecnica e colaboradores internos.',
  t.id,
  'tela'
FROM wpjy_telas t
WHERE t.arquivo COLLATE utf8mb4_unicode_ci = @page_file COLLATE utf8mb4_unicode_ci
  AND NOT EXISTS (
    SELECT 1
      FROM wpjy_permissoes
     WHERE slug COLLATE utf8mb4_unicode_ci = @perm_slug COLLATE utf8mb4_unicode_ci
  );

INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Meus Changelogs',
  'dev-changelogs',
  'adm',
  560,
  14,
  @menu_id,
  '_self',
  'lucide:notebook-pen',
  @perm_slug,
  'dev_changelogs',
  NULL
WHERE NOT EXISTS (
  SELECT 1
    FROM wpjy_menus_items
   WHERE menu = @menu_id
     AND area = 'adm'
     AND (
       link COLLATE utf8mb4_unicode_ci = 'dev-changelogs' COLLATE utf8mb4_unicode_ci
       OR slug COLLATE utf8mb4_unicode_ci = @perm_slug COLLATE utf8mb4_unicode_ci
       OR tag COLLATE utf8mb4_unicode_ci = 'dev_changelogs' COLLATE utf8mb4_unicode_ci
     )
);

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 COLLATE utf8mb4_unicode_ci = @perm_slug COLLATE utf8mb4_unicode_ci
 WHERE g.slug COLLATE utf8mb4_unicode_ci IN (
   'super_admin',
   'prevclube',
   'tiix',
   'dev-colaborador',
   'dev-viewer',
   'dev-lead',
   'tech-lead',
   'dev-arquiteto',
   'people-ops-dev',
   'delivery-manager',
   'product-owner-dev',
   'qa-colaborador',
   'design-colaborador',
   'marketing-colaborador',
   'product-colaborador',
   'workspace',
   'workspace-user',
   'colaborador-interno'
 )
   AND NOT EXISTS (
     SELECT 1
       FROM wpjy_grupo_permissao gp
      WHERE gp.id_grupo = g.id
        AND gp.id_permissao = p.id
   );

COMMIT;

SELECT id, arquivo, nome
  FROM wpjy_telas
 WHERE arquivo COLLATE utf8mb4_unicode_ci = @page_file COLLATE utf8mb4_unicode_ci;

SELECT id, slug, nome, tipo
  FROM wpjy_permissoes
 WHERE slug COLLATE utf8mb4_unicode_ci = @perm_slug COLLATE utf8mb4_unicode_ci;

SELECT id, label, link, parent, sort, slug, tag
  FROM wpjy_menus_items
 WHERE menu = @menu_id
   AND (
     link COLLATE utf8mb4_unicode_ci = 'dev-changelogs' COLLATE utf8mb4_unicode_ci
     OR slug COLLATE utf8mb4_unicode_ci = @perm_slug COLLATE utf8mb4_unicode_ci
     OR tag COLLATE utf8mb4_unicode_ci = 'dev_changelogs' COLLATE utf8mb4_unicode_ci
   );
