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

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

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


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

const MyPageSchema = new Schema({
  idapp: {
    type: String,
  },
  author_username: {
    type: String,
  },
  lang: {
    type: String,
  },
  title: {
    type: String,
  },
  subtitle: {
    type: String,
  },
  isTemplate: {
    type: Boolean,
  },
  icon: {
    type: String,
  },
  iconsize: {
    type: String,
  },
  order: {
    type: Number,
    default: 1000,
  },
  path: {
    type: String,
  },
  keywords: {
    type: String,
  },
  description: {
    type: String,
  },
  heightimg: {
    type: Number,
  },
  onlyif_logged: {
    type: Boolean,
  },
  only_residenti: {
    type: Boolean,
  },
  only_admin: {
    type: Boolean,
  },
  only_collab: {
    type: Boolean,
  },
  color: {
    type: String,
  },
  imgback: {
    type: String,
  },
  img1: {
    type: String,
  },
  content: {
    type: String,
  },
  video1: {
    type: String,
  },
  ratio1: {
    type: String,
  },
  img2: {
    type: String,
  },
  content2: {
    type: String,
  },
  video2: {
    type: String,
  },
  ratio2: {
    type: String,
  },
  img3: {
    type: String,
  },
  content3: {
    type: String,
  },
  video3: {
    type: String,
  },
  ratio3: {
    type: String,
  },
  content4: {
    type: String,
  },
  active: {
    type: Boolean,
  },
  inmenu: {
    type: Boolean,
  },
  submenu: {
    type: Boolean,
  },
  l_par: {
    type: Number,
  },
  l_child: {
    type: Number,
  },
  internalpage: {
    type: Boolean,
  },
  infooter: {
    type: Boolean,
  },
  extraclass: {
    type: String,
  },
  loadFirst: {
    type: Boolean,
  },
  showFooter: {
    type: Boolean,
  },
  sottoMenu: [{
    type: String
  }],
  hideHeader: {
    type: Boolean,
  },
  date_created: {
    type: Date,
    default: Date.now
  },
  date_updated: {
    type: Date,
  },
  sections: { type: Array },
});

MyPageSchema.statics.getFieldsForSearch = function () {
  return [{ field: 'title', type: tools.FieldType.string },
  { field: 'path', type: tools.FieldType.string },
  { field: 'keywords', type: tools.FieldType.string },
  { field: 'description', type: tools.FieldType.string },
  { field: 'content', type: tools.FieldType.string }]
};

MyPageSchema.statics.executeQueryTable = function (idapp, params, user) {
  params.fieldsearch = this.getFieldsForSearch();
  return tools.executeQueryTable(this, idapp, params, user);
};

MyPageSchema.statics.findAllIdApp = async function (idapp) {
  const MyPage = this;

  const myfind = { idapp };

  try {
    return await MyPage.find(myfind).sort({ title: 1 }).lean();
  } catch (err) {
    console.error("Errore in MyPage:", err, model);
    return null;
  }

};

MyPageSchema.statics.findOnlyStruttRec = async function (idapp) {
  const MyPage = this;

  const arrFirst = await MyPage.find(
    {
      idapp,
      loadFirst: true,
    }).lean();

  const arrfixed = await MyPage.find(
    {
      idapp,
      $or: [
        { loadFirst: { $exists: false } },
        { loadFirst: { $exists: true, $eq: false } }],
    }
    , {
      title: 1,
      subtitle: 1,
      icon: 1,
      order: 1,
      keywords: 1,
      description: 1,
      path: 1,
      active: 1,
      onlyif_logged: 1,
      isTemplate: 1,
      only_residenti: 1,
      only_admin: 1,
      only_collab: 1,
      inmenu: 1,
      submenu: 1,
      iconsize: 1,
      extraclass: 1,
      loadFirst: 1,
      sottoMenu: 1,
    }).lean();

  return [...arrFirst, ...arrfixed];

};

MyPageSchema.statics.findInternalPages = async function (idapp) {
  const MyPage = this;

  try {
    const myfind = {
      idapp,
      internalpage: { $exists: true, $eq: true }
    };

    const result = await MyPage.find(myfind, {
      title: 1,
      path: 1,
      onlyif_logged: 1,
      only_residenti: 1,
      only_admin: 1,
      only_collab: 1,
    }).lean();

    return result;

  } catch (err) {
    console.log('findInternalPages', err);
    throw err;
  }
};

const MyPage = mongoose.model('MyPage', MyPageSchema);

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


module.exports = { MyPage };
