import API from '../../API.js';
const chalk = require("chalk");
import {table} from 'table';

exports.command = 'rollback <firmwareID> <versionID>'
exports.desc = 'Rollback your firmware to the provided version.'
exports.builder = function (yargs) {

  const epilog = chalk.dim("Darwin Command Line Interface\nCopyright 2026 Darwin Inc. All Rights Reserved.\n")

  return yargs
    .usage('Usage:\ndarwincli firmware rollback <firmwareID> <versionID>')
    .positional('firmwareID', {
      describe: 'The id of the firmware to rollback',
      type: 'string'
    })
    .positional('versionID', {
      describe: 'The id of the firmware version to make active',
      type: 'string'
    })
    .help()
    .epilog(epilog)
}

exports.handler = function (argv) {
  API.clientCallDarwinAPI("PUT", "firmwareUpload", {
    "firmwareID": argv.firmwareID,
    "versionID": argv.versionID,
  }, [], (result, data) => {
    if (!result) {
      console.log(chalk.bold.red(data.error || data))
      return
    }
    console.log(chalk.blue.bold("Your firmware has been rolled back."))
  }, process.env.DARWIN_CLIENT_ID, process.env.DARWIN_CLIENT_SECRET)
}
