SELECT 'OK_MIGRATION' AS resultado
WHERE EXISTS (SELECT 1 FROM sys_migrations WHERE migration='012_sprint9_ocr_ai_human_review.sql');

SELECT 'OK_TABLES' AS resultado
WHERE (SELECT COUNT(*) FROM information_schema.tables WHERE table_schema=DATABASE() AND table_name IN ('ia_ejecuciones','ia_resultados','ia_consumos_diarios','ia_revision_eventos'))=4;

SELECT 'OK_PERMISSIONS' AS resultado
WHERE (SELECT COUNT(*) FROM org_permisos WHERE codigo IN ('ai.process','ai.review','ai.manage'))=3;

SELECT 'OK_LOCAL_PROVIDERS' AS resultado
WHERE (SELECT COUNT(*) FROM ia_proveedores WHERE codigo IN ('local_rules','local_command','human') AND activo=1)=3;

SELECT 'OK_HUMAN_REVIEW' AS resultado
WHERE NOT EXISTS (
  SELECT 1 FROM ia_resultados r
  WHERE r.estado_revision IN ('aceptado','corregido','rechazado')
    AND (r.revisado_por IS NULL OR r.revisado_en IS NULL)
);

SELECT 'OK_TENANT_EXECUTIONS' AS resultado
WHERE NOT EXISTS (
  SELECT 1 FROM ia_ejecuciones e
  JOIN gene_casos c ON c.id=e.id_caso
  WHERE e.id_empresa<>c.id_empresa
);

SELECT 'OK_TENANT_RESULTS' AS resultado
WHERE NOT EXISTS (
  SELECT 1 FROM ia_resultados r
  JOIN ia_ejecuciones e ON e.id=r.id_ejecucion
  WHERE r.id_empresa<>e.id_empresa
);
