 # Commands:
 #   hubot remember my harvest account <email> with password <password> - Saves your Harvest credentials to allow Hubot to track time for you
 #   hubot forget my harvest account <email> - Deletes your account credentials from Hubot's memory
-#   hubot [I'm] harvesting on <project>: <notes> - Start time tracking for a project with the given notes
-#   hubot [I've] finished on <project> - Stop time tracking on the given project
+#   hubot start harvest at <project>: notes - TODO
+#   hubot finish harvest at <project> - TODO
+#   hubot daily harvest - Hubot responds with your entries for today
 # 
 # Author:
 #   Quintus

    @email    = email
    @password = password

  # Asynchronously tests whether the account credentials are
  # valid. If so, the callback gets passed true, otherwise
  # it gets passed false.
  test: (msg, callback) ->
    msg.http(@base_url).headers
      "Content-Type": "application/json"
      "Accept": "application/json"
    .auth(@email, @password)
    .path("account/who_am_i").get() (err, res, body) ->
      if 200 <= res.statusCode <= 299
        callback true
      else
        callback false

    msg.message.user.harvest_account = null
    msg.reply "Okay, I erased your credentials from my memory."

  robot.respond /daily harvest/i, (msg) ->
    msg.message.user.harvest_account.daily msg, (status, body) ->
      if 200 <= status <= 299
        msg.reply "Your entries for today, #{msg.message.user.name}:"
        for entry in body.day_entries
          if entry.ended_at == ""
            msg.reply "* #{entry.project} (#{entry.client}) → #{entry.task} <#{entry.notes}> [running since #{entry.started_at} (#{entry.hours}h)]"
          else
            msg.reply "* #{entry.project} (#{entry.client}) → #{entry.task} <#{entry.notes}> [#{entry.started_at} - #{entry.ended_at} (#{entry.hours}h)]"
      else
        msg.reply "Failed with status #{status}."

# Class managing the Harvest account associated with
# a user. Keeps track of the user's credentials and can
# be used to query the Harvest API on behalf of that user.
#
# The API calls are asynchronous, i.e. the methods executing
# the request immediately return. To process the response,
# you have to attach a callback to the method call, which
# unless documtened otherwise will receive two arguments,
# the first being the response's status code, the second
# one is the response's body as a JavaScript object created
# via `JSON.parse`.
class HarvestAccount

  constructor: (email, password) ->
