const log = require('../../../logging')
const InstanceCLI = require('../../../clients/Instance')

async function builder (yargs) {
  yargs
    .positional('instanceId', {
      type: 'string',
      describe: 'Instance id',
      demandOption: true
    })
    .option('verbose', {
      alias: 'v',
      type: 'boolean',
      describe: 'Console will receive verbose error output',
      demandOption: false
    })
}

async function handler (argv) {
  try {
    const instance = new InstanceCLI(argv)
    await instance.disableMonitoring()
    log.info('Success')
  } catch (e) {
    if (e.response) {
      log.error(`${e.response.status} ${e.response.data.error || e.response.statusText}`)
    } else {
      if (e.message) {
        log.error(e.message)
      } else {
        log.error('Urecognized error, use --verbose for details')
      }
    }

    if (argv.verbose) { log.error(JSON.stringify(e)) }
    process.exit(1)
  }
}

module.exports = {
  builder,
  handler,
  command: 'disable <instanceId> [--verbose]',
  describe: 'Disable network monitoring'
}
