#!/usr/bin/env node

const { spawnSync } = require('child_process');
const { join } = require('path');

// Parse Node.js version
const version = process.version;
// slice skips the `v` from `v22.0.1`
const versionParts = version.slice(1).split('.');
const majorVersion = parseInt(versionParts[0], 10);
const minorVersion = parseInt(versionParts[1], 10);

const isVersionTooOld = majorVersion < 20 || (majorVersion === 20 && minorVersion < 17);

if (isVersionTooOld) {
  console.error('Error: GitLab Duo CLI requires Node.js version 20.17 or higher.');
  console.error(`Current version: ${version}`);
  console.error('\nPlease upgrade your Node.js installation:');
  console.error('  - Visit https://nodejs.org/ to download the latest version');
  console.error('  - Or use a version manager like nvm, fnm, or asdf');
  process.exit(1);
}

// Run the actual CLI
const cliPath = join(__dirname, 'index.js');
const result = spawnSync(process.execPath, [cliPath, ...process.argv.slice(2)], {
  stdio: 'inherit',
});

process.exit(result.status || 0);
