const { handleError } = require('../../error')
const chalk = require('chalk')
const { getApi } = require('../../utils')

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

async function handler (argv) {
  const api = await getApi()
  const imageId = argv.imageId
  try {
    await api.v1DeleteImage(imageId)
    console.log(
      chalk.green(`Successfully deleted image ${imageId}`)
    )
  } catch (err) {
    handleError(err, `Failed to delete ${imageId}`, argv.verbose)
  }
}

module.exports = {
  builder,
  handler,
  command: 'delete <imageId>',
  aliases: ['rm', 'del'],
  describe: 'Delete image'
}
