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

const tenantRfc = process.argv[2] || 'DESPACHO_MO3NI6U8_B9VGG';
const contribuyenteId = process.argv[3];
const year = process.argv[4] || '2025';
const month = process.argv[5];

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 monthFilter = month ? `AND mes = ${Number(month)}` : '';
  const { rows } = await pool.query(
    `SELECT anio, mes, regimen_fiscal, ingresos_cobrados, egresos_pagados,
            iva_trasladado_total, iva_acreditable, computed_at
       FROM metricas_mensuales
      WHERE contribuyente_id = $1 AND anio = $2 ${monthFilter}
      ORDER BY mes, regimen_fiscal`,
    [contribuyenteId, Number(year)],
  );
  for (const r of rows) console.log(r);
  await prisma.$disconnect();
}
main().catch(async e => { console.error(e); await prisma.$disconnect().catch(() => {}); process.exit(1); });
