let axios = require("axios");
let admin = require("firebase-admin");
var fs = require("fs");
let serviceAccount = require("./service-key.json");
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
});
let db = admin.firestore();

function runMentorBuilder() {
  db.collection("FlatLine Records")
    .get()
    .then((snap) => {
      let promiseList = [];
      snap.forEach((element) => {
        //console.log(element.id)
        promiseList.push(
          axios.get("https://financementorbox.com/mentors/" + element.id)
        );
      });
      Promise.all(promiseList).then((list) => {
        let promiseList = [];
        for (let i of list) {
          let arr = i.request.path.split("/");
          //console.log(Object.keys(i),i.request.path)//arr[arr.length-1])
          promiseList.push(
            fs.writeFile(
              `site/mentors/${arr[arr.length - 1]}.html`,
              i.data,
              function (err) {
                if (err) throw err;
                console.log("File created successfully.");
              }
            )
          );
          //console.log(i);
        }
        //   Promise.all(promiseList).then(list=>{
        //     console.log(list.length)
        //   })
      });
    });
}
//console.log('DataBase',db)
module.exports = runMentorBuilder;