const { buildParameterPlan } = require('../src/lib/parameter_selectors');
const { injectParametersWithPlan } = require('../src/lib/parameter_injector');

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 main() {
  const layers = parseLayers(readArg('layers', '1,10,100,1000'));
  const stepCount = readNumber('steps', 30);
  const seed = readArg('seed', 'mazex-parameter-selector-demo');
  const expression = readArg('expr', 'x + y');
  const checkpoints = [1, Math.ceil(stepCount / 2), stepCount];

  const result = layers.map((layerNo) => ({
    layer_no: layerNo,
    step_count: stepCount,
    checkpoints: checkpoints.map((stepNo) => {
      const plan = buildParameterPlan({
        layerNo,
        stepNo,
        stepCount,
        seed,
      });
      const injected = injectParametersWithPlan({
        expression,
        seed: `${seed}:${layerNo}:${stepNo}:inject`,
        parameterPlan: plan,
      });
      return {
        step_no: stepNo,
        ...plan,
        injected_expression: injected.expression,
        actual_injection_score: injected.total_difficulty_score,
      };
    }),
  }));

  console.log(JSON.stringify(result, null, 2));
}

main();
