 # Commands:
 #   hubot sendgrid total - total sendgrid usage for the account
 #   hubot sendgrid today - Total usage for today
-#   hubot sendgrid c[ategory] <category> - Today's usage for the given category
+#   hubot sendgrid last X [days] - Total usage for the last X days
+#   hubot sendgrid c[ategory] [total] <category> - Today or all time usage for the given category
+#   hubot sendgrid c[ategory] last X [days] <category> - Total usage for the last X days for the given category
+#   hubot sendgrid categories - list all categories for account
 #
 # Author:
 #   sixfeetover
+#   drdamour
 
 env = process.env
   

      opts =
        days: 0
      query msg, opts, (json) ->
        msg.send formatResponse(json[0])

    robot.respond /(sendgrid)( me)? total/i, (msg) ->
      opts =
        aggregate: 1
      query msg, opts, (json) ->
        msg.send formatResponse(json)

    robot.respond /(sendgrid)( me)? c(ategory)? (.*)/i, (msg) ->
      category = msg.match[4]
      msg.send "Category: #{category}"
      opts =
        days: 0
        category: [category]
      query msg, opts, (json) ->
        msg.send formatResponse(json[0])
  
query = (msg, opts, callback) ->
  opts.api_user = env.HUBOT_SENDGRID_USER

  spamreports: 'Spam Reports'
  repeat_spamreports: 'Repeat Spam Reports'

formatResponse = (json, header) =>
  details = for stat, description of stats
    "  #{description}: #{json[stat]}"
  details.unshift "Stats for #{header}:"
  details.join("\n")
