#!/usr/bin/env node
const argv = require("yargs").argv;
const fetchInternetArchiveMetadata = require("./tasks/fetchInternetArchiveMetadata");
const ensureInternetArchiveItemsIndexByMd5 = require("./tasks/ensureInternetArchiveItemsIndexByMd5");
const logger = require("./logger");
const DiscordWinstonTransport = require("./DiscordWinstonTransport");
const Skins = require("./data/skins");
const db = require("./db");
const Discord = require("discord.js");
const tweet = require("./tasks/tweet");

async function main() {
  const client = new Discord.Client();
  // The Winston transport logs in the client.
  await DiscordWinstonTransport.addToLogger(client, logger);

  switch (argv._[0]) {
    case "tweet":
      await tweet(client);
      break;
    case "fetch-metadata":
      console.log("Going to download metadata from the Internet Archive");
      await fetchInternetArchiveMetadata();
      break;

    case "ensure-md5s":
      await ensureInternetArchiveItemsIndexByMd5();
      break;
    case "metadata": {
      const hash = argv._[1];
      console.log(await Skins.getInternetArchiveUrl(hash));
      break;
    }
    case "reconcile": {
      await Skins.reconcile();
      break;
    }
    case "skin": {
      const hash = argv._[1];
      logger.info({ hash });
      console.log(await Skins.getSkinByMd5(hash));
      break;
    }
    default:
      console.log(`Unknown command ${argv._[0]}`);
  }
  logger.close();
  client.destroy();
  await db.close();
}

main();
