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

exports.command = 'list'
exports.desc = 'List the firmware for which you have access.'
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 list')
    .help()
    .epilog(epilog)
}

exports.handler = function (argv) {
  API.clientCallDarwinAPI("GET", "firmware", {}, [], (result, data) => {
    if (!result) {
      return
    }
    console.log(chalk("You have access to the following firmware.\n" + chalk.bold.blue("Use a firmware's 'id' when performing an operation on it.")))

    let tdata = [["id", "name", "description"]]
    for (let i = 0; i < data.length; i = i + 1) {
      let val = [data[i].id, data[i].name, data[i].description]
      tdata.push(val)
    }

    let options = {
      columns: {
        0: {
          width: 5
        },
        1: {
          width: 15
        },
        3: {
          width: 20
        }
      }
    };

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