'use strict';

var when   = require('when');
var _      = require('lodash');
var models = require('../models');

/* ====================================================== */

exports.getRecipients = function(req, res) {

  var fetchRecipients = function(currentUserId, courseId) {
    var deferred = when.defer();

    models.Enrollment.findAll({
      where: { CourseId: courseId }
    }).then(function(enrollments) {
      models.User.findAll({
        where: { id: _.pluck(enrollments, 'UserId') }
      }).then(function(recipients) {
        // Return all users enrolled in the course except themselves
        deferred.resolve(_.reject(recipients, function(recipient) {
          return recipient.id === currentUserId;
        }));
      }).catch(function(err) {
        deferred.reject({ status: 500, body: err });
      });
    }).catch(function(err) {
      deferred.reject({ status: 500, body: err });
    });

    return deferred.promise;
  };

  var userId = 1; // TODO: don't hardcode this, use req.user.id

  fetchRecipients(userId, req.params.courseId).then(function(resp) {
    res.status(200).json(resp);
  }).catch(function(err) {
    res.status(err.status).json({ error: err.body });
  });

};

/* ====================================================== */

exports.getConversation = function(req, res) {

  var fetchConversation = function(courseId, userOneId, userTwoId) {
    var deferred = when.defer();

    models.Conversation.find({
      where: { CourseId: courseId, UserOneId: userOneId, UserTwoId: userTwoId },
      include: [models.Message]
    }).then(function(conversation) {
      if ( !conversation ) {
        deferred.reject({ status: 404, body: 'That conversation could not be found' });
      } else {
        deferred.resolve(conversation);
      }
    }).catch(function(err) {
      console.log('error finding conversation:', err);
      deferred.reject({ status: 500, body: err });
    });

    return deferred.promise;
  };

  fetchConversation(req.params.courseId, req.query.userOne, req.query.userTwo).then(function(resp) {
    res.status(200).json(resp);
  }).catch(function(err) {
    res.status(err.status).json({ error: err.body });
  });

};
@@ -221,30 +221,31 @@
 %0A  var fetch
-Recipi
+Enrollm
 ents = funct
@@ -423,97 +423,145 @@
     
-models.User.findAll(%7B%0A        where: %7B id: _.pluck(enrollments, 'UserId') %7D
+deferred.resolve(_.reject(enrollments, function(enrollment) %7B%0A        return enrollment.UserId === currentUserId;%0A      %7D));
 %0A    
-  
 %7D).
-then
+catch
 (fun
@@ -570,126 +570,122 @@
 ion(
-recipients) %7B%0A        // Return all users enrolled in the course except themselves%0A        deferred.resolve(_.reject(r
+err) %7B%0A      deferred.reject(%7B status: 500, body: err %7D);%0A    %7D);%0A%0A    return deferred.promise;%0A  %7D;%0A%0A  var fetchR
 ecip
@@ -685,25 +685,26 @@
 chRecipients
-,
+ =
  function(re
@@ -705,25 +705,27 @@
 ion(
-recipi
+enrollm
 ent
+s
 ) %7B%0A    
     
@@ -724,80 +724,127 @@
     
-      return recipient.id === currentUserId;%0A        %7D));%0A 
+var deferred = when.defer();%0A%0A    models.User.findAll(%7B%0A      where: %7B id: _.pluck(enrollments, 'UserId') %7D%0A
     
- 
 %7D).
-catch
+then
 (fun
@@ -845,33 +845,38 @@
 en(function(
-err
+recipients
 ) %7B%0A
-  
       deferr
@@ -884,49 +884,24 @@
 d.re
-ject(%7B status: 500, body: err %7D);%0A      %7D
+solve(recipients
 );%0A 
@@ -1091,30 +1091,31 @@
 .id%0A%0A  fetch
-Recipi
+Enrollm
 ents(userId,
@@ -1135,16 +1135,44 @@
 ourseId)
+%0A  .then(fetchRecipients)%0A  
 .then(fu

clean up getRecipients endpoint