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

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

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

const { User } = require('./user');

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

const MailingListSchema = new Schema({
});

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

MailingListSchema.statics.findAllIdAppSubscribed = function (idapp) {

  const myfind = {
    idapp,
    news_on: true,
    $or: [
      { deleted: { $exists: false } },
      { deleted: { $exists: true, $eq: false } }],
    $or: [
      { email_errata: { $exists: false } },
      { email_errata: { $exists: true, $ne: true } }],
  };

  // Extract only the Teacher where in the users table the field permissions is set 'Teacher' bit.

  return tools.findAllQueryIdApp(User, myfind);
};

MailingListSchema.statics.findAllIdAppDiarioSubscr = function (idapp) {

  const myfind = {
    idapp,
    diario_on: true,
    $or: [
      { deleted: { $exists: false } },
      { deleted: { $exists: true, $eq: false } }],
    $or: [
      { email_errata: { $exists: false } },
      { email_errata: { $exists: true, $ne: true } }],
  };

  // Extract only the Teacher where in the users table the field permissions is set 'Teacher' bit.

  return tools.findAllQueryIdApp(User, myfind);
};
MailingListSchema.statics.findAllIdAppDiarioSubscr = function (idapp) {

  const myfind = {
    idapp,
    test: true,
    $or: [
      { deleted: { $exists: false } },
      { deleted: { $exists: true, $eq: false } }],
    $or: [
      { email_errata: { $exists: false } },
      { email_errata: { $exists: true, $ne: true } }],
  };

  // Extract only the Teacher where in the users table the field permissions is set 'Teacher' bit.

  return tools.findAllQueryIdApp(User, myfind);
};

MailingListSchema.statics.getnumSent = async function (idapp, idUser) {

  const myfind = { idapp, news_on: true, lastid_newstosent: idUser };

  // Extract only the Teacher where in the users table the field permissions is set 'Teacher' bit.

  return await User.countDocuments(myfind);
};

MailingListSchema.statics.isOk = async function (idapp, iduser, idUser) {


  const myfind = { idapp, _id: iduser, news_on: true, lastid_newstosent: { $ne: idUser } };

  // Extract only the Teacher where in the users table the field permissions is set 'Teacher' bit.

  return await User.countDocuments(myfind) > 0;
};

MailingListSchema.statics.findAllIdApp = async function (idapp) {

  const myfind = { idapp };

  return await tools.findAllQueryIdApp(User, myfind);
};

MailingListSchema.statics.isUnsubscribed = async function (idapp, hash) {

  let myperson = await User.findOne({ idapp, hash });
  if (!!myperson) {
    return (!myperson.news_on)
  }
};

MailingListSchema.statics.findByHash = function (idapp, hash) {

  const myfind = { idapp, hash };

  // Extract only the Teacher where in the users table the field permissions is set 'Teacher' bit.

  return User.findOne(myfind).lean();
};

const MailingList = mongoose.model('MailingList', MailingListSchema);

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


module.exports = { MailingList };
