

var mongoose = require('mongoose');
var timestamps = require('mongoose-time');
var serverConfig = require('../../config/v16/config');
var messages = require('../../config/v16/messages');
var schemaVersion = serverConfig.config.schemaVersion;
var Schema = mongoose.Schema;
var denormalize = require('mongoose-filter-denormalize').denormalize;
var ObjectId = mongoose.Schema.ObjectId;
var uniqueValidator = require('mongoose-unique-validator');
var stringify = require('safe-stable-stringify');
var WorkerQueueObjectSchema = new Schema({
    runid: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'runtasks',
        default: null
    },
    runtask_denorm: {
        type: ObjectId,
        ref: 'runtasks',
        default: null,
        autopopulate: true
    },
    userid: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'users',
        default: null
    },
    user_denorm: {
        type: ObjectId,
        ref: 'users',
        default: null,
        autopopulate: true
    },
    worker_denorm_string: {
        type: String,
        default: messages.messages.NA
    },
    xml: {
        type: String,
        default: null
    },
    __schemaVersion: {
        type: String,
        default: schemaVersion
    }
}).plugin(timestamps());
//https://www.npmjs.com/package/mongoose-autopopulate
WorkerQueueObjectSchema.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;
}

WorkerQueueObjectSchema.pre('validate', function (next) {

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

WorkerQueueObjectSchema.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.runtask_denorm = user.runid;
    //user.markModified('user_denorm');
    //user.markModified('runtask_denorm');
    var serializedString = stringify(user, replacer);
    user.worker_denorm_string = serializedString;
    //user.markModified('workernode_denorm_string');
    //user.isNew = true;
    return next();
});
WorkerQueueObjectSchema.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.runtask_denorm = user.runid;
    //user.markModified('user_denorm');
    //user.markModified('runtask_denorm');
    var serializedString = stringify(user, replacer);
    user.worker_denorm_string = serializedString;
    //user.markModified('workernode_denorm_string');
    user.isNew = true;
    return next();
});

WorkerQueueObjectSchema.index({
    runid: 1,
    userid: 1
}, {
    unique: true
});

WorkerQueueObjectSchema.plugin(uniqueValidator);
var WorkerQueueObject = mongoose.model('workers', WorkerQueueObjectSchema);

module.exports = WorkerQueueObject;