process.env.METRICAS_BYPASS_CACHE = '1';
import { prisma, tenantDb } from '../src/config/database.js';
import { calcularIngresosPorRegimen } from '../src/services/dashboard.service.js';

const tenantRfc = process.argv[2] || 'DESPACHO_MO3NI6U8_B9VGG';
const contribuyenteId = process.argv[3] || 'b3761db6-0b8d-4251-8078-4ddc31e9c75b';
const yearMonth = process.argv[4] || '2025-05';

async function main() {
  const tenant = await prisma.tenant.findFirst({ where: { rfc: tenantRfc }, select: { id: true, databaseName: true } });
  if (!tenant) return;
  const pool = await tenantDb.getPool(tenant.id, tenant.databaseName);

  const [anio, mes] = yearMonth.split('-').map(Number);
  const lastDay = new Date(anio, mes, 0).getDate();
  const fi = `${yearMonth}-01`;
  const ff = `${yearMonth}-${String(lastDay).padStart(2, '0')}`;

  const r = await calcularIngresosPorRegimen(pool, tenant.id, fi, ff, undefined, undefined, false, contribuyenteId);
  console.log(`\n=== Ingresos ${yearMonth} contrib=${contribuyenteId} (BYPASS_CACHE=1) ===`);
  console.log(`Total: ${r.total.toFixed(2)}`);
  for (const p of r.porRegimen) {
    console.log(`  ${p.regimenClave} (${p.regimenDescripcion}): ${p.monto.toFixed(2)}`);
  }
  await prisma.$disconnect();
}
main().catch(async e => { console.error(e); await prisma.$disconnect().catch(() => {}); process.exit(1); });
