import { prisma, tenantDb } from '../src/config/database.js';

async function main() {
  const tenants = await prisma.tenant.findMany({ select: { id: true, rfc: true, databaseName: true } });
  for (const t of tenants) {
    let pool;
    try { pool = await tenantDb.getPool(t.id, t.databaseName); } catch { continue; }
    console.log(`\n=== ${t.rfc} ===`);
    const { rows } = await pool.query(`
      SELECT tipo_comprobante, metodo_pago, COUNT(*)::int AS cnt
        FROM cfdis
       WHERE cfdi_tipo_relacion = '07' AND status NOT IN ('Cancelado','0')
       GROUP BY tipo_comprobante, metodo_pago
       ORDER BY cnt DESC
    `);
    for (const r of rows) {
      console.log(`  ${r.tipo_comprobante}/${r.metodo_pago || 'null'}: ${r.cnt}`);
    }
  }
  await prisma.$disconnect();
}
main().catch(async e => { console.error(e); await prisma.$disconnect().catch(() => {}); process.exit(1); });
