text: // username - string unique, required, trimmed
  // email - string, required, unique, validated
  // thoughts - array of _id values referenceing the thought model
  // friends - array of _id values referencing the User model (self reference)
  // virtual - friendCount - retrieves the length of the users friends array field on query
  
  const { Schema, model } = require('mongoose');
  
  const userSchema = new Schema(
      {
          username: {
              type: String,
              required: true,
              unique: true,
              trim: true
          },
          email: {
              type: String,
              required: true,
              unique: true, 
              validate: {
                  validator: function(v) {
                    return /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/.test(v);
                  },
                  message: props => `${props.value} is not a valid email address!`
                },
          },
          thoughts: [
              {
                  type: Schema.Types.ObjectId,
                  ref: 'Thought',
              },
          ],
          friends: [
              {
                  type: Schema.Types.ObjectId,
                  ref: 'User'
              },
          ],
      },
      {
          toJSON: {
              virtuals: true,
          },
      }
  );
  
  userSchema.virtual('friendCount').get(function () {
      return this.friends.length;
  });
  
  const User = model('user', userSchema);
  
  module.exports = User;