const { getArgValues } = require('./helpers/get-arg-values');
const { getExistingStepNums } = require('./helpers/get-existing-step-nums');
const { getProjectPath } = require('./helpers/get-project-path');
const { createStepFile, reorderSteps } = require('./utils');

const anyStepExists = (steps, stepsToFind) =>
  stepsToFind.some(num => steps.includes(num));

const projectPath = getProjectPath();
const args = getArgValues(process.argv);

let { num, start } = args;
if (!start) {
  throw `No steps created. start arg val must be specified.`;
}
if (!num) {
  throw `No steps created. num arg val must be specified.`;
}
num = parseInt(num, 10);
const stepStart = parseInt(start, 10);

if (num < 1 || num > 100) {
  throw `No steps created. arg 'num' must be between 1 and 100 inclusive`;
}

const maxStepNum = stepStart + num - 1;

const existingSteps = getExistingStepNums(projectPath);

if (anyStepExists(existingSteps, [start, maxStepNum])) {
  throw `Step not created. At least one of the steps specified between ${start} and ${maxStepNum} already exists.`;
}

for (let stepNum = stepStart; stepNum <= maxStepNum; stepNum++) {
  createStepFile({ stepNum, projectPath });
}
console.log(`Sucessfully added ${num} steps`);
reorderSteps();