text: 'use strict';
  
  exports = module.exports = function(app, mongoose) {
    var userSchema = new mongoose.Schema({
      username: { type: String, unique: true },
      password: String,
      email: { type: String, unique: true },
      roles: {
        admin: { type: mongoose.Schema.Types.ObjectId, ref: 'Admin' },
        account: { type: mongoose.Schema.Types.ObjectId, ref: 'Account' },
        accountType: {type: String, default:''}
      },
      firstName: { type: String, default: ''},
      lastName: { type: String, default: ''},
      sendMailCounts:{ type: Number, default: 0},
      isCompletedProfile: {type: String, default: 'no'},
      isActive: String,
      timeCreated: { type: Date, default: Date.now },
      resetPasswordToken: String,
      resetPasswordExpires: Date,
      twitter: {},
      github: {},
      facebook: {},
      google: {},
      tumblr: {},
      search: [String],
      phone: { type: String, default: '' },
      zip: { type: String, default: '' },    
      address: { type: String, default: '' },
      city: { type: String, default: '' },
      state: { type: String, default: '' },
      occupation: { type: String, default: '' },
      otherSpecify: { type: String, default: '' },
      atlantic: { type: Boolean, default: '' },
      hunterdon: { type: Boolean, default: '' },
      sussex: { type: Boolean, default: '' },
      gloucester: { type: Boolean, default: '' },
      salem: { type: Boolean, default: '' },
      cumberland: { type: Boolean, default: '' },
      ocean: { type: Boolean, default: '' },
      camden: { type: Boolean, default: '' },
      monmouth: { type: Boolean, default: '' },
      bergen: { type: Boolean, default: '' },
      merser: { type: Boolean, default: '' },
      union: { type: Boolean, default: '' },
      hudson: { type: Boolean, default: '' },
      somerset: { type: Boolean, default: '' },
      essex: { type: Boolean, default: '' },
      passaic: { type: Boolean, default: '' },
      capemay: { type: Boolean, default: '' },
      morris: { type: Boolean, default: '' },
      burlington: { type: Boolean, default: '' },
      middlesex: { type: Boolean, default: '' },
      warren: { type: Boolean, default: '' },
      whereHeardUs: { type: String, default: '' },
      memberRanking: { type: String, default: '' },
      badge: { type: String, default: '' },
      probationStrike: { type: Number, default: 0 },
      status: {
        submitted: { type: Number, default: '' },
        new: { type: Number, default: '' },
      },
      photoURL: { type: String, default:''},
      registeredClosingStats: { type: String, default:'no'},
      closingStatsExplanation: {type: String, default:''},
      rankingScore: { type: Number, default: 0}
    });
    userSchema.methods.isVerified = function(done){
      this.populate('roles.account', function(err, user){
        if(err){
          return done(err);
        }
        var flag = user.roles.account && user.roles.account.isVerified && user.roles.account.isVerified === 'yes';
        return done(null, flag);
      });
    };
    userSchema.methods.canPlayRoleOf = function(role) {
      if (role === "admin" && this.roles.admin) {
        return true;
      }
  
      if (role === "account" && this.roles.account) {
        return true;
      }
  
      return false;
    };
    userSchema.methods.defaultReturnUrl = function() {
      var returnUrl = '/';
      if (this.canPlayRoleOf('account')) {
        returnUrl = '/account';
      }
  
      if (this.canPlayRoleOf('admin')) {
        returnUrl = '/admin';
      }
  
      return returnUrl;
    };
    userSchema.statics.encryptPassword = function(password, done) {
      var bcrypt = require('bcrypt');
      bcrypt.genSalt(10, function(err, salt) {
        if (err) {
          return done(err);
        }
  
        bcrypt.hash(password, salt, function(err, hash) {
          done(err, hash);
        });
      });
    };
    userSchema.statics.validatePassword = function(password, hash, done) {
      var bcrypt = require('bcrypt');
      bcrypt.compare(password, hash, function(err, res) {
        done(err, res);
      });
    };
    userSchema.plugin(require('./plugins/pagedFind'));
    userSchema.index({ username: 1 }, { unique: true });
    userSchema.index({ email: 1 }, { unique: true });
    userSchema.index({ timeCreated: 1 });
    userSchema.index({ 'twitter.id': 1 });
    userSchema.index({ 'github.id': 1 });
    userSchema.index({ 'facebook.id': 1 });
    userSchema.index({ 'google.id': 1 });
    userSchema.index({ search: 1 });
    userSchema.set('autoIndex', (app.get('env') === 'development'));
    app.db.model('User', userSchema);
  };
  