const { getErrorMessage } = require('../../error')
const asciiArt = require('ascii-art')
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(
      asciiArt.style(`Successfully deleted image ${imageId}`, 'green', true)
    )
  } catch (error) {
    const errorMessage = getErrorMessage(error)
    console.error(`Failed to delete ${imageId}: ${errorMessage}`)

    if (argv.detailedErrors) {
      console.log(error)
    }
  }
}

module.exports = {
  builder,
  handler,
  command: 'delete <imageId>',
  describe: 'Delete image'
}
