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

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

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

mongoose.set('debug', false);

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

const SiteSchema = new Schema({
  active: {
    type: Boolean,
  },
  idapp: {
    type: String,
  },
  name: {
    type: String,
  },
  adminemail: {
    type: String,
  },
  manageremail: {
    type: String,
  },
  replyTo: {
    type: String,
  },
  host: {
    type: String,
  },
  host_test: {
    type: String,
  },
  portapp: {
    type: String,
  },
  dir: {
    type: String,
  },
  dir_test: {
    type: String,
  },
  email_from: {
    type: String,
  },
  email_pwd: {
    type: String,
  },
  telegram_key: {
    type: String,
  },
  telegram_bot_name: {
    type: String,
  },
  telegram_key_test: {
    type: String,
  },
  teleg_cfg: {
    type: String,
  },
  teleg_cfg_test: {
    type: String,
  },
  telegram_bot_name_test: {
    type: String,
  },
  telegram_support_chat: {
    type: String,
  },
  pathreg_add: {
    type: String,
  },
  who: {
    type: String
  },
  status: {
    type: String
  },
  note: {
    type: String
  },
  domain_provider: {
    type: String,
  },
  domain_expiring: {
    type: Date
  },
  next_payment: {
    type: Date
  },
  description: {
    type: String,
  },
  keywords: {
    type: String,
  },
  confpages: {
    font: { type: String, default: '' },
    col_toolbar: { type: String },
    col_bgfooter: { type: String },
    show_darkopt: { type: Boolean, default: true },
    showButtHome: { type: Boolean },
    showProfile: { type: Boolean },
    showUserMenu: { type: Boolean },
    showRegButton: { type: Boolean },
    enableReg: { type: Boolean },
    showNL: { type: Boolean },
    showMsgs: { type: Boolean },
    showNotif: { type: Boolean },
    showCoins: { type: Boolean },
    showNameSurname: { type: Boolean },
    showCompetenze: { type: Boolean },
    showConnected: { type: Boolean },
    bookingEvents: { type: Boolean },
    enableEcommerce: { type: Boolean },
    enableGroups: { type: Boolean },
    enableCircuits: { type: Boolean },
    enableProj: { type: Boolean },
    enableTodos: { type: Boolean },
    enableRegByBot: { type: Boolean },
    enableRegMultiChoice: { type: Boolean },
    enableDebugOn: { type: Boolean },
    enabledRegNeedTelegram: { type: Boolean },
    showViewEventi: { type: Boolean },
    showViewGroups: { type: Boolean },
    showViewCircuits: { type: Boolean },
    showViewUsers: { type: Boolean },
    showViewBooking: { type: Boolean },
    showViewProfile: { type: Boolean },
    enablePwa: { type: Boolean },
    lang: {
      type: Number,
      default: 0,
    },
    videoPromo: { type: String },
    PDFPromo: { type: String },
  },
  confsite: {
    options: {  // ConfSite
      type: Number,
      default: 0,
    },
  },
  policy: {
    show: { type: Boolean },
    owneremail: { type: String },
    siteName: { type: String },
    ownerDataName: { type: String },
    managerData: { type: String },
    includeData: { type: String },
    url: { type: String },
    lastdataupdate: { type: String },
    country: { type: String },
  },
  contacts: {
    facebook: { type: String },
    instagram: { type: String },
    whatsapp: { type: String },
    whatsapp_home: { type: Boolean },
    telegram: { type: String },
    youtube: { type: String },
    email: { type: String },
    address: { type: String },
    map: { type: String },
    info2: { type: String },
    cell: { type: String },
  },
});

var Site = module.exports = mongoose.model('Site', SiteSchema);

module.exports.getFieldsForSearch = function () {
  return []
};

module.exports.executeQueryTable = async function (idapp, params, userreq) {
  params.fieldsearch = this.getFieldsForSearch();
  // return tools.executeQueryTable(this, null, params);

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

  // Solo l'Admin puó leggerlo

  const extrapar = params.extrapar;

  if (extrapar) {
    if (User.isManager(userreq.perm)) {
      return await Site.findOne({ idapp: extrapar }).lean();
    } else {
      return await Site.findOne({ idapp: extrapar }, {
        name: 1, manageremail: 1,
        confsite: 1,
        description: 1,
        keywords: 1,
      }).lean();
    }
  }

  if (User.isAdmin(userreq.perm)) {
    const myarr = await Site.find({});

    // return await Site.find({}).lean();
    return ({ count: myarr.length, rows: myarr })

  }


};

module.exports.findAll = async function () {

  const myfind = { active: true };

  return await Site.find(myfind).lean();

};

module.exports.findAllIdApp = async function (idapp) {

  const myfind = { idapp, active: true };

  let rec = await Site.findOne(myfind).lean();
  if (rec) {

    rec.email_pwd = '';
    rec.telegram_key = '';
    rec.telegram_key_test = '';
    // rec.confsite = {};

    return rec;
  }
  return {};

};
