mongoose = require('mongoose').set('debug', false)
const Schema = mongoose.Schema;

const tools = require('../tools/general');

mongoose.Promise = global.Promise;
mongoose.level = "F";

const { IImg } = require('../models/myscheda');

// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
  schema.options.usePushEach = true
});

const productInfoSchema = new Schema({
  idapp: {
    type: String,
  },
  deleted: {
    type: Boolean,
  },
  department: {
    type: String, ref: 'Department'
  },
  sku: {
    type: String,
  },
  code: {
    type: String,
    unique: true,
    required: true,
  },
  codice: { // codice interno prodotto
    type: String,
  },
  id_wp: {  // id in wordpress
    type: String,
  },
  codice_EAN: {
    type: String,
  },
  barcode: {
    type: String,
  },
  name: {
    type: String,
    index: true,
  },
  description: {
    type: String,
  },
  short_descr: {
    type: String,
  },
  idCatProds: [{ type: Schema.Types.ObjectId, ref: 'CatProd', index: true }],
  idSubCatProds: [{ type: Schema.Types.ObjectId, ref: 'SubCatProd' }],
  idStatoProdotto: {
    type: Number, index: true
  },
  color: {
    type: String
  },
  size: {
    type: String  // 11x4x3
  },
  weight: {
    type: Number
  },
  weight_lordo: {
    type: Number
  },
  unit: {
    type: Number,
  },
  unit_lordo: {
    type: Number,
  },
  sfuso: {  // serve se moltiplicare le qta (es: 12 kg) oppure fare (2 x 20 ml)
    type: Boolean
  },
  vegan: {
    type: Boolean
  },
  icon: {
    type: String,
  },
  img: {
    type: String,
  },
  imagefile: {
    type: String,
  },
  image_not_found: {
    type: Boolean,
  },
  vers_img: {
    type: Number,
  },
  image_link: {
    type: String,
  },
  link_scheda: {
    type: String,
  },
  link: {
    type: String,
  },
  checkout_link: {
    type: String,
  },
  versioneGM: {
    type: String,
  },
  img2: {
    type: String,
  },
  img3: {
    type: String,
  },
  img4: {
    type: String,
  },
  ingredienti: {
    type: String,
  },
  valori_nutrizionali: {
    type: String,
  },
  note: {
    type: String,
  },
  idAuthors: [{ type: Schema.Types.ObjectId, ref: 'Author', index: true }],
  idCollana: { type: Schema.Types.ObjectId, ref: 'Collana', index: true },
  idPublisher: { type: Schema.Types.ObjectId, ref: 'Publisher', index: true },
  collezione: {
    type: String,
  },
  ListaArgomenti: {
    type: String,
  },
  date_pub: {
    type: Date,
    index: 1,
  },
  date_pub_ts: {
    type: Number,
  },
  productTypes: [{
    type: Number,
  }],

  date_updated: {
    type: Date,
  },
  
  date_updated_fromGM: {
    type: Date,
  },

  totVen: Number,
  totFat: Number,
  vLast3M: Number,
  fatLast3M: Number,
  fatLast6M: Number,
  fatLast1Y: Number,
  fatLast2Y: Number,
  vLast6M: {
    type: Number,
    index: true,
  },
  vLast1Y: {
    type: Number, index: true
  },
  vLast2Y: Number,
  dataUltimoOrdine: Date,
  rank3M: Number,
  rank6M: Number,
  rank1Y: Number,

  descrizione_breve_macro: String,
  descrizione_completa_macro: String,
  descr_trafiletto_catalogo: String,
  sottotitolo: String,
  link_macro: String,

});

var productInfo = module.exports = mongoose.model('ProductInfo', productInfoSchema);

module.exports.getFieldsForSearch = function () {
  return [
    { field: 'name', type: tools.FieldType.string },
    { field: 'code', type: tools.FieldType.string },
    { field: 'sku', type: tools.FieldType.string },
    { field: 'codice_EAN', type: tools.FieldType.string },
  ]
};

module.exports.executeQueryTable = function (idapp, params) {
  params.fieldsearch = this.getFieldsForSearch();
  return tools.executeQueryTable(this, idapp, params);
};

/*module.exports.findAllIdApp = async function (idapp, code, id) {
  let myfind = {};
  let myqueryadd = {};
  let query = [];

  try {

    if (idapp)
      myfind = {
        idapp,
        $or: [
          { deleted: { $exists: false } },
          { deleted: false }
        ]
      };

    if (code) {
      myfind = { ...myfind, code }
    }
    if (id) {
      myqueryadd = {
        $addFields: {
          myId1: {
            $toObjectId: id,
          },
        },
      }
      myfind = {
        $expr: {
          $eq: ["$_id", "$myId1"],
        },
      }

      query.push(myqueryadd);
    }

    query.push(
      { $match: myfind },
      {
        $lookup: {
          from: 'catprods',
          localField: 'idCatProds',
          foreignField: '_id',
          as: 'catprods'
        }
      },
      {
        $lookup: {
          from: 'authors',
          localField: 'idAuthors',
          foreignField: '_id',
          as: 'authors'
        }
      },
      {
        $lookup: {
          from: 'collanas',
          localField: 'idCollana',
          foreignField: '_id',
          as: 'collana'
        }
      },
      {
        $lookup: {
          from: 'publishers',
          localField: 'idPublisher',
          foreignField: '_id',
          as: 'publisher'
        }
      },
      {
        $lookup: {
          from: 'subcatprods',
          localField: 'idSubCatProds',
          foreignField: '_id',
          as: 'subcatprods'
        }
      },
      {
        $sort: {
          name: 1 // 1 for ascending order, -1 for descending order
        }
      },
    );

    let ris = await productInfo.aggregate(query)

    return ris;

  } catch (e) {
    console.error('E', e);
  }

};*/



module.exports.createIndexes()
  .then(() => { })
  .catch((err) => { throw err; });

