/**
 * Force load with https on production environment
 * https://devcenter.heroku.com/articles/http-routing#heroku-headers
 */
module.exports = function(environments, status) {
  environments = environments || ["production"];
  status = status || 301;
  // console.log("New force SSL ");
  // console.log("\tenvs : ", environments);
  // console.log("\tstatus: ", status);
  // console.log("\tENV : ", process.env.NODE_ENV);
  return function(req, res, next) {
    if (environments.indexOf(process.env.NODE_ENV) >= 0) {
      if (req.headers["x-forwarded-proto"] !== "https") {
        const urlToRedirectTo = `https://${req.hostname}${req.originalUrl}`;
        // console.log("\tRedirect :", urlToRedirectTo);
        res.redirect(status, urlToRedirectTo);
      } else {
        next();
      }
    } else {
      next();
    }
  };
};
