import clear from 'clear';

// Import helpers
import getCurrentEvenLTSNodeVersion from './getCurrentEvenLTSNodeVersion';
import getMajorNodeVersionInDockerfile from './getMajorNodeVersionInDockerfile';
import print from './print';

/**
 * Validate the Node version of the project, display results or warning
 * @author Gabe Abrams
 */
const validateNodeVersion = async (): Promise<void> => {
  // Find current even LTS version of Node.js
  const currentEvenMajorLTSVersion = await getCurrentEvenLTSNodeVersion();
  if (!currentEvenMajorLTSVersion) {
    clear();
    print.title('Node Version Error');
    console.log('');
    console.log('Unable to find the latest even LTS version of Node.js.');
    console.log('Please check your internet connection and try again.');
    console.log('');
    print.enterToContinue();
    return;
  }

  // Find the version of node in the Dockerfile
  const nodeMajorVersion = await getMajorNodeVersionInDockerfile();
  if (!nodeMajorVersion) {
    clear();
    print.title('Node Version Not Found');
    console.log('');
    console.log('Unable to find the version of Node.js in the Dockerfile.');
    console.log('Please check the Dockerfile and try again.');
    console.log('');
    print.enterToContinue();
    return;
  }
  if (nodeMajorVersion !== currentEvenMajorLTSVersion) {
    clear();
    print.title('!!! Node OUT OF DATE !!!');
    console.log('');
    console.log(`The version of Node.js in the Dockerfile is ${nodeMajorVersion}.`);
    console.log(`The latest even LTS version of Node.js is ${currentEvenMajorLTSVersion}.`);
    console.log('');
    console.log('Please update the Dockerfile soon:');
    console.log(`FROM node:${currentEvenMajorLTSVersion}-alpine`);
    console.log('');
    print.enterToContinue();
  } else {
    clear();
    print.title('✓✓✓ Node Version Check Passed ✓✓✓');
    console.log('');
    console.log(`The version of Node.js in the Dockerfile is ${nodeMajorVersion}.`);
    console.log(`The latest even LTS version of Node.js is ${currentEvenMajorLTSVersion}.`);
    console.log('');
    console.log('No action needed.');
    console.log('');
    print.enterToContinue();
  }
};

export default validateNodeVersion;
