// ?e require('mongoose').connections[0].db.command({ buildInfo: 1 })

import { Message } from 'eris';
import mongoose from 'mongoose';
import { Client, Command, RichEmbed } from '../class';
import { version as erisVersion } from '../../node_modules/eris/package.json';
import { version as mongooseVersion } from '../../node_modules/mongoose/package.json';
import { version as ariVersion } from '../../node_modules/ari-client/package.json';
import { version as amiVersion } from '../../node_modules/asterisk-manager/package.json';
import { version as nodeMailerVersion } from '../../node_modules/nodemailer/package.json';
import { version as ioredisVersion } from '../../node_modules/ioredis/package.json';
import { version as stripeVersion } from '../../node_modules/stripe/package.json';
import { version as cronVersion } from '../../node_modules/cron/package.json';
import { version as ttsVersion } from '../../node_modules/@google-cloud/text-to-speech/package.json';
import { version as bullVersion } from '../../node_modules/bull/package.json';

export default class SysInfo extends Command {
  constructor(client: Client) {
    super(client);
    this.name = 'sysinfo';
    this.description = 'Information about the various services/system we use to run.';
    this.usage = 'sysinfo';
    this.permissions = 0;
    this.enabled = true;
  }

  public async run(message: Message) {
    try {
      const embed = new RichEmbed();
      embed.setTitle('System & Service Information');
      embed.setDescription('__Format of Services__\n- {Server/Service Name} + {various server stats} | {Node Library Used for Communication w/ Server or Service}');
      const mongoBuild = await mongoose.connections[0].db.command({ buildInfo: 1 });
      const mongoDatabase: {
        collections: number,
        objects: number,
        dataSize: number,
      } = await mongoose.connections[0].db.command({ dbStats: 1 });
      const asteriskInformation = await this.client.util.pbx.ari.asterisk.getInfo();
      const redisVersion = await this.client.util.exec('redis-cli info | grep "redis_version"');
      embed.addField('Database', `- [MongoDB v${mongoBuild.version}](https://www.mongodb.com/) + Documents: ${mongoDatabase.objects} | [Mongoose ODM v${mongooseVersion}](https://github.com/Automattic/mongoose)\n- CR Local Storage w/ GZIP Compression | Internal\n- [Redis v${redisVersion.split(':')[1]}](https://redis.io/) | [IORedis v${ioredisVersion}](https://github.com/luin/ioredis)`, true);
      embed.addField('Telephony/PBX', `- [Asterisk v${asteriskInformation.system.version}](https://www.asterisk.org/) | [Asterisk ARI Node.js Client v${ariVersion}](https://github.com/asterisk/node-ari-client) & [Asterisk AMI Node.js Client v${amiVersion}](https://github.com/danjenkins/node-asterisk-ami)`, true);
      embed.addField('Email', `- [Postfix SMTP v3.1.12](http://www.postfix.org/) | [Nodemailer v${nodeMailerVersion}](https://github.com/nodemailer/nodemailer)`, true);
      embed.addField('Discord', `- N/A | [Eris v${erisVersion}](https://github.com/abalabahaha/eris)`, true);
      embed.addField('Payments', `- [Stripe API](https://stripe.com/) | [Stripe Node.js Client v${stripeVersion}](https://github.com/stripe/stripe-node)`, true);
      embed.addField('Scheduling', `- [Cron](https://systemd.io/) | [Cron Scheduling Node.js v${cronVersion}](https://github.com/node-cron/node-cron)\n- Internal Queue | [Bull v${bullVersion}](https://github.com/OptimalBits/bull) w/ IORedis`, true);
      embed.addField('Audio & Voice', `- [Google Cloud ML](https://cloud.google.com/text-to-speech) | [Google Cloud Node.js Text to Speech Synthesizer v${ttsVersion}](https://github.com/googleapis/nodejs-text-to-speech)\n - [FFMPEG](https://ffmpeg.org/) | Internal`, true);
      embed.setFooter(this.client.user.username, this.client.user.avatarURL);
      embed.setTimestamp();
      message.channel.createMessage({ embed });
    } catch (err) {
      this.client.util.handleError(err, message, this);
    }
  }
}
