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

const TENANT_RFC = 'DESPACHO_MO3NI6U8_B9VGG';

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

  // Get the full latest Facturapi CFDI with ALL fields
  const { rows } = await pool.query(
    `SELECT * FROM cfdis
      WHERE source = 'facturapi'
      ORDER BY fecha_emision DESC
      LIMIT 1`,
  );
  if (rows.length === 0) {
    console.log('No hay CFDIs Facturapi');
    return;
  }

  const r = rows[0];
  console.log('UUID:', r.uuid);
  console.log('');
  console.log('Campos relevantes de emisor/receptor:');
  const keys = Object.keys(r).sort();
  for (const k of keys) {
    if (/emisor|receptor|regimen|contribuyente|type|tipo|facturapi|uso_cfdi|forma|metodo|total|iva|lugar|fecha|status|version|uuid|id|source|serie|folio|xml_original/i.test(k)) {
      const v = r[k];
      const val = typeof v === 'string' && v.length > 200 ? v.substring(0, 200) + '…' : v;
      console.log(`  ${k} = ${val instanceof Date ? val.toISOString() : String(val).substring(0, 200)}`);
    }
  }

  await prisma.$disconnect();
}

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