// MongoDB Playground
// To disable this template go to Settings | MongoDB | Use Default Template For Playground.
// Make sure you are connected to enable completions and to be able to run a playground.
// Use Ctrl+Space inside a snippet or a string literal to trigger completions.

// Select the database to use.
use('test_FreePlanet');


let passo1 = [
  {
    $match: {
      idapp: "13",
      "profile.mycircuits": {
        $elemMatch: {
          circuitname: {
            $eq: "RISO Viterbo",
          },
        },
      },
    },
  },
  {
    $lookup: {
      from: "circuits",
      as: "circuit",
      let: {
        circuitname: "RISO Viterbo",
        idapp: "$idapp",
      },
      pipeline: [
        {
          $match: {
            $expr: {
              $and: [
                {
                  $eq: [
                    "$name",
                    "$$circuitname",
                  ],
                },
                {
                  $eq: [
                    "$idapp",
                    "$$idapp",
                  ],
                },
              ],
            },
          },
        },
      ],
    },
  },
  {
    $unwind: "$circuit",
  },
      {
    $project: {
      username: 1,
      profile: 1,
      idapp: 1,
      "circuit.name": 1,
      "circuit._id": 1,
      "circuit.refused_users": 1,
    },
  },
]

let passo2 = [
  {
    $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",
                  ],
                },
              ],
            },
          },
        },
      ],
    },
  },
]

let passo2b = [
 {
    $unwind: "$account",
  }
];


let passo3 = [
  {
    $match: {
      idapp: "13",
    },
  },
  {
    $sort: {
      desc: 1,
    },
  },

];

let passo3b = [
 {
    $unwind: "$circuit",
  },
]

let passo3c = [
  {
    $lookup: {
      from: "circuit.refused_users",
      as: "myris",
      let: {
        username: "$username",
      },
      pipeline: [
        {
          $match: {
            $expr: {
              $and: [
                {
                  $eq: [
                    "$$username",
                    "$username",
                  ],
                },
              ],
            },
          },
        },
      ],
    },
  },
]

let passo3cOFF = [
  {
    $match: {
      "circuit.refused_users": {
        $elemMatch: {
          username: {
            $eq: "Emilie",
          },
        },
      },
    },
  },

];

let passo4 = [
  {
    $lookup: {
      from: "users",
      localField: "refused_users.username",
      foreignField: "username",
      as: "user",
    },
  },
  {
    $project: {
      "user.idapp": 1,
      "user.username": 1,
      "user.profile.img": 1,
      "user.profile.qualifica": 1,
    },
  },
]

let passo4b = [
  {
    $replaceRoot: {
      newRoot: {
        $mergeObjects: [
          {
            $arrayElemAt: [
              "$user",
              0,
            ],
          },
          "$$ROOT",
        ],
      },
    },
  },
]

let passo4c = [

  {
    $unwind: "$user",
  },
  {
    $replaceRoot: {
      newRoot: "$user",
    },
  },
]

let passo5 = [
  {
    $group: {
      _id: null,
      count: {
        $sum: 1,
      },
      results: {
        $push: "$$ROOT",
      },
    },
  },
  {
    $project: {
      count: 1,
      rows: {
        $slice: [
          "$results",
          0,
          10,
        ],
      },
    },
  }
];


let aggregation = [];

let test = false;

if (test) {
    aggregation = [...aggregation, ...passo4]; 
} else {
    aggregation = [...aggregation, ...passo1]; 
    aggregation = [...aggregation, ...passo2]; 
    aggregation = [...aggregation, ...passo2b]; 
    aggregation = [...aggregation, ...passo3]; 
    aggregation = [...aggregation, ...passo3b]; 
    aggregation = [...aggregation, ...passo3c]; 
    // aggregation = [...aggregation, ...passo4]; 
    aggregation = [...aggregation, ...passo4b]; 
    // aggregation = [...aggregation, ...passo4c]; 
    // aggregation = [...aggregation, ...passo5];
}

// Run the aggregation and open a cursor to the results.
// Use toArray() to exhaust the cursor to return the whole result set.
// You can use hasNext()/next() to iterate through the cursor page by page.
db.users.aggregate(aggregation);



db.getCollection('myskills').deleteMany({ '_id': { $lte: 10000 }})
db.getCollection('mygoods').deleteMany({ '_id': { $lte: 10000 }})
db.getCollection('mybachecas').deleteMany({ '_id': { $lte: 10000 }})
db.getCollection('myhosps').deleteMany({ '_id': { $lte: 10000 }})
db.getCollection('mygroups').deleteMany({ '_id': { $lte: 10000 }})