require('../config/loadEnv')();

const fs = require('fs');
const path = require('path');
const rewardHttpSmokeService = require('../services/rewardHttpSmokeService');

function getArg(name, fallback = '') {
  const prefix = `--${name}=`;
  const found = process.argv.find((arg) => arg.startsWith(prefix));
  return found ? found.slice(prefix.length) : fallback;
}

async function main() {
  const baseUrl = getArg('base-url', process.env.REWARD_CLIENT_BASE_URL || 'http://127.0.0.1:4317');
  const services = getArg('services', '')
    .split(',')
    .map((service) => service.trim())
    .filter(Boolean);
  const result = await rewardHttpSmokeService.runDryRunSmoke({ baseUrl, services });
  const outputPath = path.join(__dirname, '..', 'data', 'reward-http-dry-run-smoke.json');
  fs.writeFileSync(outputPath, JSON.stringify(result, null, 2));
  console.log(JSON.stringify(result, null, 2));
  if (!result.ok) process.exitCode = 1;
}

main().catch((err) => {
  console.error(err.message);
  process.exitCode = 1;
});
