// Import libs
import clear from 'clear';

// Import shared types
import Deployment from './types/Deployment';

// Import shared helpers
import validateWizardVersion from './validateWizardVersion';

// Import screens
import chooseDeployment from './screens/chooseDeployment';
import showChooser from './helpers/showChooser';
import showModifyEnvVars from './screens/showModifyEnvVars';
import chooseTarget from './screens/chooseTarget';
import deployNow from './screens/deployNow';
import viewLogs from './screens/viewLogs';
import connectToDatabase from './screens/connectToDatabase';
import relatedClusters from './screens/relatedClusters';
import reviewCVEs from './screens/reviewCVEs';
import setNumInstances from './screens/setNumInstances';

/* eslint-disable no-console */

/**
 * Show the wizard
 * @author Gabe Abrams
 */
const showWizard = async () => {
  // Validate version of dev wizard
  validateWizardVersion();

  // The current chosen deployment
  let deployment: Deployment = chooseDeployment();

  // Endless work loop
  while (true) {
    clear();

    // Ask user to choose a menu option
    const menuOption = showChooser({
      question: 'What do you want to show/modify?',
      options: [
        {
          description: 'Environment Vars',
          tag: 'E',
        },
        {
          description: 'Target of Deployment (version or branch)',
          tag: 'T',
        },
        {
          description: 'Number of Instances',
          tag: 'N',
        },
        {
          description: 'Deploy/Redeploy',
          tag: 'D',
        },
        {
          description: 'Switch Deployment',
          tag: 'S',
        },
        {
          description: 'Log Viewer (App Logs from Today)',
          tag: 'L',
        },
        {
          description: 'Related Clusters',
          tag: 'R',
        },
        {
          description: 'Connect to database',
          tag: 'C',
        },
        {
          description: 'Vulnerability Checker',
          tag: 'V',
        },
      ],
      title: `${deployment.name} | Main Menu`,
    });

    // Handle each case
    if (menuOption.tag === 'E') {
      await showModifyEnvVars(deployment);
    } else if (menuOption.tag === 'T') {
      await chooseTarget(deployment);
    } else if (menuOption.tag === 'D') {
      await deployNow(deployment);
    } else if (menuOption.tag === 'S') {
      deployment = await chooseDeployment();
    } else if (menuOption.tag === 'L') {
      await viewLogs(deployment);
    } else if (menuOption.tag === 'R') {
      await relatedClusters(deployment);
    } else if (menuOption.tag === 'C') {
      await connectToDatabase(deployment);
    } else if (menuOption.tag === 'V') {
      await reviewCVEs();
    } else if (menuOption.tag === 'N') {
      await setNumInstances(deployment);
    }
  }
};

// Start
showWizard();
