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' },
        student: {type: mongoose.Schema.Types.ObjectId, ref: 'Student' }
      },
      isActive: String,
      timeCreated: { type: Date, default: Date.now },
      resetPasswordToken: String,
      resetPasswordExpires: Date,
      search: [String]
    });
  
    userSchema.methods.canPlayRoleOf = function(role) {
      if (role === "admin" && this.roles.admin) {
        return true;
      }
  
      if (role === "account" && this.roles.account) {
        return true;
      }
  
      if (role === "student" && this.roles.student) {
        return true;
      }
  
      return false;
    };
  
    userSchema.methods.defaultReturnUrl = function() {
      var returnUrl = '/';
      // if (this.canPlayRoleOf('account')) {
      //   returnUrl = '/account/';
      // }
  
      if (this.canPlayRoleOf('student')) {
        returnUrl = '/game/';
      }
      
      if (this.canPlayRoleOf('admin')) {
        returnUrl = '/admin/students';
      }
  
      return returnUrl;
    };
    userSchema.statics.encryptPassword = function(password, done) {
      var bcrypt = require('bcryptjs');
      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('bcryptjs');
      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({ search: 1 });
    userSchema.set('autoIndex', (app.get('env') === 'development'));
    app.db.model('User', userSchema);
  };
  