'use strict'

const { prettyJson, stripKeyword } = require('./bot_tools')

module.exports = (controller) => {
  // temporarily necessary until botkit removes the need for a team's
  // persistance to a db in order for slash-commands to work
  controller.storage.teams.update({id: 'T03E23VAN', name: 'Reddit Casual'})

  // populates users
  controller.hears('^!db:seed:users', 'direct_message', (bot, message) => {
    bot.api.users.list({}, (err, res) => {
      if (err) return bot.reply(message, new Error(err))
      res.members.forEach((thing) => {
        controller.storage.users.update(thing)
      })
    })
  })

  // populates channels
  controller.hears('^!db:seed:channels', 'direct_message', (bot, message) => {
    bot.api.channels.list({}, (err, res) => {
      if (err) return bot.reply(message, new Error(err))
      res.channels.forEach((thing) => {
        controller.storage.channels.update(thing)
      })
    })
  })

  // populates groups
  controller.hears('^!db:seed:groups', 'direct_message', (bot, message) => {
    bot.api.groups.list({}, (err, res) => {
      if (err) return bot.reply(message, new Error(err))
      res.groups.forEach((thing) => {
        controller.storage.groups.update(thing)
      })
    })
  })

  // see a user's info
  controller.hears('^!info', 'direct_message', (bot, message) => {
    const user = stripKeyword(message)
    controller.storage.users.where({name: user})
      .then((userInfo) => bot.reply(message, prettyJson(userInfo)))
  })

  // all groups, channels, users bot has access to
  controller.hears('^!all', 'direct_message', (bot, message) => {
    const model = stripKeyword(message)
    controller.storage[model].all()
      .then((coll) => coll.map((el) => el.name))
      .then((names) => bot.reply(message, prettyJson(names)))
  })
}

@@ -595,39 +595,102 @@
 thing)%0A      %7D)%0A
+      bot.reply(message, %60Seeded $%7Bres.members.length%7D users%60)%0A
     %7D)%0A
-
   %7D)%0A%0A  // popul
@@ -976,32 +976,99 @@
 thing)%0A      %7D)%0A
+      bot.reply(message, %60Seeded $%7Bres.channels.length%7D channels%60)%0A
     %7D)%0A  %7D)%0A%0A  /
@@ -1342,32 +1342,32 @@
 s.update(thing)%0A
-
       %7D)%0A    %7D)%0A
@@ -1355,24 +1355,87 @@
 g)%0A      %7D)%0A
+      bot.reply(message, %60Seeded $%7Bres.groups.length%7D groups%60)%0A
     %7D)%0A  %7D)%0A

add confirmations to seeding tasks