const chalk = require('chalk')
const inquirer = require('inquirer')
const { clearConsole } = require('./util/clearConsole')
const shouldBackend = require('./util/shouldBackend')
const loadUserOptions = require('./loadUserOptions')
const { installDeps, installComposerDeps, injectedScripts } = require('./util/installDeps')
const { error, stopSpinner, log, hasYarn, hasPnpm3OrLater, logWithSpinner, execa } = require('@vue/cli-shared-utils')

async function start (options = {}, context = process.cwd()) {
  const isTestOrDebug = process.env.CONTROLLA_CLI_TEST || process.env.CONTROLLA_CLI_DEBUG
  const packageManagerChoices = []

  await clearConsole(true)
  packageManagerChoices.push({
    name: 'Use NPM',
    value: 'npm',
    short: 'NPM'
  })

  // ask for packageManager once
  if (hasYarn() || hasPnpm3OrLater()) {
    if (hasYarn()) {
      packageManagerChoices.push({
        name: 'Use Yarn',
        value: 'yarn',
        short: 'Yarn'
      })
    }

    if (hasPnpm3OrLater()) {
      packageManagerChoices.push({
        name: 'Use PNPM',
        value: 'pnpm',
        short: 'PNPM'
      })
    }
  }

  const packageManager = options.packageManager || await inquirer.prompt([
    {
      name: 'packageManager',
      type: 'list',
      message: 'Pick the package manager to use when installing dependencies:',
      choices: packageManagerChoices
    }
  ]).then(list => list.packageManager)

  const userOptions = await loadUserOptions(context)

  const isShouldBackend = await shouldBackend(userOptions.type)

  const frontendPath = isShouldBackend ? userOptions.type === 'saas' ? `${context}/central` : `${context}/frontend` : context

  // if (isShouldBackend) {
  //   log()
  //   logWithSpinner('🚧', 'Creating virtualhost...')
  //   //this.emit('creation', { event: 'creating-virtualhost' })
  //   await execa(`sudo sed -i -e "s/127.0.0.1\tlocalhost/127.0.0.1\tlocalhost\n127.0.0.1\t${userOptions.siteName}.test/g" /etc/hosts`, { cwd: this.context, shell: true}).then(_ => {
  //     stopSpinner()
  //   })
  // }

  // install plugins
  stopSpinner()
  log('🚀  Installing CLI plugins. This might take a while...')
  log()
  if (isTestOrDebug) {
    // in development, avoid installation process
    await require('./util/setupDevProject')(context)
  } else {
    await installDeps(frontendPath, packageManager, options.registry)
  }

  if (userOptions.type === 'saas') {
    await installDeps(`${context}/tenant`, packageManager, options.registry)
  }

  // install composer
  if (isShouldBackend) {
    log()
    log(`🚀  Installing additional dependencies...`)
    log()
    await installDeps(context, packageManager, options.registry)
  }

  // install composer
  if (isShouldBackend) {
    log()
    log(`🚀  Installing Composer dependencies...`)
    log()
    await installComposerDeps(context, 'composer')
  }

  // Injected additional scripts
  if (isShouldBackend) {
    log()
    log(`📦  Injected additional scripts...`)
    log()
    await injectedScripts(context, 'composer')
  }

  // run complete cbs if any (injected by generators)
  log()
  logWithSpinner('⚓', `Running completion hooks...`)
  await execa("find . -name '.controllaignore' -exec rename 's/controlla/git/' -- {} +", { cwd: this.context, shell: true })

  stopSpinner()
  log()
  log(`🎉  Successfully started project ${chalk.yellow(userOptions.siteName)}.`)
  log(
    `👉  Get started with the following commands:\n\n` +
    chalk.cyan(` ${chalk.gray('$')} ${packageManager === 'yarn' ? 'yarn serve' : packageManager === 'pnpm' ? 'pnpm run serve' : 'npm run serve'}`)
  )
  log()
}

module.exports = (...args) => {
  return start(...args).catch(err => {
    stopSpinner(false) // do not persist
    error(err)
    if (!process.env.CONTROLLA_CLI_TEST) {
      process.exit(1)
    }
  })
}
