text: /* eslint-disable */
  const { Formulaire, Offre } = require("../../common/model");
  const logger = require("../../common/logger");
  const _ = require("lodash");
  const config = require("config");
  const axios = require("axios");
  
  const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
  
  const getAllApplications = async (options) => {
    let { page, allCandidature, limit, query } = { page: 1, allCandidature: [], limit: 1000, ...options };
  
    let params = { page, limit, query };
    logger.debug(`Requesting LBA with parameters`, params);
  
    const response = await axios.get(
      `https://labonnealternance${
        config.env === "production" ? "" : "-recette"
      }.apprentissage.beta.gouv.fr/api/application/search`,
      {
        headers: {
          Application: config.lba.application,
          "API-key": config.lba.apiKey,
        },
        params,
      }
    );
  
    const { data, pagination } = response.data;
    allCandidature = allCandidature.concat(data); // Should be properly exploded, function should be pure
  
    if (page < pagination.number_of_page) {
      return getAllApplications({ page: page + 1, allCandidature, limit });
    } else {
      return allCandidature;
    }
  };
  
  const createOffreCollection = async (application) => {
    logger.info("Deleting offres collections...");
    await Offre.deleteMany({});
  
    logger.info("Creating offres collections...");
    let formulaires = await Formulaire.find({}).lean();
  
    await Promise.all(
      formulaires.map(async (form) => {
        await Promise.all(
          form.offres.map(async (offre) => {
            const filtOffre = _.omit(offre, ["_id"]);
            const filtForm = _.omit(form, ["_id", "offres", "mailing", "events", "statut"]);
            filtForm.statutFormulaire = form.statut;
  
            // if (found.length > 0) {
            //   filtOffre.candidatures = found.length;
            // } else {
            //   filtOffre.candidatures = 0;
            // }
            // const candidatures = await application.getApplication(offre._id);
  
            // console.log(candidatures);
  
            // filtOffre.candidatures = offre.candidatures =
            //   candidatures.data.data.length > 0 ? candidatures.data.data.length : undefined;
  
            // console.log("candidatures", filtOffre.candidatures);
  
            await Offre.collection.insertOne({ ...filtOffre, ...filtForm });
            // await delay(300);
          })
        );
      })
    );
  
    let offres = await Offre.countDocuments();
  
    return { offres };
  };
  
  module.exports = { createOffreCollection };
  