text: const crypto = require('crypto');
  const mongoose = require('mongoose');
  const validator = require('validator');
  const bcrypt = require('bcryptjs');
  
  const userSchema = new mongoose.Schema({
    name: {
      type: String,
      required: [true, 'Please tell us your name'],
    },
    email: {
      type: String,
      required: [true, 'Please provide your email'],
      unique: true,
      lowercase: true, //converts to lowercase
      validate: [validator.isEmail, 'Please provide a valid email'],
    },
    photo: {
      type: String,
      default: 'default.jpg',
    },
    role: {
      type: String,
      enum: ['user', 'guide', 'lead-guide', 'admin'],
      default: 'user',
    },
    password: {
      type: String,
      required: [true, 'Please provide a password'],
      minlength: 8,
      select: false,
    },
    passwordConfirm: {
      type: String,
      required: [true, 'Please confirm your password'],
      validate: [
        //this only works on SAVE!!!!
        function (val) {
          return val === this.password;
        },
        `Password didn't match, please enter the same password`,
      ],
    },
    passwordChangedAt: Date,
    passwordResetToken: String,
    passwordResetExpires: Date,
    active: {
      type: Boolean,
      default: true,
      select: false,
    },
  });
  
  //instance methods available on every document
  
  userSchema.methods.correctPassword = async function (
    candidatePassword,
    userPassword,
  ) {
    return await bcrypt.compare(candidatePassword, userPassword);
  };
  
  userSchema.methods.changedPasswordAfter = function (JWTTImestamp) {
    if (this.passwordChangedAt) {
      const changedTimeStamp = this.passwordChangedAt.getTime() / 1000;
      return JWTTImestamp < changedTimeStamp;
    }
  
    return false; //false means not changed
  };
  
  userSchema.methods.createPasswordResetToken = function () {
    const resetToken = crypto.randomBytes(32).toString('hex');
  
    this.passwordResetToken = crypto
      .createHash('sha256')
      .update(resetToken)
      .digest('hex');
    this.passwordResetExpires = Date.now() + 10 * 60 * 1000; //expires in 10 minutes from now
  
    // console.log({ resetToken }, this.passwordResetToken);
  
    return resetToken;
  };
  
  userSchema.pre('save', async function (next) {
    //only run this function if password was actually modified
    if (!this.isModified('password')) return next();
  
    //hash the password with cost  of 12
    this.password = await bcrypt.hash(this.password, 12);
  
    //delete password confirmation
    this.passwordConfirm = undefined;
    next();
  });
  
  userSchema.pre('save', function (next) {
    if (!this.isModified('password') || this.isNew) return next();
  
    this.passwordChangedAt = Date.now() - 1000;
    next();
  });
  
  userSchema.pre(/^find/, function (next) {
    this.find({ activefff: { $ne: false } });
    next();
  });
  
  module.exports = mongoose.model('User', userSchema);
  