'use strict';
const path = require( 'path' );
const fs = require( 'fs' );
const config = {};
if( !process.env.NODE_ENV ) process.env.NODE_ENV = "dev"
console.log( 'apixtribe process.env.NODE_ENV: ', process.env.NODE_ENV );
// VOIR l'ancien fichier de cnfig au cas ou il manque des chemins dans config
// voir la doc http://gitlab.ndda.fr/philc/apiamaildigitfr/wikis/InstallConf
config.prod = {
	mainDir: __dirname,
	tmp: path.join( __dirname, '/tmp' ),
	public: path.join( __dirname, '/public' ),
	archivefolder: path.join( __dirname, '/archive' ),
	rootURL: '{{setupdns}}',
	domain: path.join( __dirname, '/data/tribee' ),
	withssl: "{{withssl}}",
	SSLCredentials: {
		key: path.join( __dirname, '/data/certs/{{setupdns}}.key' ),
		cert: path.join( __dirname, '/data/certs/{{setupdns}}.crt' ),
		ca: path.join( __dirname, '/data/certs/{{setupdns}}.csr' )
	},
	port: {
		http: "{{httpport}}",
		https: "{{httpsport}}"
	},
	jwtSecret: '{{jwtsecretkey}}',
	saltRounds: 10,
	languagesAvailable: [ 'fr', 'en', 'it', 'de', 'ru' ],
	lg: {},
	exposedHeaders: {
		'x-auth': 'xauth',
		'x-uuid': 'xuuid',
		'x-language': 'xlang',
		'x-client-id': 'xtribeid',
		'x-workOn': 'xworkOn',
		'x-app': 'xapp'
	},
	bodyparse: {
		urlencoded: {
			limit: '50mb',
			extended: true
		},
		json: { limit: '500mb' }
	}
};
// Development and test config
// apixtribe.local.fr

config.dev = {
	mainDir: __dirname,
	tmp: path.join( __dirname, '/tmp' ),
	public: path.join( __dirname, '/public' ),
	//public allow to serve on /public file into folder /public with or without login
	archivefolder: path.join( __dirname, '/archive' ),
	rootURL: 'apixtribe.local.fr',
	domain: path.join( __dirname, '/data/tribee' ),
	withssl: "YES",
	SSLCredentials: {
		key: path.join( __dirname, '/setup/data/certs/apixtribe.local.fr.key' ),
		cert: path.join( __dirname, '/setup/data/certs/apixtribe.local.fr.crt' ),
		ca: path.join( __dirname, '/setup/data/certs/apixtribe.local.fr.csr' )
	},
	port: {
		http: "{{httpport}}",
		https: "{{httpsport}}"
	},
	jwtSecret: 'dnsqd515+npsc^dsqdsqd^d$qdd$$$dŝqdze154615ae.Dsd:sqd!',
	// Avoid authentification for uuid2 and this auth token
	// Always remove this from production
	devnoauthxuuid: "2",
	devnoauthxauth: "autoriserparlapatrouille",
	saltRounds: 10,
	languagesAvailable: [ 'fr', 'en', 'it', 'de', 'ru' ],
	lg: {},
	exposedHeaders: {
		'x-auth': 'xauth',
		'x-uuid': 'xuuid',
		'x-language': 'xlang',
		'x-client-id': 'xtribeid',
		'x-workOn': 'xworkOn',
		'x-app': 'xapp'
	},
	bodyparse: {
		urlencoded: {
			limit: '50mb',
			extended: true
		},
		json: { limit: '500mb' }
	}
};
if( !config[ process.env.NODE_ENV ] ) {
	console.log( 'config.js -> Exit setup due to node_ENV have to be set as prod or dev instead of ', process.env.NODE_ENV )
	process.exit();
}
const confuse = config[ process.env.NODE_ENV ];
if( confuse.withssl == "YES" ) {
	if( !fs.existsSync( confuse.SSLCredentials.key ) ) {
		const prompt = require( 'prompt-sync' )( { sigint: true } );
		//const repdata = ( process.NODE_ENV == "dev" ) ? 'devdata' : 'data';
		const reinit = prompt( `Missing file to ssl ${confuse.SSLCredentials.key}, please run the process letsencrypt to get a ssl certificat or reinit project (type reinit will erase ) and answer no to question: Do you want to manage ssl : ` );
		if( reinit == 'reinit' ) {
			fs.removeSync( `${__dirname}/config.js` );
			fs.removeSync( `${__dirname}/data` )
		}
		process.exit();
	}
	confuse.SSLCredentials.key = fs.readFileSync( confuse.SSLCredentials.key, 'utf8' );
	confuse.SSLCredentials.cert = fs.readFileSync( confuse.SSLCredentials.cert, 'utf8' );
	confuse.SSLCredentials.ca = fs.readFileSync( confuse.SSLCredentials.ca, 'utf8' );
}
module.exports = confuse;
