

var mongoose = require('mongoose');
var timestamps = require('mongoose-time');
var serverConfig = require('../../config/v16/config');
var utils = require('../../config/v16/utils');
var messages = require('../../config/v16/messages');
var denormalize = require('mongoose-filter-denormalize').denormalize;
var ObjectId = mongoose.Schema.ObjectId;
var stringify = require('safe-stable-stringify');
var schemaVersion = serverConfig.config.schemaVersion;
var uniqueValidator = require('mongoose-unique-validator');
var generateUniqueKey = require('mongoose-generate-unique-key');
var stsFileSchema = new mongoose.Schema({
    id: {
        type: String,
        required: true
    },
    sts_denorm_string: {
        type: String
    },
    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
    },
    __schemaVersion: {
        type: String,
        default: schemaVersion
    },
    references: [{ String: String }]
}).plugin(timestamps());
stsFileSchema.plugin(uniqueValidator);

function replacer(key, value) {
    if (key == "sts_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;
}
stsFileSchema.plugin(generateUniqueKey('gen_id', function () {
    return String(Math.floor(Math.random() * 1000000000000));
}));
stsFileSchema.plugin(require('mongoose-autopopulate'));
stsFileSchema.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.sts_denorm_string = serializedString;
    // user.markModified('sts_denorm_string');
    //user.isNew = true;
    return next();
});
stsFileSchema.pre('validate', function (next) {
    var user = this;
    if (this.userid === undefined || this.userid === null) {
        this.userid = serverConfig.config.DefaultId;
    }

    if (!this.originalname.includes('.sts')) {
        var err = new Error(messages.messages.INVALIDFILEEXT);
        next(err);
    }
    return next();
});
stsFileSchema.pre('save', function (next) {
    var user = this;
    if (this.userid === undefined || this.userid === null) {
        this.userid = serverConfig.config.DefaultId;
    }
    if (!this.originalname.includes('.sts')) {
        var err = new Error(messages.messages.INVALIDFILEEXT);
        next(err);
    }
    var serializedString = stringify(user, replacer);
    user.sts_denorm_string = serializedString;
    user.markModified('sts_denorm_string');
    user.isNew = true;
    return next();
});
var stsFile = mongoose.model('stsfiles', stsFileSchema);
module.exports = stsFile;