const Discord = require('discord.js')
const moment = require('moment')
const ScheduleStats = require('../structs/db/ScheduleStats.js')

module.exports = async (message) => {
  const results = await ScheduleStats.getAll()
  const bot = message.client
  const scheduleStats = results.find(r => r._id === 'default')
  if (!scheduleStats) {
    return message.channel.send('More time is needed to gather enough information. Try again later.')
  }
  if (scheduleStats.feeds === 0) {
    return message.channel.send('No feeds found for any data.')
  }
  const sizeFetches = await bot.shard.fetchClientValues('guilds.cache.size')

  const aggregated = {
    guilds: sizeFetches.reduce((prev, val) => prev + val, 0),
    feeds: scheduleStats.feeds,
    cycleTime: scheduleStats.cycleTime,
    cycleFails: scheduleStats.cycleFails,
    cycleURLs: scheduleStats.cycleURLs,
    lastUpdated: new Date(scheduleStats.lastUpdated)
  }

  const visual = new Discord.MessageEmbed()
    .setAuthor('Basic Performance Stats')
    .setDescription(`
**Unique Feeds** - Number of unique feed links (duplicate links are not counted).
**Total Feeds** - Number of total feeds.
**Cycle Duration** - The time it takes to process all the unique feed links.
**Cycle Failures** - The number of failures out of the number of unique feeds per cycle. 
**Success Rate** - The rate at which unique links connect successfully per cycle. A high rate is necessary to ensure that all feeds are fetched.\n\u200b`)

  const guilds = `${aggregated.guilds}`
  const feeds = `${aggregated.feeds}`
  const cycleURLs = `${aggregated.cycleURLs}`
  const cycleFails = `${aggregated.cycleFails.toFixed(2)}`
  const cycleTime = `${aggregated.cycleTime.toFixed(2)}s`
  const successRate = `${((1 - aggregated.cycleFails / aggregated.cycleURLs) * 100).toFixed(2)}%`

  visual
    .addField('Servers', guilds, true)
    .addField('Shard Count', bot.shard.count, true)
    .addField('\u200b', '\u200b', true)

  let diff = moment.duration(moment().diff(moment(aggregated.lastUpdated)))
  if (diff.asMinutes() < 1) {
    diff = `Updated ${diff.asSeconds().toFixed(2)} seconds ago`
  } else {
    diff = `Updated ${diff.asMinutes().toFixed(2)} minutes ago`
  }

  visual
    .addField('Unique Feeds', cycleURLs, true)
    .addField('Total Feeds', feeds, true)
    .addField('Cycle Duration', cycleTime, true)
    .addField('Cycle Failures', cycleFails, true)
    .addField('Success Rate', successRate, true)
    .addField('\u200b', '\u200b', true)
    .setFooter(diff)

  await message.channel.send('', visual)
}

@@ -64,16 +64,74 @@
 oment')%0A
+const GeneralStats = require('../models/GeneralStats.js')%0A
 const Sc
@@ -237,45 +237,141 @@
 nst 
-results = await ScheduleStats.getAll(
+%5BallScheduleStats, allGeneralStats%5D = await Promise.all(%5B%0A    ScheduleStats.getAll(),%0A    GeneralStats.Model.find().lean().exec()%0A  %5D
 )%0A  
@@ -425,13 +425,22 @@
 s = 
-resul
+allScheduleSta
 ts.f
@@ -1071,16 +1071,208 @@
 Updated)
+,%0A    articlesSent: allGeneralStats.find(doc =%3E doc._id === GeneralStats.TYPES.ARTICLES_SENT),%0A    articlesBlocked: allGeneralStats.find(doc =%3E doc._id === GeneralStats.TYPES.ARTICLES_BLOCKED)
 %0A  %7D%0A%0A  
@@ -1339,20 +1339,8 @@
 sic 
-Performance 
 Stat
@@ -2149,16 +2149,189 @@
 ed(2)%7D%25%60
+%0A  const articlesSent = aggregated.articlesSent ? aggregated.articlesSent.data : 0%0A  const articlesBlocked = aggregated.articlesBlocked ? aggregated.articlesBlocked.data : 0
 %0A%0A  visu
@@ -3010,23 +3010,167 @@
    .
-setFooter(diff)
+addField('Articles Sent', articlesSent, true)%0A    .setFooter(diff)%0A%0A  if (articlesBlocked) %7B%0A    visual.addField('Articles Blocked', articlesBlocked, true)%0A  %7D
 %0A%0A  

Add articles sent/blocked in stats command