const Client = require('../../clients/Client')
const { handleError } = require('../../error')
const { MAST_API_BASE_PATH } = require('./constants')

/**
 * Get assessments for an instance via corellium-mast API.
 *
 * @param yargs
 * @returns {Promise<void>}
 */
async function builder (yargs) {
  yargs
    .option('instance', {
      alias: 'i',
      type: 'string',
      demandOption: true,
      describe: 'virtual device id',
      default: process.env.INSTANCE
    })
    .option('verbose', {
      alias: 'v',
      type: 'boolean',
      describe: 'console will receive verbose error output',
      default: false
    })
}

async function handler (argv) {
  const { instance, verbose } = argv
  const client = new Client(argv)
  try {
    const res = await client._fetch('GET', `${MAST_API_BASE_PATH}/${instance}/instances/${instance}/assessments`)
    console.log(JSON.stringify(res))
  } catch (error) {
    handleError(error, 'get-assessments failed', verbose)
  }
}

module.exports = {
  builder,
  handler,
  command: 'get-assessments',
  describe: 'Get all MATRIX assessments for a given instance'
}
