text: const mongoose = require("mongoose");
  const Schema = mongoose.Schema;
  const bcrypt = require("bcryptjs");
  
  const userSchema = new Schema({
    email: {
      type: String,
      trim: true,
      unique: true,
      match: [/.+@.+\..+/, "Please enter a valid e-mail address"],
    },
  
    password: {
      type: String,
      trim: true,
      required: "Password is Required",
      validate: [({ length }) => length >= 6, "Password should be longer."],
    },
  
    shows: [
      {
        type: Array,
        ref: "shows",
      },
    ],
  });
  
  //Define Schema methods
  userSchema.methods = {
    checkPassword: function (inputPassword) {
      return bcrypt.compareSync(inputPassword, this.password);
    },
    hashPassword: (plainTextPassword) => {
      return bcrypt.hashSync(plainTextPassword, 10);
    },
  };
  
  //Defines hooks for pre-saving
  userSchema.pre("save", function (next) {
    if (!this.password) {
      console.log("models/user.js =======NO PASSWORD PROVIDED=======");
      next();
    } else {
      console.log("models/user.js hashPassword in pre save");
  
      this.password = this.hashPassword(this.password);
      next();
    }
  });
  
  var User = mongoose.model("User", userSchema);
  
  module.exports = User;
  