var mongoose = require('mongoose');
const Subscription = mongoose.model('subscribers');

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

const telegrambot = require('../../telegram/telegrambot');

module.exports = {
  doOtherThingsAfterDeleted: async function (tablename, rec) {
    try {
      if (tablename === 'users') {

        await ListaIngresso.deleteUserInListaIngresso(rec.idapp, rec.username);

        // Controlla se aveva invitati, li regala a quello sopra
        const arrap = await User.getDownlineByUsername(rec.idapp, rec.username);
        for (let user of arrap) {

          await User.findOneAndUpdate({
            idapp: rec.idapp,
            username: user.username
          }, { $set: { aportador_solidario: rec.aportador_solidario } }, { new: false });

          let msg = 'Spostato ' + user.name + ' ' + user.surname + ' sotto di ' + rec.aportador_solidario;
          telegrambot.sendMsgTelegramToTheManagers(idapp, msg);
        }

        // Delete also all the subscribers record of this User
        return Subscription.deleteOne({ userId: rec._id })
      }
    } catch (e) {
      console.error(e.message);
      return false
    }
    return true;
  },
  doOtherThingsAfterDuplicated: async function (tablename, myrec, mynewrec) {
    try {
      if (tablename === 'users') {
        // Delete also all the subscribers record of this User

      }
      return { myrec }

    } catch (e) {
      return false
    }
    return true;
  }
};
