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

exports.command = 'versions <siteID>'
exports.desc = 'List the versions of a site.'
exports.builder = function (yargs) {

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

  return yargs
    .usage('Usage:\ndarwincli site versions <siteID>')
    .positional('siteID', {
      describe: 'The id of the site to manage',
      type: 'string'
    })
    .help()
    .epilog(epilog)
}

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

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

    //print the version information
    let tdata = [["id", "name", "current", "description", "date"]]
    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].name, data.versions[i].current, data.versions[i].description, data.versions[i].date]
        tdata.push(val)
      //}
    }

    let options = {
      columns: {
        0: {
          width: 4
        },
        1: {
          width: 17
        },
        2: {
          width: 7
        },
        3: {
          width: 25
        },
        4: {
          width: 10
        }
      }
    };

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