/*
 * KodeBlox Copyright 2017 Sayak Mukhopadhyay
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http: //www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

"use strict";

module.exports = new Promise((resolve, reject) => {
    let db = require('../db');
    let mongoose = db.mongoose;
    let Schema = mongoose.Schema;

    let populatedSystem = new Schema({
        id: { type: Number, unique: true },
        edsm_id: Number,
        name: String,
        name_lower: { type: String, lowercase: true },
        x: Number,
        y: Number,
        z: Number,
        population: Number,
        is_populated: Boolean,
        government_id: Number,
        government: { type: String, lowercase: true },
        allegiance_id: Number,
        allegiance: { type: String, lowercase: true },
        state_id: Number,
        state: { type: String, lowercase: true },
        security_id: Number,
        security: { type: String, lowercase: true },
        primary_economy_id: Number,
        primary_economy: { type: String, lowercase: true },
        power: { type: String, lowercase: true },
        power_state: { type: String, lowercase: true },
        power_state_id: Number,
        needs_permit: Boolean,
        updated_at: Date,
        simbad_ref: { type: String, lowercase: true },
        controlling_minor_faction_id: { type: Number, ref: 'faction.id' },
        controlling_minor_faction: { type: String, lowercase: true, ref: 'faction.name_lower' },
        reserve_type_id: Number,
        reserve_type: { type: String, lowercase: true },
        minor_faction_presences: [{
            name: String,
            name_lower: { type: String, lowercase: true },
            government: { type: String, lowercase: true },
            state: { type: String, lowercase: true },
            influence: Number,
            allegiance: { type: String, lowercase: true },
        }]
    }, { runSettersOnQuery: true });

    populatedSystem.pre('save', function (next) {
        this.updated_at *= 1000;
        this.name_lower = this.name;
        if (this.minor_faction_presences) {
            this.minor_faction_presences.forEach((faction, index, factions) => {
                factions[index].name_lower = faction.name;
            }, this);
        }
        next();
    });

    populatedSystem.pre('findOneAndUpdate', function (next) {
        this._update.updated_at *= 1000;
        this._update.name_lower = this._update.name;
        if (this._update.minor_faction_presences) {
            this._update.minor_faction_presences.forEach((faction, index, factions) => {
                factions[index].name_lower = faction.name;
            }, this);
        }
        next();
    });

    let model = mongoose.model('populatedSystem', populatedSystem);

    resolve(model);
})
@@ -2058,138 +2058,60 @@
     
-name: String,%0A            name_lower: %7B type: String, lowercase: true %7D,%0A            government: %7B type: String, lowercase: true %7D
+minor_faction_id:Number,%0A            state_id:Number
 ,%0A  
@@ -2195,68 +2195,8 @@
 mber
-,%0A            allegiance: %7B type: String, lowercase: true %7D,
 %0A   

Fix wrong schema for minor faction prescences in populated systems