// Smoke test for the FreeScout CS report service, bypassing HTTP auth.
// Run on the server:  cd /var/www/inventory/dashboard && node scripts/test-freescout-report.mjs [days]

import { config as loadEnv } from 'dotenv';
import { createPool } from '../../shared/db/pg.js';
import { FreescoutService } from '../services/freescout/freescout.service.js';

loadEnv({ path: new URL('../.env', import.meta.url).pathname });

const days = Number(process.argv[2]) || 30;

const freescoutPool = createPool('FREESCOUT_DB', {
  user: process.env.FREESCOUT_DB_USERNAME,
  database: process.env.FREESCOUT_DB_DATABASE,
  max: 2,
});
const phonePool = process.env.ACOT_PHONE_DB_HOST
  ? createPool('ACOT_PHONE_DB', {
      user: process.env.ACOT_PHONE_DB_USERNAME,
      database: process.env.ACOT_PHONE_DB_DATABASE,
      max: 2,
    })
  : null;

// Stub redis in "end" status so the service skips caching entirely.
const redisStub = { status: 'end' };

const service = new FreescoutService(redisStub, freescoutPool, phonePool);
const t0 = Date.now();
try {
  const report = await service.getReport(days);
  console.log(JSON.stringify(report, null, 2));
  console.error(`\nOK — ${days}d report in ${Date.now() - t0}ms`);
} finally {
  await freescoutPool.end();
  await phonePool?.end();
}
