'use strict';

const REQUIRED_MAJOR = 22;

function checkNodeVersion(versionString) {
  if (typeof versionString !== 'string' || versionString.length === 0) {
    return { ok: false, detected: String(versionString), required: REQUIRED_MAJOR };
  }
  const major = parseInt(versionString.split('.')[0], 10);
  if (Number.isNaN(major) || major < REQUIRED_MAJOR) {
    return { ok: false, detected: versionString, required: REQUIRED_MAJOR };
  }
  return { ok: true, detected: versionString, required: REQUIRED_MAJOR };
}

function formatBanner(detected) {
  return [
    '',
    '[31molam: unsupported Node.js version[0m',
    '',
    `  required: Node ${REQUIRED_MAJOR}+`,
    `  detected: Node ${detected}`,
    '',
    '  Why: the CLI uses node:fs.globSync, which was added in Node 22.0.0.',
    '  Older runtimes fail at module-load before any error handler runs.',
    '',
    '  Fix (pick one):',
    '    mise:    mise use --global node@22',
    '    nvm:     nvm install 22 && nvm use 22',
    '    asdf:    asdf install nodejs 22.20.0 && asdf global nodejs 22.20.0',
    '    brew:    brew install node@22 && brew link --overwrite --force node@22',
    '',
    '  Then re-run: olam bootstrap',
    '',
  ].join('\n');
}

module.exports = { checkNodeVersion, formatBanner, REQUIRED_MAJOR };
