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

async function main() {
  const t = await prisma.tenant.findFirst({ where: { rfc: 'DESPACHO_MO7JE8BZ_VDOPR' } });
  if (!t) { console.log('Zorro tenant no encontrado'); return; }
  const pool = await tenantDb.getPool(t.id, t.databaseName);

  console.log('--- E PUE EMITIDAS (cualquier fecha) ---');
  const emit = await pool.query(`
    SELECT EXTRACT(year FROM fecha_emision) as anio,
           regimen_fiscal_emisor, count(*) as n,
           SUM(total_mxn)::numeric(14,2) as total
    FROM cfdis
    WHERE tipo_comprobante = 'E' AND metodo_pago = 'PUE'
      AND status NOT IN ('Cancelado','0')
    GROUP BY 1, 2 ORDER BY 1 DESC, 2
  `);
  console.table(emit.rows);

  console.log('\n--- E PUE RECIBIDAS (cualquier fecha) ---');
  const rec = await pool.query(`
    SELECT EXTRACT(year FROM fecha_emision) as anio,
           regimen_fiscal_receptor, count(*) as n,
           SUM(total_mxn)::numeric(14,2) as total
    FROM cfdis
    WHERE tipo_comprobante = 'E' AND metodo_pago = 'PUE'
      AND status NOT IN ('Cancelado','0')
    GROUP BY 1, 2 ORDER BY 1 DESC, 2
  `);
  console.table(rec.rows);

  await prisma.$disconnect();
}
main().catch(e => { console.error(e); process.exit(1); });
