const Client = require('../../clients/Client')
const { handleError } = require('../../error')
const log = require('../../logging')
const { getMastApiBasePath } = require('./api-base-path')

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

async function handler (argv) {
  const { assessment, instance, verbose } = argv
  const client = new Client(argv)
  try {
    const API_BASE_PATH = await getMastApiBasePath(client, instance)
    await client._fetch('DELETE', `${API_BASE_PATH}/${instance}/assessments/${assessment}`)
    log.info('Assessment deleted.')
  } catch (error) {
    handleError(error, 'delete-assessment failed', verbose)
  }
}

module.exports = {
  builder,
  handler,
  command: 'delete-assessment',
  describe: 'Delete MATRIX assessment'
}
