const express = require('express');
const router = express.Router();

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

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

const { authenticate } = require('../middleware/authenticate');

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

const { User } = require('../models/user');
const { Circuit } = require('../models/circuit');
const { Account } = require('../models/account');

const _ = require('lodash');

const { ObjectID } = require('mongodb');

async function getCircuitRecAdminsInfo(idapp, data) {

  if (data && data.admins) {
    for (const admin of data.admins) {
      const myuser = await User.findOne({ idapp, username: admin.username }, { 'profile.img': 1 }).lean();
      admin.profile = { img: myuser.profile.img };
    }
  }

  return data;
}

router.post('/loadall', authenticate, async (req, res) => {
  const idapp = req.body.idapp;

  try {
    let listaccounts = []
    if (User.isAdmin(req.user.perm)) {

      listaccounts = await Account.find({ idapp }).lean();

    }

    res.send({ listaccounts });

  } catch (e) {
    console.error('Error in Accounts', e);
    return res.status(400).send(e);
  }

  const ris = null;

});

module.exports = router;
