const tools = require('../tools/general');

console.log('__');
console.log('__');
console.log('__');
console.log('__');
console.log('****************************************************');
console.log('***           AVVIO DEL SERVER NODE.JS           ***');
console.log('****************************************************');
console.log('__');

if ((process.env.NODE_ENV === 'production')) {
  console.log('          *** AMBIENTE DI PRODUZIONE !!!!')
} else if (process.env.NODE_ENV === 'test') {
  console.log('          *** ### AMBIENTE DI TEST ')
} else if (process.env.NODE_ENV === 'development') {
  console.log('          *** ### AMBIENTE DI SVILUPPO (LOCALE) ')
}
console.log('__');

// still in app.js
const node_env = process.env.NODE_ENV || 'production';

var file = `.env.${node_env}`;

// GLOBALI (Uguali per TUTTI)
process.env.LINKVERIF_REG = '/vreg';
process.env.CHECKREVERIF_EMAIL = '/reverif_email';
process.env.LINK_REQUEST_NEWPASSWORD = '/requestnewpwd';
process.env.ADD_NEW_SITE = '/addNewSite';
process.env.LINK_UPDATE_PASSWORD = '/updatepassword';
process.env.LINK_UPDATE_PWD = '/updatepwd';
process.env.KEY_APP_ID = 'KKPPAA5KJK435J3KSS9F9D8S9F8SD98F9SDF';

console.log("Starting Node with: " + file);
require('dotenv').config({ path: file });

process.env.DATABASE = process.env.DATABASE || 'FreePlanet';

console.log('process.env.DOMAIN:', process.env.DOMAIN);
const domain = process.env.DOMAIN;
const username = encodeURIComponent(process.env.MONGODB_USER);
const password = encodeURIComponent(process.env.MONGODB_PWD);
const database = process.env.DATABASE || "test"; // Nome del database, default a 'test'

if (process.env.AUTH_DB_PASSING === "1") {
  // replace username and password in the process.env.DOMAIN
  process.env.MONGODB_URI = process.env.DOMAIN_AUTH.replace('{username}', username).replace('{password}', password) + database;
  process.env.MONGODB_URI_NOPWD = process.env.DOMAIN_AUTH.replace('{username}', username) + database;
} else {
  process.env.MONGODB_URI = domain + database;
  process.env.MONGODB_URI_NOPWD = domain + database;
}

console.log('process.env.MONGODB_URI:', process.env.MONGODB_URI_NOPWD);
