const AgentCLI = require('./Agent')
const { validateNonEmpty } = require('../../utils')
const { getErrorMessage } = require('../../error')
const log = require('../../logging')

async function builder (yargs) {
  yargs.option('verbose', {
    alias: 'v',
    type: 'boolean',
    describe: 'Console will receive verbose error output'
  }).option('project', {
    type: 'input',
    describe: 'Project ID',
    demandOption: true,
    string: true,
    validateNonEmpty
  }).option('instance', {
    type: 'input',
    describe: 'Instance ID',
    demandOption: true,
    string: true,
    validateNonEmpty
  })
}

async function handler (argv) {
  try {
    const agent = new AgentCLI(argv)
    const status = await agent.ready()
    log.plain(JSON.stringify(status))
  } catch (err) {
    const errorMessage = getErrorMessage(err)
    log.error(`Query agent ready status failed: ${errorMessage}`)
    if (argv.detailedErrors) { log.error(err) }
  }
}

module.exports = {
  builder,
  handler,
  command: 'ready',
  describe: 'Query agent ready status'
}
