use('test_FreePlanet');

let aggregation = [
  {
    $match: {
      idapp: "13",
      "profile.mycircuits": {
        $elemMatch: {
          circuitname: {
            $eq: "Circuito RIS Bologna",
          },
        },
      },
    },
  },
  {
    $lookup: {
      from: "circuits",
      as: "circuit",
      let: {
        circuitname: "Circuito RIS Bologna",
        idapp: "$idapp",
      },
      pipeline: [
        {
          $match: {
            $expr: {
              $and: [
                {
                  $eq: [
                    "$name",
                    "$$circuitname",
                  ],
                },
                {
                  $eq: [
                    "$idapp",
                    "$$idapp",
                  ],
                },
              ],
            },
          },
        },
      ],
    },
  },
  {
    $unwind: "$circuit",
  },
  {
    $project: {
      username: 1,
      name: 1,
      surname: 1,
      date_reg: 1,
      profile: 1,
      idapp: 1,
      "circuit.name": 1,
      "circuit._id": 1,
    },
  },
  {
    $lookup: {
      from: "accounts",
      as: "account",
      let: {
        username: "$username",
        idapp: "$idapp",
        circuitId: "$circuit._id",
      },
      pipeline: [
        {
          $match: {
            $expr: {
              $and: [
                {
                  $eq: [
                    "$$username",
                    "$username",
                  ],
                },
                {
                  $eq: [
                    "$$idapp",
                    "$idapp",
                  ],
                },
                {
                  $eq: [
                    "$$circuitId",
                    "$circuitId",
                  ],
                },
              ],
            },
          },
        },
      ],
    },
  },
  {
    $unwind: "$account",
  },
  {
    $match: {
      idapp: "13",
    },
  },
  {
    $addFields: {
      myId1: {
        $toObjectId: "$userId",
      },
      namecomplete: {
        $concat: [
          {
            $toLower: "$name",
          },
          {
            $toLower: "$surname",
          },
          { $toLower: "$username" },
        ],
      },
    },
  },
  {
    $lookup: {
      from: "users",
      localField: "myId1",
      foreignField: "_id",
      as: "user",
    },
  },
  {
    $replaceRoot: {
      newRoot: {
        $mergeObjects: [
          {
            $arrayElemAt: [
              "$user",
              0,
            ],
          },
          "$$ROOT",
        ],
      },
    },
  },
  {
    $project: {
      username: 1,
      name: 1,
      surname: 1,
      "profile.img": 1,
      "profile.mycircuits": 1,
      "profile.qualifica": 1,
      "profile.resid_province": 1,
      "account._id": 1,
      "account.saldo": 1,
      "account.fidoConcesso": 1,
      "account.qta_maxConcessa": 1,
      "account.totTransato": 1,
      reported: 1,
      date_report: 1,
      username_who_report: 1,
      namecomplete: 1,
      date_reg: 1,
    },
  },
  {
    $sort: {
      date_reg: 1,
    },
  },
  {
    $group: {
      _id: null,
      count: {
        $sum: 1,
      },
      results: {
        $push: "$$ROOT",
      },
    },
  },
  {
    $project: {
      count: 1,
      rows: {
        $slice: [
          "$results",
          1,
          15,
        ],
      },
    },
  },
];

db.users.aggregate(aggregation);

