var Waterline = require('../../../lib/waterline'),
    assert = require('assert');

describe('Collection Query', function() {

  describe('.update()', function() {

    describe('with nested model values', function() {
      var query;

      before(function(done) {

        var waterline = new Waterline();
        var Model = Waterline.Collection.extend({
          identity: 'user',
          connection: 'foo',
          attributes: {
            name: {
              type: 'string',
              defaultsTo: 'Foo Bar'
            },
            nestedModel: {
              model: 'nested'
            }
          }
        });
        var Nested = Waterline.Collection.extend({
          identity: 'nested',
          connection: 'foo',
          attributes: {
            name: 'string'
          }
        });

        waterline.loadCollection(Model);
        waterline.loadCollection(Nested);

        // Fixture Adapter Def
        var adapterDef = { update: function(con, col, criteria, values, cb) { return cb(null, [values]); }};

        var connections = {
          'foo': {
            adapter: 'foobar'
          }
        };

        waterline.initialize({ adapters: { foobar: adapterDef }, connections: connections }, function(err, colls) {
          if(err) return done(err);
          query = colls.collections.user;
          done();
        });
      });

      it('should reduce the nested object down to a foreign key', function(done) {
        query.update({}, { name: 'foo', nestedModel: { id: 1337, name: 'joe' }}, function(err, status) {
          assert(!err);
          assert(status[0].nestedModel);
          assert(status[0].nestedModel === 1337);
          done();
        });
      });
    });

    describe('with nested collection values', function() {
      var query, updatedModels = [];

      before(function(done) {

        var waterline = new Waterline();
        var Model = Waterline.Collection.extend({
          identity: 'user',
          connection: 'foo',
          attributes: {
            name: {
              type: 'string',
              defaultsTo: 'Foo Bar'
            },
            nestedModels: {
              collection: 'nested',
              via: 'model'
            }
          }
        });
        var Nested = Waterline.Collection.extend({
          identity: 'nested',
          connection: 'foo',
          attributes: {
            name: 'string',
            model: {
              model: 'user'
            }
          }
        });

        waterline.loadCollection(Model);
        waterline.loadCollection(Nested);

        // Fixture Adapter Def
        var adapterDef = {
          update: function(con, col, criteria, values, cb) {
            updatedModels.push(criteria.where);
            return cb(null, [values]);
          }
        };

        var connections = {
          'foo': {
            adapter: 'foobar'
          }
        };

        waterline.initialize({ adapters: { foobar: adapterDef }, connections: connections }, function(err, colls) {
          if(err) return done(err);
          query = colls.collections.user;
          done();
        });
      });


      //
      // TO-DO:
      // Make this not use a shit load of queries. (currently 10)!
      //

      it('should attempt to update each nested model', function(done) {

        var nestedModels = [
          { id: 1337, name: 'joe', model: 2 },
          { id: 1338, name: 'moe', model: 3 },
          { id: 1339, name: 'flow', model: 4 }
        ];

        query.update({}, { id: 5, name: 'foo', nestedModels: nestedModels }, function(err, status) {
          assert(!err);
          assert(status[0].nestedModels.length === 0);
          assert(updatedModels.length === 10);
          done();
        });
      });
    });

  });
});

@@ -922,32 +922,83 @@
 ure Adapter Def%0A
+        var _id = 1;%0A        var findValues = %5B%5D;%0A%0A
         var adap
@@ -1007,16 +1007,26 @@
 rDef = %7B
+%0A         
  update:
@@ -1068,16 +1068,113 @@
 s, cb) %7B
+%0A            values.id = _id;%0A            findValues.push(values);%0A            _id++;%0A           
  return 
@@ -1186,20 +1186,144 @@
 ll, 
-%5B
 values
-%5D); %7D
+);%0A          %7D,%0A          find: function(con, col, criteria, cb) %7B%0A            cb(null, findValues%5B_id - 1%5D);%0A          %7D%0A        
 %7D;%0A%0A
@@ -2897,32 +2897,83 @@
 ure Adapter Def%0A
+        var _id = 1;%0A        var findValues = %5B%5D;%0A%0A
         var adap
@@ -2975,32 +2975,32 @@
  adapterDef = %7B%0A
-
           update
@@ -3108,31 +3108,224 @@
     
-return cb(null, %5Bvalues
+values.id = _id;%0A            findValues.push(values);%0A            _id++;%0A            return cb(null, %5Bvalues%5D);%0A          %7D,%0A%0A          find: function(con, col, criteria, cb) %7B%0A            cb(null, findValues%5B_id - 1
 %5D);%0A

fix the test. Closes #406