// Print the referencia.jpeg as a raster image via backend Node
process.env.PRINTER_HOST = process.env.PRINTER_HOST || '192.168.87.147';
process.env.PRINTER_DEVICE_ID = process.env.PRINTER_DEVICE_ID || 'matricial2';
process.env.PRINTER_TIMEOUT_MS = process.env.PRINTER_TIMEOUT_MS || '60000';

process.env.PORT = process.env.PORT || '3002';
process.env.TEST_BASE_URL = process.env.TEST_BASE_URL || `http://localhost:${process.env.PORT}`;
require('../src/server.js');

const axios = require('axios');

async function sleep(ms) { return new Promise(r => setTimeout(r, ms)); }

async function run() {
  const base = process.env.TEST_BASE_URL || 'http://localhost:3002';
  await sleep(500);
  const widths = [576, 512, 448, 384];
  for (const w of widths) {
    try {
      console.log(`Intentando imprimir referencia.jpeg con width=${w}`);
      const resp = await axios.post(base + '/api/print/image', { path: 'referencia.jpeg', width: w, threshold: 128, mode: 'mono' }, { timeout: 180000 });
      console.log(resp.data);
      if (resp.data && resp.data.ok) {
        console.log('Impresión OK con width=', w);
        return;
      }
    } catch (e) {
      console.error('Error al imprimir con width', w, e.message);
    }
    await sleep(800);
  }
  console.error('No se pudo confirmar OK en ninguna anchura. Revisa el resultado visual o ajusta el umbral.');
}

run().catch((e) => { console.error(e); process.exit(1); });
