

var mongoose = require('mongoose');
var timestamps = require('mongoose-time');
var serverConfig = require('../../config/v16/config');
var messages = require('../../config/v16/messages');
var utils = require('../../config/v16/utils');
var denormalize = require('mongoose-filter-denormalize').denormalize;
var ObjectId = mongoose.Schema.ObjectId;
var schemaVersion = serverConfig.config.schemaVersion;
var uniqueValidator = require('mongoose-unique-validator');
var generateUniqueKey = require('mongoose-generate-unique-key');
var stringify = require('safe-stable-stringify');
var postFileSchema = new mongoose.Schema({
    id: {
        type: String,
        required: true
    },
    originalname: {
        type: String,
        required: true
    },
    encoding: {
        type: String,
        default: null
    },
    mimetype: {
        type: String,
        default: null
    },
    fieldname: {
        type: String,
        default: null
    },
    gen_id: {
        type: String,
        unique: true
    },
    filename: {
        type: String,
        default: null
    },
    metadata: {
        type: String,
        default: null
    },
    bucketName: {
        type: String,
        default: null
    },
    chunkSize: {
        type: Number,
        default: null
    },
    md5: {
        type: String,
        default: null
    },
    uploadDate: {
        type: String,
        default: null
    },
    contentType: {
        type: String,
        default: null
    },

    size: {
        type: Number,
        default: null
    },
    postfile_denorm_string: {
        type: String,
        default: messages.messages.NA
    },
    __schemaVersion: {
        type: String,
        default: schemaVersion
    },
    references: [{ String: String }]
}).plugin(timestamps());

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

function replacer(key, value) {
    if (key == "postfile_denorm_string") return undefined;else if (key == "__schemaVersion") return undefined;else if (key == "encoding") return undefined;else if (key == "mimetype") return undefined;else if (key == "fieldname") return undefined;else if (key == "gen_id") return undefined;else if (key == "metadata") return undefined;else if (key == "chunkSize") return undefined;else if (key == "md5") return undefined;else if (key == "uploadDate") return undefined;else if (key == "contentType") return undefined;else if (key == "size") return undefined;else return value;
}
postFileSchema.plugin(generateUniqueKey('gen_id', function () {
    return String(Math.floor(Math.random() * 1000000000000));
}));
postFileSchema.pre('validate', function (next) {

    return next();
});

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

var postFile = mongoose.model('postfiles', postFileSchema);
module.exports = postFile;