

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var timestamps = require('mongoose-time');
var serverConfig = require('../../config/v16/config');
var schemaVersion = serverConfig.config.schemaVersion;
var denormalize = require('mongoose-filter-denormalize').denormalize;
var messages = require('../../config/v16/messages');
var ObjectId = mongoose.Schema.ObjectId;
var uniqueValidator = require('mongoose-unique-validator');
var stringify = require('safe-stable-stringify');
var WorkerNodeSchema = new mongoose.Schema({
    name: {
        type: String
    },
    workernode_denorm_string: {
        type: String,
        default: messages.messages.NA
    },

    hostname: {
        type: String,
        required: true,
        uppercase: true
    },
    ip: {
        type: String,
        required: true
    },
    port: {
        type: String,
        required: true
    },
    accessToken: {
        type: String,
        required: true
    },
    enabled: {
        type: Number,
        default: 1,
        required: true
    },
    processing: {
        type: Number,
        default: 0,
        required: true
    },
    isDead: {
        type: Number,
        default: 0,
        required: true
    },
    lastAccessed: {
        type: String,
        default: Date.now()
    },
    userid: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'users',
        default: null
    },
    user_denorm: {
        type: ObjectId,
        ref: 'users',
        default: null,
        autopopulate: true
    },

    __schemaVersion: {
        type: String,
        default: schemaVersion
    }

}).plugin(timestamps());

WorkerNodeSchema.plugin(require('mongoose-autopopulate'));

function replacer(key, value) {
    if (key == "workernode_denorm_string") return undefined;else if (key == "__schemaVersion") return undefined;else if (key == "gen_id") return undefined;else return value;
}
WorkerNodeSchema.pre('validate', function (next) {

    if (this.userid === undefined || this.userid === null) {
        this.userid = serverConfig.config.DefaultId;
    }
    return next();
});

WorkerNodeSchema.pre('update', function (next) {
    var user = this;
    if (this.userid === undefined || this.userid === null) {
        this.userid = serverConfig.config.DefaultId;
    }
    // user.user_denorm = user.userid;
    // user.markModified('user_denorm');

    // var serializedString = stringify(user, replacer);
    // user.workernode_denorm_string = serializedString;
    //user.markModified('workernode_denorm_string');
    // user.isNew = true;
    return next();
});
WorkerNodeSchema.pre('save', function (next) {
    var user = this;
    if (this.userid === undefined || this.userid === null) {
        this.userid = serverConfig.config.DefaultId;
    }
    // user.user_denorm = user.userid;
    //user.markModified('user_denorm');

    // var serializedString = stringify(user, replacer);
    // user.workernode_denorm_string = serializedString;
    // // user.markModified('workernode_denorm_string');
    //  user.isNew = true;
    return next();
});
WorkerNodeSchema.index({
    port: 1,
    hostname: 1
}, {
    unique: true
});

WorkerNodeSchema.plugin(uniqueValidator);
var WorkerNodes = mongoose.model('WorkerNodes', WorkerNodeSchema);
module.exports = WorkerNodes;