const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const timestamps = require('mongoose-timestamp');
const bcrypt = require('bcrypt-nodejs');

const userSchema = new Schema({
  user_id: {
    type: String,
    require: true,
    default: '0'
  },
  state: {
    type: Number,
    require: true,
    default: 0 //  0 : pending, 1 : admin, 2 : user ok, 3 : user disabled
  },
  uid: {
    type: String,
    require: true,
    default: '0'
  },
  username: {
    type: String,
    require: true,
    default: ''
  },
  name: {
    type: String,
    require: true,
    default: ''
  },
  last_name: {
    type: String,
    require: true,
    default: ''
  },
  email: {
    type: String,
    require: true,
    default: ''
  },
  profession: {
    type: String,
    require: true,
    default: ''
  },
  photo: {
    type: String,
    default: ''
  },
  password: {
    type: String,
    require: true,
    default: ''
  },
  linux: {
    type: String,
    require: true
  },
  connection: {
    type: Date,
    require: true
  },
  so: {
    type: Number, // 0: NO, 1: SI
    require: true,
    default: 0
  },
  text: {
    type: String,
    require: true,
    default: ''
  }
});

userSchema.methods.generateHash = function(password) {
  return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};

userSchema.methods.validPassword = function(password) {
  return bcrypt.compareSync(password, this.password);
};

userSchema.plugin(timestamps);
const User = mongoose.model('User', userSchema);

module.exports.User = User;
