-- 2026-04-15
-- Formaliza o agendamento manual no core de telas/permissoes/menu
-- e prepara baseline para admin, cliente, profissional, empresa, assistente e corretor.
-- Idempotente: pode rodar mais de uma vez sem duplicar registros.

START TRANSACTION;

SET @menu_id := 35;
SET @tiixhealth_root_id := (
  SELECT id
    FROM wpjy_menus_items
   WHERE menu = @menu_id
     AND slug = 'menu.adm.tiixhealth'
   ORDER BY CASE WHEN area = 'adm' THEN 0 WHEN area IS NULL OR area = '' THEN 1 ELSE 2 END, id DESC
   LIMIT 1
);

SET @kit_cliente_id := (SELECT id FROM wpjy_perm_kits WHERE slug = 'kit.cliente' LIMIT 1);
SET @kit_profissional_id := (SELECT id FROM wpjy_perm_kits WHERE slug = 'kit.profissional' LIMIT 1);
SET @kit_empresa_id := (SELECT id FROM wpjy_perm_kits WHERE slug = 'kit.empresa' LIMIT 1);
SET @kit_assistente_id := (SELECT id FROM wpjy_perm_kits WHERE slug = 'kit.assistente' LIMIT 1);
SET @kit_corretor_id := (SELECT id FROM wpjy_perm_kits WHERE slug = 'kit.corretor' LIMIT 1);

-- 1) Tela
INSERT INTO wpjy_telas (arquivo, nome, descricao, icone, ativa, ajuda)
SELECT 'agendamento-manual.php', 'Agendamento Manual', 'Fluxo guiado para montar agendamentos por paciente, profissional, unidade e taxonomias', 'ti ti-calendar-plus', 1, NULL
WHERE NOT EXISTS (
  SELECT 1
    FROM wpjy_telas
   WHERE arquivo = 'agendamento-manual.php'
);

-- 2) Permissao de tela
INSERT INTO wpjy_permissoes (slug, nome, descricao, id_tela, tipo)
SELECT 'admin.agendamento_manual.view', 'Agendamento Manual', 'Acesso a tela de agendamento manual', t.id, 'tela'
  FROM wpjy_telas t
 WHERE t.arquivo = 'agendamento-manual.php'
   AND NOT EXISTS (
     SELECT 1
       FROM wpjy_permissoes
      WHERE slug = 'admin.agendamento_manual.view'
   );

-- 3) Permissao de menu
INSERT INTO wpjy_permissoes (slug, nome, descricao, id_tela, tipo)
SELECT 'menu.admin.agendamento_manual', 'Menu - Agendamento Manual', 'Exibir item de menu Agendamento Manual em TIIXHEALTH', NULL, 'menu'
WHERE NOT EXISTS (
  SELECT 1
    FROM wpjy_permissoes
   WHERE slug = 'menu.admin.agendamento_manual'
);

-- 4) Item de menu em TIIXHEALTH
INSERT INTO wpjy_menus_items
  (label, link, area, parent, sort, menu, target, icon, slug, tag, parametro_url)
SELECT
  'Agendamento Manual',
  'agendamento-manual',
  'adm',
  @tiixhealth_root_id,
  2,
  @menu_id,
  '_self',
  'lucide:calendar-plus',
  'menu.admin.agendamento_manual',
  'agendamento_manual',
  NULL
WHERE @tiixhealth_root_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1
      FROM wpjy_menus_items
     WHERE menu = @menu_id
       AND link = 'agendamento-manual'
  );

-- 5) Libera legados para grupos operacionais conhecidos
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.agendamento_manual.view', 'menu.admin.agendamento_manual')
 WHERE g.slug IN ('tiix', 'clientes', 'profissionais', 'assistentes-unidade', 'empresa', 'corretora', 'corretor')
   AND NOT EXISTS (
     SELECT 1
       FROM wpjy_grupo_permissao gp
      WHERE gp.id_grupo = g.id
        AND gp.id_permissao = p.id
   );

-- 6) Namespace novo
INSERT INTO wpjy_perm_namespaces (ns, label, descricao, escopo, ativo)
SELECT 'admin.agendamento-manual', 'Admin / Agendamento Manual', 'Fluxo guiado de agendamento manual com contexto por paciente, profissional e unidade', 'contrato', 1
WHERE NOT EXISTS (
  SELECT 1
    FROM wpjy_perm_namespaces
   WHERE ns = 'admin.agendamento-manual'
);

-- 7) Mapeamento tela -> namespace
INSERT INTO wpjy_perm_tela_namespace (padrao_arquivo, ns, sensivel)
SELECT 'pages/admin/agendamento-manual.php', 'admin.agendamento-manual', 0
WHERE NOT EXISTS (
  SELECT 1
    FROM wpjy_perm_tela_namespace
   WHERE padrao_arquivo = 'pages/admin/agendamento-manual.php'
     AND ns = 'admin.agendamento-manual'
);

-- 8) Allow nos kits que podem disparar o fluxo
INSERT INTO wpjy_perm_kit_namespace (kit_id, namespace_id, effect)
SELECT kit_id, namespace_id, 'allow'
  FROM (
    SELECT @kit_cliente_id AS kit_id, n.id AS namespace_id
      FROM wpjy_perm_namespaces n
     WHERE n.ns = 'admin.agendamento-manual'
       AND @kit_cliente_id IS NOT NULL
    UNION ALL
    SELECT @kit_profissional_id AS kit_id, n.id AS namespace_id
      FROM wpjy_perm_namespaces n
     WHERE n.ns = 'admin.agendamento-manual'
       AND @kit_profissional_id IS NOT NULL
    UNION ALL
    SELECT @kit_empresa_id AS kit_id, n.id AS namespace_id
      FROM wpjy_perm_namespaces n
     WHERE n.ns = 'admin.agendamento-manual'
       AND @kit_empresa_id IS NOT NULL
    UNION ALL
    SELECT @kit_assistente_id AS kit_id, n.id AS namespace_id
      FROM wpjy_perm_namespaces n
     WHERE n.ns = 'admin.agendamento-manual'
       AND @kit_assistente_id IS NOT NULL
    UNION ALL
    SELECT @kit_corretor_id AS kit_id, n.id AS namespace_id
      FROM wpjy_perm_namespaces n
     WHERE n.ns = 'admin.agendamento-manual'
       AND @kit_corretor_id IS NOT NULL
  ) q
WHERE NOT EXISTS (
  SELECT 1
    FROM wpjy_perm_kit_namespace kn
   WHERE kn.kit_id = q.kit_id
     AND kn.namespace_id = q.namespace_id
);

-- 9) Tela -> kit
INSERT INTO wpjy_perm_tela_kit_map (id_tela, kit_id, prioridade)
SELECT t.id, q.kit_id, 100
  FROM wpjy_telas t
  JOIN (
    SELECT @kit_cliente_id AS kit_id
    UNION ALL SELECT @kit_profissional_id
    UNION ALL SELECT @kit_empresa_id
    UNION ALL SELECT @kit_assistente_id
    UNION ALL SELECT @kit_corretor_id
  ) q
    ON q.kit_id IS NOT NULL
 WHERE t.arquivo = 'agendamento-manual.php'
   AND NOT EXISTS (
     SELECT 1
       FROM wpjy_perm_tela_kit_map tk
      WHERE tk.id_tela = t.id
        AND tk.kit_id = q.kit_id
   );

COMMIT;

SELECT
  @tiixhealth_root_id AS tiixhealth_root_id,
  @kit_cliente_id AS kit_cliente_id,
  @kit_profissional_id AS kit_profissional_id,
  @kit_empresa_id AS kit_empresa_id,
  @kit_assistente_id AS kit_assistente_id,
  @kit_corretor_id AS kit_corretor_id;

SELECT id, label, link, area, parent, sort, slug
  FROM wpjy_menus_items
 WHERE menu = @menu_id
   AND (link = 'agendamento-manual' OR id = @tiixhealth_root_id OR parent = @tiixhealth_root_id)
 ORDER BY parent, sort, id;

SELECT slug, tipo
  FROM wpjy_permissoes
 WHERE slug IN ('admin.agendamento_manual.view', 'menu.admin.agendamento_manual')
 ORDER BY slug;

SELECT ns
  FROM wpjy_perm_namespaces
 WHERE ns = 'admin.agendamento-manual';
