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

exports.command = 'versions <firmwareID>'
exports.desc = 'List the versions of a firmware.'
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 versions <firmwareID>')
    .positional('firmwareID', {
      describe: 'The id of the firmware to manage',
      type: 'string'
    })
    .help()
    .epilog(epilog)
}

exports.handler = function (argv) {
  API.clientCallDarwinAPI("GET", "firmware/" + argv.firmwareID, {}, [], (result, data) => {
    if (!result) {
      return
    }

    //print some site information
    console.log(chalk.blue(data.firmware.name))
    console.log(chalk.bold(data.firmware.description))
    console.log(chalk.white.italic("Versions:"))

    //print the version information
    let tdata = [["id", "version", "active", "comments", "url"]]
    for (let i = 0; i < data.versions.length; i = i + 1) {
      //if (data[i].valid == '1') {
        let val = [data.versions[i].id, data.versions[i].version, data.versions[i].active, data.versions[i].comments, data.versions[i].url]
        tdata.push(val)
      //}
    }

    let options = {
      columns: {
        0: {
          width: 4
        },
        1: {
          width: 8
        },
        2: {
          width: 8
        },
        3: {
          width: 25
        },
        4: {
          width: 20
        }
      }
    };

    let output = table(tdata, options);
    console.log(output);
  }, process.env.DARWIN_CLIENT_ID, process.env.DARWIN_CLIENT_SECRET)
}
