const { handleError } = require('../../error')
const chalk = require('chalk')
const Client = require('../../clients/Client')
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'
    })
    .option('orientation', {
      alias: 'o',
      type: 'number',
      describe: 'Orientation type'
    })
}

async function handler (argv) {
  const client = new Client(argv)
  const instanceId = argv.instanceId
  const orientation = argv.orientation || 4
  try {
    await client._fetch('POST', `v1/instances/${instanceId}/rotate`, { orientation })
    console.log(
      chalk.green(`Successfully rotated instance ${instanceId}`)
    )
  } catch (err) {
    handleError(err, `Failed to rotate ${instanceId}`, argv.verbose)
  }
}

module.exports = {
  builder,
  handler,
  command: 'rotate <instanceId> [orientation]',
  describe: 'Rotate instance'
}
