-- 2026-04-17
-- Libera paginas de operacao colaborativa no menu tecnico.
-- Objetivos:
-- 1) cadastrar incidentes, base operacional e qualidade operacional como telas
-- 2) liberar slugs de view/manage para colaboradores internos
-- 3) colocar os atalhos no menu tecnico sem duplicidade

START TRANSACTION;

SET @menu_id = 35;
SET @parent_id = 560;

INSERT INTO wpjy_telas (arquivo, nome, descricao, icone, ativa, ajuda)
SELECT 'incidentes-operacionais.php', 'Incidentes Operacionais', 'Gestao de incidentes internos e postmortems.', 'ti ti-alert-octagon', 1, NULL
WHERE NOT EXISTS (SELECT 1 FROM wpjy_telas WHERE arquivo COLLATE utf8mb4_unicode_ci = 'incidentes-operacionais.php' COLLATE utf8mb4_unicode_ci);

INSERT INTO wpjy_telas (arquivo, nome, descricao, icone, ativa, ajuda)
SELECT 'base-operacional.php', 'Base Operacional', 'Runbooks, decisoes e conhecimento vinculado a tarefas.', 'ti ti-book-2', 1, NULL
WHERE NOT EXISTS (SELECT 1 FROM wpjy_telas WHERE arquivo COLLATE utf8mb4_unicode_ci = 'base-operacional.php' COLLATE utf8mb4_unicode_ci);

INSERT INTO wpjy_telas (arquivo, nome, descricao, icone, ativa, ajuda)
SELECT 'qualidade-operacional.php', 'Qualidade Operacional', 'Eventos de qualidade, custos, sugestoes e snapshots.', 'ti ti-chart-arcs-3', 1, NULL
WHERE NOT EXISTS (SELECT 1 FROM wpjy_telas WHERE arquivo COLLATE utf8mb4_unicode_ci = 'qualidade-operacional.php' COLLATE utf8mb4_unicode_ci);

INSERT INTO wpjy_permissoes (slug, nome, descricao, id_tela, tipo)
SELECT 'workspace.incidents.view', 'Incidentes Operacionais', 'Acesso a pagina de incidentes operacionais.', t.id, 'tela'
FROM wpjy_telas t
WHERE t.arquivo COLLATE utf8mb4_unicode_ci = 'incidentes-operacionais.php' COLLATE utf8mb4_unicode_ci
  AND NOT EXISTS (SELECT 1 FROM wpjy_permissoes WHERE slug COLLATE utf8mb4_unicode_ci = 'workspace.incidents.view' COLLATE utf8mb4_unicode_ci);

INSERT INTO wpjy_permissoes (slug, nome, descricao, id_tela, tipo)
SELECT 'workspace.incidents.manage', 'Gerir incidentes operacionais', 'Criar, editar e remover incidentes operacionais.', t.id, 'acao'
FROM wpjy_telas t
WHERE t.arquivo COLLATE utf8mb4_unicode_ci = 'incidentes-operacionais.php' COLLATE utf8mb4_unicode_ci
  AND NOT EXISTS (SELECT 1 FROM wpjy_permissoes WHERE slug COLLATE utf8mb4_unicode_ci = 'workspace.incidents.manage' COLLATE utf8mb4_unicode_ci);

INSERT INTO wpjy_permissoes (slug, nome, descricao, id_tela, tipo)
SELECT 'workspace.knowledge.view', 'Base Operacional', 'Acesso a runbooks, decisoes e vinculos de conhecimento.', t.id, 'tela'
FROM wpjy_telas t
WHERE t.arquivo COLLATE utf8mb4_unicode_ci = 'base-operacional.php' COLLATE utf8mb4_unicode_ci
  AND NOT EXISTS (SELECT 1 FROM wpjy_permissoes WHERE slug COLLATE utf8mb4_unicode_ci = 'workspace.knowledge.view' COLLATE utf8mb4_unicode_ci);

INSERT INTO wpjy_permissoes (slug, nome, descricao, id_tela, tipo)
SELECT 'workspace.knowledge.manage', 'Gerir base operacional', 'Criar, editar e remover runbooks, decisoes e vinculos.', t.id, 'acao'
FROM wpjy_telas t
WHERE t.arquivo COLLATE utf8mb4_unicode_ci = 'base-operacional.php' COLLATE utf8mb4_unicode_ci
  AND NOT EXISTS (SELECT 1 FROM wpjy_permissoes WHERE slug COLLATE utf8mb4_unicode_ci = 'workspace.knowledge.manage' COLLATE utf8mb4_unicode_ci);

INSERT INTO wpjy_permissoes (slug, nome, descricao, id_tela, tipo)
SELECT 'workspace.quality.view', 'Qualidade Operacional', 'Acesso aos paineis de qualidade, custos e snapshots.', t.id, 'tela'
FROM wpjy_telas t
WHERE t.arquivo COLLATE utf8mb4_unicode_ci = 'qualidade-operacional.php' COLLATE utf8mb4_unicode_ci
  AND NOT EXISTS (SELECT 1 FROM wpjy_permissoes WHERE slug COLLATE utf8mb4_unicode_ci = 'workspace.quality.view' COLLATE utf8mb4_unicode_ci);

INSERT INTO wpjy_permissoes (slug, nome, descricao, id_tela, tipo)
SELECT 'workspace.quality.manage', 'Gerir qualidade operacional', 'Criar, editar e remover eventos, custos e sugestoes.', t.id, 'acao'
FROM wpjy_telas t
WHERE t.arquivo COLLATE utf8mb4_unicode_ci = 'qualidade-operacional.php' COLLATE utf8mb4_unicode_ci
  AND NOT EXISTS (SELECT 1 FROM wpjy_permissoes WHERE slug COLLATE utf8mb4_unicode_ci = 'workspace.quality.manage' COLLATE utf8mb4_unicode_ci);

INSERT INTO wpjy_menus_items (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT 'Incidentes', 'incidentes-operacionais', 'adm', @parent_id, 15, @menu_id, '_self', 'lucide:shield-alert', 'workspace.incidents.view', 'workspace_incidents', NULL
WHERE NOT EXISTS (
  SELECT 1 FROM wpjy_menus_items
  WHERE menu = @menu_id AND area = 'adm'
    AND (link COLLATE utf8mb4_unicode_ci = 'incidentes-operacionais' COLLATE utf8mb4_unicode_ci
      OR slug COLLATE utf8mb4_unicode_ci = 'workspace.incidents.view' COLLATE utf8mb4_unicode_ci
      OR tag COLLATE utf8mb4_unicode_ci = 'workspace_incidents' COLLATE utf8mb4_unicode_ci)
);

INSERT INTO wpjy_menus_items (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT 'Base Operacional', 'base-operacional', 'adm', @parent_id, 16, @menu_id, '_self', 'lucide:book-open-text', 'workspace.knowledge.view', 'workspace_knowledge', NULL
WHERE NOT EXISTS (
  SELECT 1 FROM wpjy_menus_items
  WHERE menu = @menu_id AND area = 'adm'
    AND (link COLLATE utf8mb4_unicode_ci = 'base-operacional' COLLATE utf8mb4_unicode_ci
      OR slug COLLATE utf8mb4_unicode_ci = 'workspace.knowledge.view' COLLATE utf8mb4_unicode_ci
      OR tag COLLATE utf8mb4_unicode_ci = 'workspace_knowledge' COLLATE utf8mb4_unicode_ci)
);

INSERT INTO wpjy_menus_items (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT 'Qualidade Operacional', 'qualidade-operacional', 'adm', @parent_id, 17, @menu_id, '_self', 'lucide:gauge', 'workspace.quality.view', 'workspace_quality', NULL
WHERE NOT EXISTS (
  SELECT 1 FROM wpjy_menus_items
  WHERE menu = @menu_id AND area = 'adm'
    AND (link COLLATE utf8mb4_unicode_ci = 'qualidade-operacional' COLLATE utf8mb4_unicode_ci
      OR slug COLLATE utf8mb4_unicode_ci = 'workspace.quality.view' COLLATE utf8mb4_unicode_ci
      OR tag COLLATE utf8mb4_unicode_ci = 'workspace_quality' 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 IN (
  'workspace.incidents.view',
  'workspace.incidents.manage',
  'workspace.knowledge.view',
  'workspace.knowledge.manage',
  'workspace.quality.view',
  'workspace.quality.manage'
)
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;
