'use strict';

var
    mongoose = require('mongoose'),
    no_duplicate = require('../no-duplicate.js'),
    Schema = mongoose.Schema,
    ObjectId = mongoose.Schema.ObjectId,
    async = require('async');

var db = mongoose.connection;

db.on('error', console.error.bind(console, 'Connection Error: please check if mongodb is running on localhost'));

var TestSchema = new Schema({
  name: { type: String, default: 'Riyadh' },
  comment: { type: String, default: 'lalalalalalala' },
  data: { type: ObjectId, ref:"Test"},
  date: String,
  payload: {
    include: String,
    complex:{
      some: String,
      notInclude: String
    },
    notInclude: Number
  }
})
TestSchema.plugin(no_duplicate.getPluginWithOptions({exclude:["date", "payload.notInclude", "payload.complex.notInclude", "notInclude"], sparse:true, dropDups:false}));

var Test;
if(typeof  should  == 'undefined' || !should){
  Test = mongoose.model('Test', TestSchema);
  mongoose.connect('mongodb://localhost:27017/no-duplicate');
  global.should = require('should');
}else{
  Test = mongoose.model('Test');

}
var test1 = new Test({
    name: "Riyah",
    comment: "lalalala",
    date: new Date().toISOString(),
    payload: {
          include: "blah",
          complex:{
            some: "gogog",
            notInclude: "notInclude"
          },
        notInclude: 0
      }
  });
var test2 = new Test({
  name: "Riyah",
  comment: "lalalala",
  date: new Date().toISOString(),
  payload: {
    include: "blah",
    complex:{
      some: "gogog",
      notInclude: "000"
    },
    notInclude: 12
  }
});
var test3 = new Test({
  name: "Riyah",
  comment: "lalalala00",
  date: new Date().toISOString(),
  payload: {
    include: "blah",
    complex:{
      some: "gogog",
      notInclude: "notInclude"
    },
    notInclude: 0
  }
});

describe('Mongoose Soft Delete Plugin', function () {
  it('should add fields to schema if they do not exist already', function (done) {
    Test.remove({}).exec();
    async.parallel([
      function(next){
        test1.save(next);
      },
      function(next){
        test2.save(next);
      },
      function(next){
        test3.save(next);
      },
    ],function(err){
      test1.sha1_hash.should.be.equal(test2.sha1_hash);
      test1.sha1_hash.should.not.equal(test3.sha1_hash);

      done();
    });
  });

});