-- QA Sprint 11. Cada consulta debe devolver 0 filas o cero cruces.
SELECT COUNT(*) AS subscriptions_cross_tenant
FROM billing_suscripciones s JOIN org_empresas e ON e.id=s.id_empresa
WHERE s.id_empresa<>e.id;

SELECT COUNT(*) AS events_cross_tenant
FROM billing_suscripcion_eventos ev JOIN billing_suscripciones s ON s.id=ev.id_suscripcion
WHERE ev.id_empresa<>s.id_empresa;

SELECT COUNT(*) AS checkouts_cross_tenant
FROM billing_checkout_sesiones c JOIN billing_suscripciones s ON s.id=c.id_suscripcion
WHERE c.id_empresa<>s.id_empresa;

SELECT COUNT(*) AS duplicate_active_subscriptions
FROM (
  SELECT id_empresa,COUNT(*) n FROM billing_suscripciones
  WHERE estado IN ('trial','activa','past_due','pausada')
  GROUP BY id_empresa HAVING COUNT(*)>1
) x;

SELECT p.codigo,l.codigo,l.valor_decimal,l.ilimitado
FROM billing_planes p JOIN billing_plan_limites l ON l.id_plan=p.id
WHERE p.estado='activo'
ORDER BY p.codigo,l.codigo;
