const { selectLayerFormulaPlan } = require('../src/lib/formula_selectors');

function readArg(name, fallback) {
  const index = process.argv.indexOf(`--${name}`);
  if (index === -1 || index + 1 >= process.argv.length) return fallback;
  return process.argv[index + 1];
}

function readNumber(name, fallback) {
  const value = Number(readArg(name, fallback));
  return Number.isFinite(value) ? value : fallback;
}

function parseLayers(value) {
  return String(value || '1,10,100,1000')
    .split(',')
    .map((item) => Number(item.trim()))
    .filter((item) => Number.isInteger(item) && item > 0);
}

function summarizePlan(plan) {
  const first = plan.steps[0];
  const middle = plan.steps[Math.floor(plan.steps.length / 2)];
  const last = plan.steps[plan.steps.length - 1];
  return {
    layer_no: plan.layer_no,
    step_count: plan.step_count,
    total_formula_count_target: plan.total_formula_count_target,
    average_formula_count_per_step: plan.average_formula_count_per_step,
    total_net_formula_score_target: plan.total_net_formula_score_target,
    checkpoints: [first, middle, last].filter(Boolean),
  };
}

function main() {
  const layers = parseLayers(readArg('layers', '1,10,100,1000'));
  const stepCount = readNumber('steps', 30);
  const seasonSeed = readArg('seed', 'mazex-selector-demo');
  const result = layers.map((layerNo) =>
    summarizePlan(
      selectLayerFormulaPlan({
        layerNo,
        stepCount,
        seasonSeed,
      }),
    ),
  );
  console.log(JSON.stringify(result, null, 2));
}

main();
