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

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

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

const { ObjectId } = require('mongodb');
const { Account } = require('../models/account');

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

const MovementSchema = new Schema({
  _id: {
    type: String,
    default: function () {
      return new ObjectId().toString();
    },
  },
  idapp: {
    type: String,
  },
  notifId: {
    type: String,
  },
  transactionDate: {
    type: Date,
  },
  accountFromId: {
    type: String,
  },
  accountToId: {
    type: String,
  },
  idOrdersCart: { type: Schema.Types.ObjectId, ref: 'OrdersCart' },
  causal_table: {
    type: String,
  },
  causal_IdRec: {
    type: String,
  },
  amount: {
    type: Number,
  },
  causal: {
    type: String,
    default: '',
  },
  causalDest: {
    type: String,
    default: '',
  },
  residual: {
    type: Number,
    default: 0,
  },
  expiringDate: {
    type: Date,
  },
  confirmed: {
    type: Boolean,
    default: false,
  },
});

MovementSchema.statics.findAllIdApp = async function (idapp) {
  const MyMovement = this;

  const myfind = { idapp };

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

MovementSchema.pre('save', async function (next) {
  if (this.isNew) {
    this.transactionDate = new Date();
  }

  next();
});

MovementSchema.statics.getFieldsForSearch = function () {
  return [
    { field: 'causal', type: tools.FieldType.string },
    { field: 'amount', type: tools.FieldType.number },
  ];
};

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

MovementSchema.statics.addMov = async function (
  idapp,
  accountFromIdTable,
  accountToIdTable,
  amount,
  causal,
  causalDest,
  notifId,
  idOrdersCart
) {
  try {
    const { Circuit } = require('./circuit');

    // Only positive values
    amount = Math.abs(amount);

    let mymov = await Movement.create({
      idapp,
      transactionDate: new Date(),
      accountFromId: accountFromIdTable._id,
      accountToId: accountToIdTable._id,
      idOrdersCart,
      amount,
      causal,
      causalDest,
      residual: 0,
      notifId,
      // expiringDate:
    });

    if (mymov) {
      // Update saldo dell'Account
      await Account.addtoSaldo(accountToIdTable, amount, true);

      await Account.addtoSaldo(accountFromIdTable, -amount, false);

      return mymov;
    }
  } catch (e) {
    console.error('Error in addMov', e.message);
  }
};

MovementSchema.statics.getQueryMovsByCircuitId = async function (idapp, username, groupname, contocom, circuitId) {
  try {
    if (!circuitId) {
      return [];
    }
    const myaccount = await Account.getAccountByUsernameAndCircuitId(
      idapp,
      username,
      circuitId,
      false,
      true,
      groupname,
      contocom
    );

    if (myaccount) {
      let aggr1 = [
        {
          $match: {
            idapp,
            $or: [{ accountFromId: myaccount._id }, { accountToId: myaccount._id }],
          },
        },
        {
          $lookup: {
            from: 'accounts',
            localField: 'accountFromId',
            foreignField: '_id',
            as: 'accfrom',
          },
        },
        { $unwind: '$accfrom' },
        {
          $lookup: {
            from: 'users',
            let: { username: '$accfrom.username', idapp: '$accfrom.idapp' },
            pipeline: [
              {
                $match: {
                  $expr: {
                    $and: [{ $eq: ['$$username', '$username'] }, { $eq: ['$$idapp', '$idapp'] }],
                  },
                },
              },
            ],
            as: 'userfrom',
          },
        },
        {
          $unwind: {
            path: '$userfrom',
            preserveNullAndEmptyArrays: true,
          },
        },
        {
          $lookup: {
            from: 'mygroups',
            let: { groupname: '$accfrom.groupname', idapp: '$accfrom.idapp' },
            pipeline: [
              {
                $match: {
                  $expr: {
                    $and: [{ $eq: ['$$groupname', '$groupname'] }, { $eq: ['$$idapp', '$idapp'] }],
                  },
                },
              },
            ],
            as: 'groupfrom',
          },
        },
        {
          $unwind: {
            path: '$groupfrom',
            preserveNullAndEmptyArrays: true,
          },
        },

        {
          $lookup: {
            from: 'circuits',
            let: { contocom: '$accfrom.contocom', idapp: '$accfrom.idapp' },
            pipeline: [
              {
                $match: {
                  $expr: {
                    $and: [{ $eq: ['$$contocom', '$path'] }, { $eq: ['$$idapp', '$idapp'] }],
                  },
                },
              },
            ],
            as: 'contocomfrom',
          },
        },
        {
          $unwind: {
            path: '$contocomfrom',
            preserveNullAndEmptyArrays: true,
          },
        },
        {
          $lookup: {
            from: 'accounts',
            localField: 'accountToId',
            foreignField: '_id',
            as: 'accto',
          },
        },
        { $unwind: '$accto' },
        {
          $lookup: {
            from: 'circuits',
            localField: 'accfrom.circuitId',
            foreignField: '_id',
            as: 'circuitfrom',
          },
        },
        {
          $unwind: '$circuitfrom',
        },
        {
          $lookup: {
            from: 'circuits',
            localField: 'accto.circuitId',
            foreignField: '_id',
            as: 'circuitto',
          },
        },
        {
          $unwind: '$circuitto',
        },
        {
          $lookup: {
            from: 'users',
            let: { username: '$accto.username', idapp: '$accto.idapp' },
            pipeline: [
              {
                $match: {
                  $expr: {
                    $and: [{ $eq: ['$$username', '$username'] }, { $eq: ['$$idapp', '$idapp'] }],
                  },
                },
              },
            ],
            as: 'userto',
          },
        },
        {
          $unwind: {
            path: '$userto',
            preserveNullAndEmptyArrays: true,
          },
        },
        {
          $lookup: {
            from: 'mygroups',
            let: { groupname: '$accto.groupname', idapp: '$accto.idapp' },
            pipeline: [
              {
                $match: {
                  $expr: {
                    $and: [{ $eq: ['$$groupname', '$groupname'] }, { $eq: ['$$idapp', '$idapp'] }],
                  },
                },
              },
            ],
            as: 'groupto',
          },
        },
        {
          $unwind: {
            path: '$groupto',
            preserveNullAndEmptyArrays: true,
          },
        },
        {
          $lookup: {
            from: 'circuits',
            let: { contocom: '$accto.contocom', idapp: '$accto.idapp' },
            pipeline: [
              {
                $match: {
                  $expr: {
                    $and: [{ $eq: ['$$contocom', '$path'] }, { $eq: ['$$idapp', '$idapp'] }],
                  },
                },
              },
            ],
            as: 'contocomto',
          },
        },
        {
          $unwind: {
            path: '$contocomto',
            preserveNullAndEmptyArrays: true,
          },
        },
        {
          $project: {
            transactionDate: 1,
            amount: 1,
            causal: 1,
            causalDest: 1,
            notifId: 1,
            'circuitfrom.symbol': 1,
            'circuitto.symbol': 1,
            'userfrom.verified_by_aportador': 1,
            'userfrom.name': 1,
            'userfrom.surname': 1,
            'userfrom.username': 1,
            'userfrom.profile.img': 1,
            'userto.name': 1,
            'userto.surname': 1,
            'userto.username': 1,
            'userto.profile.img': 1,
            'userto.verified_by_aportador': 1,
            'groupfrom.groupname': 1,
            'groupfrom.verified_by_aportador': 1,
            'groupfrom.title': 1,
            'groupfrom.photos': 1,
            'groupto.groupname': 1,
            'groupto.title': 1,
            'groupto.photos': 1,
            'groupto.verified_by_aportador': 1,
            'contocomfrom.path': 1,
            'contocomfrom.name': 1,
            'contocomto.path': 1,
            'contocomto.name': 1,
          },
        },
      ];

      return aggr1;
    }
  } catch (e) {
    return [];
  }

  return [];
};

MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function (idapp, circuitId) {
  try {
    if (!circuitId) {
      return [];
    }
    let aggr1 = [
      {
        $match: {
          idapp,
        },
      },
      {
        $lookup: {
          from: 'accounts',
          localField: 'accountFromId',
          foreignField: '_id',
          as: 'accfrom',
        },
      },
      { $unwind: '$accfrom' },
      {
        $lookup: {
          from: 'users',
          let: { username: '$accfrom.username', idapp: '$accfrom.idapp' },
          pipeline: [
            {
              $match: {
                $expr: {
                  $and: [{ $eq: ['$$username', '$username'] }, { $eq: ['$$idapp', '$idapp'] }],
                },
              },
            },
          ],
          as: 'userfrom',
        },
      },
      {
        $unwind: {
          path: '$userfrom',
          preserveNullAndEmptyArrays: true,
        },
      },
      {
        $lookup: {
          from: 'mygroups',
          let: { groupname: '$accfrom.groupname', idapp: '$accfrom.idapp' },
          pipeline: [
            {
              $match: {
                $expr: {
                  $and: [{ $eq: ['$$groupname', '$groupname'] }, { $eq: ['$$idapp', '$idapp'] }],
                },
              },
            },
          ],
          as: 'groupfrom',
        },
      },
      {
        $unwind: {
          path: '$groupfrom',
          preserveNullAndEmptyArrays: true,
        },
      },
      {
        $lookup: {
          from: 'circuits',
          let: { contocom: '$accfrom.contocom', idapp: '$accfrom.idapp' },
          pipeline: [
            {
              $match: {
                $expr: {
                  $and: [{ $eq: ['$$contocom', '$path'] }, { $eq: ['$$idapp', '$idapp'] }],
                },
              },
            },
          ],
          as: 'contocomfrom',
        },
      },
      {
        $unwind: {
          path: '$contocomfrom',
          preserveNullAndEmptyArrays: true,
        },
      },
      {
        $lookup: {
          from: 'accounts',
          localField: 'accountToId',
          foreignField: '_id',
          as: 'accto',
        },
      },
      {
        $unwind: {
          path: '$accto',
          preserveNullAndEmptyArrays: true,
        },
      },
      {
        $match: { 'accto.circuitId': circuitId },
      },
      {
        $lookup: {
          from: 'circuits',
          localField: 'accfrom.circuitId',
          foreignField: '_id',
          as: 'circuitfrom',
        },
      },
      {
        $unwind: {
          path: '$circuitfrom',
          preserveNullAndEmptyArrays: true,
        },
      },
      {
        $lookup: {
          from: 'circuits',
          localField: 'accto.circuitId',
          foreignField: '_id',
          as: 'circuitto',
        },
      },
      {
        $unwind: {
          path: '$circuitto',
          preserveNullAndEmptyArrays: true,
        },
      },
      {
        $lookup: {
          from: 'users',
          let: { username: '$accto.username', idapp: '$accto.idapp' },
          pipeline: [
            {
              $match: {
                $expr: {
                  $and: [{ $eq: ['$$username', '$username'] }, { $eq: ['$$idapp', '$idapp'] }],
                },
              },
            },
          ],
          as: 'userto',
        },
      },
      {
        $unwind: {
          path: '$userto',
          preserveNullAndEmptyArrays: true,
        },
      },
      {
        $lookup: {
          from: 'mygroups',
          let: { groupname: '$accto.groupname', idapp: '$accto.idapp' },
          pipeline: [
            {
              $match: {
                $expr: {
                  $and: [{ $eq: ['$$groupname', '$groupname'] }, { $eq: ['$$idapp', '$idapp'] }],
                },
              },
            },
          ],
          as: 'groupto',
        },
      },
      {
        $unwind: {
          path: '$groupto',
          preserveNullAndEmptyArrays: true,
        },
      },
      {
        $lookup: {
          from: 'circuits',
          let: { contocom: '$accto.contocom', idapp: '$accto.idapp' },
          pipeline: [
            {
              $match: {
                $expr: {
                  $and: [{ $eq: ['$$contocom', '$path'] }, { $eq: ['$$idapp', '$idapp'] }],
                },
              },
            },
          ],
          as: 'contocomto',
        },
      },
      {
        $unwind: {
          path: '$contocomto',
          preserveNullAndEmptyArrays: true,
        },
      },
      {
        $project: {
          transactionDate: 1,
          amount: 1,
          causal: 1,
          causalDest: 1,
          notifId: 1,
          'circuitfrom.symbol': 1,
          'circuitto.symbol': 1,
          'userfrom.username': 1,
          'userfrom.name': 1,
          'userfrom.surname': 1,
          'userfrom.profile.img': 1,
          'userto.name': 1,
          'userto.surname': 1,
          'userto.username': 1,
          'userto.profile.img': 1,
          'groupfrom.groupname': 1,
          'groupfrom.title': 1,
          'groupto.groupname': 1,
          'groupto.title': 1,
          'contocomfrom.path': 1,
          'contocomfrom.name': 1,
          'contocomfrom.title': 1,
          'contocomto.path': 1,
          'contocomto.name': 1,
          'contocomto.title': 1,
        },
      },
    ];

    return aggr1;
  } catch (e) {
    return [];
  }

  return [];
};

MovementSchema.statics.getTotal_Transactions = async function (idapp) {
  const MyMovement = this;

  const numtransac = await MyMovement.countDocuments({
    idapp,
  });

  return numtransac;
};

MovementSchema.statics.getTot_RIS_Transati = async function (idapp) {
  const MyMovement = this;

  // Utilizza l'aggregazione per sommare tutti gli 'amount' per un dato 'idapp'
  const numtot = await MyMovement.aggregate([
    {
      $match: { idapp }, // Filtra i documenti per 'idapp'
    },
    {
      $sort: {
        transactionDate: -1,
      },
    },
    {
      $group: {
        _id: null,
        totalAmount: { $sum: '$amount' }, // Somma tutti gli 'amount'
      },
    },
  ]);

  try {
    return numtot.length > 0 ? numtot[0].totalAmount : 0;
  } catch (e) {
    return 0;
  }
};

MovementSchema.statics.getMovsByCircuitId = async function (idapp, username, circuitId) {
  const MyMovement = this;

  const myquery = await MyMovement.getQueryMovsByCircuitId(idapp, username, '', '', circuitId);

  if (myquery && myquery.length > 0) {
    ris = await MyMovement.aggregate(myquery);

    return ris;
  }

  return [];
};

MovementSchema.statics.checkIfCoinsAlreadySent = async function (notifId) {
  const MyMovement = this;

  try {
    const rec = await MyMovement.findOne({ notifId }, { _id: 1 });
    return !!rec;
  } catch (e) {
    // If Error, don't send the coins
    return true;
  }
};

MovementSchema.statics.getNumTotalTransactions = async function (idapp, username = '', groupname = '', contocom = '') {
  const MyMovement = this;

  // Determina se filtrare per un account specifico
  let abilitaquerypersingolo = (username || groupname || contocom);

  if (!abilitaquerypersingolo) {
    // Se non c'è filtro, conta tutte le transazioni dell'app
    return await MyMovement.countDocuments({ idapp });
  }

  // Pipeline per contare le transazioni
  let pipeline = [
    {
      $match: { idapp },
    },
    {
      $lookup: {
        from: 'accounts',
        localField: 'accountFromId',
        foreignField: '_id',
        as: 'accfrom',
      },
    },
    { $unwind: '$accfrom' },
    {
      $lookup: {
        from: 'accounts',
        localField: 'accountToId',
        foreignField: '_id',
        as: 'accto',
      },
    },
    { $unwind: '$accto' },
  ];

  // Costruisci il filtro OR per entrata o uscita
  let orConditions = [];

  if (username) {
    orConditions.push(
      { 'accfrom.username': username },
      { 'accto.username': username }
    );
  }

  if (groupname) {
    orConditions.push(
      { 'accfrom.groupname': groupname },
      { 'accto.groupname': groupname }
    );
  }

  if (contocom) {
    orConditions.push(
      { 'accfrom.contocom': contocom },
      { 'accto.contocom': contocom }
    );
  }

  // Aggiungi il match con OR
  pipeline.push({
    $match: {
      $or: orConditions
    }
  });

  // Conta i documenti
  pipeline.push({
    $count: 'total'
  });

  const result = await MyMovement.aggregate(pipeline);

  // Se non ci sono risultati, ritorna 0
  return result.length > 0 ? result[0].total : 0;
};

MovementSchema.statics.getLastN_Transactions = async function (
  idapp,
  numtransaz = 10,
  numtransaz_uscita = 10,
  username = '',
  groupname = '',
  contocom = ''
) {
  const MyMovement = this;

  // Determina se filtrare per un account specifico
  let abilitaquerypersingolo = username || groupname || contocom;

  // Costruisce i criteri di filtro base per identificare l'account
  let matchCriteria = {};
  if (username) {
    matchCriteria['accfrom.username'] = username;
    matchCriteria['accto.username'] = username;
  }
  if (groupname) {
    matchCriteria['accfrom.groupname'] = groupname;
    matchCriteria['accto.groupname'] = groupname;
  }
  if (contocom) {
    matchCriteria['accfrom.contocom'] = contocom;
    matchCriteria['accto.contocom'] = contocom;
  }

  // Pipeline comune per i lookup
  const commonLookups = [
    {
      $lookup: {
        from: 'accounts',
        localField: 'accountFromId',
        foreignField: '_id',
        as: 'accfrom',
      },
    },
    { $unwind: '$accfrom' },
    {
      $lookup: {
        from: 'accounts',
        localField: 'accountToId',
        foreignField: '_id',
        as: 'accto',
      },
    },
    { $unwind: '$accto' },
  ];

  // Funzione helper per costruire la pipeline completa
  const buildPipeline = (isEntrata) => {
    let pipeline = [
      {
        $match: { idapp },
      },
      {
        $sort: {
          transactionDate: -1,
        },
      },
      ...commonLookups,
    ];

    // Filtro per entrata/uscita
    if (abilitaquerypersingolo) {
      let matchCondition = {};

      if (username) {
        matchCondition['username'] = isEntrata ? 'accto.username' : 'accfrom.username';
        pipeline.push({
          $match: {
            [matchCondition['username']]: username,
          },
        });
      } else if (groupname) {
        matchCondition['groupname'] = isEntrata ? 'accto.groupname' : 'accfrom.groupname';
        pipeline.push({
          $match: {
            [matchCondition['groupname']]: groupname,
          },
        });
      } else if (contocom) {
        matchCondition['contocom'] = isEntrata ? 'accto.contocom' : 'accfrom.contocom';
        pipeline.push({
          $match: {
            [matchCondition['contocom']]: contocom,
          },
        });
      }
    }

    // Limit appropriato
    const limit = isEntrata ? numtransaz : numtransaz_uscita;
    if (limit) {
      pipeline.push({ $limit: limit });
    }

    // Aggiungi tutti i lookup rimanenti
    pipeline.push(
      {
        $lookup: {
          from: 'users',
          let: { username: '$accfrom.username', idapp: '$accfrom.idapp' },
          pipeline: [
            {
              $match: {
                $expr: {
                  $and: [{ $eq: ['$$username', '$username'] }, { $eq: ['$$idapp', '$idapp'] }],
                },
              },
            },
          ],
          as: 'userfrom',
        },
      },
      {
        $unwind: {
          path: '$userfrom',
          preserveNullAndEmptyArrays: true,
        },
      },
      {
        $lookup: {
          from: 'mygroups',
          let: { groupname: '$accfrom.groupname', idapp: '$accfrom.idapp' },
          pipeline: [
            {
              $match: {
                $expr: {
                  $and: [{ $eq: ['$$groupname', '$groupname'] }, { $eq: ['$$idapp', '$idapp'] }],
                },
              },
            },
          ],
          as: 'groupfrom',
        },
      },
      {
        $unwind: {
          path: '$groupfrom',
          preserveNullAndEmptyArrays: true,
        },
      },
      {
        $lookup: {
          from: 'circuits',
          let: { contocom: '$accfrom.contocom', idapp: '$accfrom.idapp' },
          pipeline: [
            {
              $match: {
                $expr: {
                  $and: [{ $eq: ['$$contocom', '$path'] }, { $eq: ['$$idapp', '$idapp'] }],
                },
              },
            },
          ],
          as: 'contocomfrom',
        },
      },
      {
        $unwind: {
          path: '$contocomfrom',
          preserveNullAndEmptyArrays: true,
        },
      },
      {
        $lookup: {
          from: 'circuits',
          localField: 'accfrom.circuitId',
          foreignField: '_id',
          as: 'circuitfrom',
        },
      },
      {
        $unwind: '$circuitfrom',
      },
      {
        $lookup: {
          from: 'circuits',
          localField: 'accto.circuitId',
          foreignField: '_id',
          as: 'circuitto',
        },
      },
      {
        $unwind: '$circuitto',
      },
      {
        $lookup: {
          from: 'users',
          let: { username: '$accto.username', idapp: '$accto.idapp' },
          pipeline: [
            {
              $match: {
                $expr: {
                  $and: [{ $eq: ['$$username', '$username'] }, { $eq: ['$$idapp', '$idapp'] }],
                },
              },
            },
          ],
          as: 'userto',
        },
      },
      {
        $unwind: {
          path: '$userto',
          preserveNullAndEmptyArrays: true,
        },
      },
      {
        $lookup: {
          from: 'mygroups',
          let: { groupname: '$accto.groupname', idapp: '$accto.idapp' },
          pipeline: [
            {
              $match: {
                $expr: {
                  $and: [{ $eq: ['$$groupname', '$groupname'] }, { $eq: ['$$idapp', '$idapp'] }],
                },
              },
            },
          ],
          as: 'groupto',
        },
      },
      {
        $unwind: {
          path: '$groupto',
          preserveNullAndEmptyArrays: true,
        },
      },
      {
        $lookup: {
          from: 'circuits',
          let: { contocom: '$accto.contocom', idapp: '$accto.idapp' },
          pipeline: [
            {
              $match: {
                $expr: {
                  $and: [{ $eq: ['$$contocom', '$path'] }, { $eq: ['$$idapp', '$idapp'] }],
                },
              },
            },
          ],
          as: 'contocomto',
        },
      },
      {
        $unwind: {
          path: '$contocomto',
          preserveNullAndEmptyArrays: true,
        },
      },
      {
        $project: {
          transactionDate: 1,
          amount: 1,
          causal: 1,
          causalDest: 1,
          notifId: 1,
          'circuitfrom.symbol': 1,
          'circuitfrom.name': 1,
          'circuitto.symbol': 1,
          'circuitto.name': 1,
          'userfrom.verified_by_aportador': 1,
          'userfrom.username': 1,
          'userfrom.name': 1,
          'userfrom.surname': 1,
          'userfrom.profile.img': 1,
          'userto.name': 1,
          'userto.surname': 1,
          'userto.username': 1,
          'userto.profile.img': 1,
          'userto.verified_by_aportador': 1,
          'groupfrom.groupname': 1,
          'groupfrom.verified_by_aportador': 1,
          'groupfrom.title': 1,
          'groupfrom.photos': 1,
          'groupto.groupname': 1,
          'groupto.title': 1,
          'groupto.photos': 1,
          'groupto.verified_by_aportador': 1,
          'contocomfrom.path': 1,
          'contocomfrom.name': 1,
          'contocomto.path': 1,
          'contocomto.name': 1,
        },
      }
    );

    return pipeline;
  };

  // Esegui entrambe le query in parallelo se necessario
  let transazioniEntrata = [];
  let transazioniUscita = [];

  if (abilitaquerypersingolo) {
    // Esegui le query separate per entrata e uscita
    [transazioniEntrata, transazioniUscita] = await Promise.all([
      numtransaz ? MyMovement.aggregate(buildPipeline(true)) : Promise.resolve([]),
      numtransaz_uscita ? MyMovement.aggregate(buildPipeline(false)) : Promise.resolve([]),
    ]);
  } else {
    // Se non c'è filtro specifico, prendi solo le ultime N transazioni totali
    const aggr1 = buildPipeline(false); // usa la pipeline base
    if (numtransaz) {
      // Rimuovi l'ultimo limit e aggiungine uno nuovo
      aggr1.splice(
        aggr1.findIndex((stage) => stage.$limit),
        1
      );
      aggr1.splice(3, 0, { $limit: numtransaz });
    }
    transazioniEntrata = await MyMovement.aggregate(aggr1);
  }

  // Combina e ordina i risultati
  const allTransactions = [...transazioniEntrata, ...transazioniUscita];
  allTransactions.sort((a, b) => b.transactionDate - a.transactionDate);

  // Processa i risultati
  allTransactions.forEach(function (mov) {
    let ris = tools.getStringaConto(mov);
    mov.userfrom = ris.userfrom;
    mov.userto = ris.userto;
    mov.tipocontofrom = ris.tipocontofrom;
    mov.tipocontoto = ris.tipocontoto;
  });

  return allTransactions;
};

const Movement = mongoose.model('Movement', MovementSchema);

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

module.exports = { Movement };
