-- QA Sprint 16: workspace, tareas y aislamiento multiempresa.
SELECT 'tareas_huerfanas_o_cruzadas' AS prueba, COUNT(*) AS total
FROM gene_tareas t
LEFT JOIN gene_casos c ON c.id=t.id_caso AND c.id_empresa=t.id_empresa
LEFT JOIN org_empresas e ON e.id=t.id_empresa
WHERE c.id IS NULL OR e.id IS NULL;

SELECT 'tareas_preguntas_cruzadas' AS prueba, COUNT(*) AS total
FROM gene_tareas t
JOIN gene_preguntas q ON q.id=t.id_pregunta
WHERE t.id_pregunta IS NOT NULL
  AND (q.id_empresa<>t.id_empresa OR q.id_caso<>t.id_caso);

SELECT 'tareas_responsables_fuera_empresa' AS prueba, COUNT(*) AS total
FROM gene_tareas t
LEFT JOIN org_membresias m ON m.id_empresa=t.id_empresa AND m.id_usuario=t.responsable_id AND m.estado='activo'
WHERE t.responsable_id IS NOT NULL AND m.id IS NULL;

SELECT 'casos_objetivo_cruzado' AS prueba, COUNT(*) AS total
FROM gene_personas p
JOIN gene_casos c ON c.id=p.id_caso
WHERE p.id_empresa<>c.id_empresa;

SELECT 'ejecuciones_cruzadas' AS prueba, COUNT(*) AS total
FROM gene_busqueda_ejecuciones x
JOIN gene_casos c ON c.id=x.id_caso
WHERE x.id_empresa<>c.id_empresa;

SELECT 'documentos_cruzados' AS prueba, COUNT(*) AS total
FROM gene_documentos d
JOIN gene_casos c ON c.id=d.id_caso
WHERE d.id_empresa<>c.id_empresa;
