const path = require('path')
const childProcess = require('child_process')
const { logger } = require('../../shared/logger')

function executeDynamic (program, command, rawArgs) {
  if (!command) {
    program.outputHelp()
    process.exit(1)
    return
  }

  // construct the full command line manually including flags
  const commandIndex = rawArgs.indexOf(command)
  const forwardedArgs = rawArgs.slice(commandIndex + 1)

  logger.debug(`command: ${command}`)
  logger.debug(`args: ${JSON.stringify(forwardedArgs)}`)

  const binPath = path.join(process.cwd(), 'node_modules', '.bin')
  const newPath = `${binPath}:${process.env.PATH}`
  const env = { ...process.env, PATH: newPath }

  const result = childProcess.spawnSync(`dotenvx-${command}`, forwardedArgs, { stdio: 'inherit', env })
  if (result.error) {
    if (command === 'ops') {
      const ops = ` ___________________________________________________________________
|                                                                   |
|   ██████╗ ██████╗ ███████╗                                        |
|  ██╔═══██╗██╔══██╗██╔════╝                                        |
|  ██║   ██║██████╔╝███████╗                                        |
|  ██║   ██║██╔═══╝ ╚════██║                                        |
|  ╚██████╔╝██║     ███████║                                        |
|   ╚═════╝ ╚═╝     ╚══════╝                                        |
|                                                                   |
|  Learn more at [https://dotenvx.com/ops]                          |
|___________________________________________________________________|`

      console.log(ops)
      console.log('')
      logger.warn(`[INSTALLATION_NEEDED] install dotenvx-${command} to use [dotenvx ${command}] 🛡️`)
      logger.help('⮕  next run: [curl -sfS https://dotenvx.sh/ops | sh]')
      logger.help('⮕  see more: [https://dotenvx.com/ops]')
    } else {
      logger.info(`error: unknown command '${command}'`)
    }
  }

  if (result.status !== 0) {
    process.exit(result.status)
  }
}

module.exports = executeDynamic
