import path from 'path';
import {merge} from 'lodash';

// Default configuations applied to all environments
const defaultConfig = {
  env: process.env.NODE_ENV,
  get envs() {
    return {
      test: process.env.NODE_ENV === 'test',
      development: process.env.NODE_ENV === 'development',
      production: process.env.NODE_ENV === 'production'
    }
  },
  apiVersion: require('../../package.json').apiVersion,
  root: path.normalize(__dirname + '/../../..'),
  port: process.env.PORT || 4567,
  ip: process.env.IP || '0.0.0.0',
  loanManagementApi: {
    baseUrl: process.env.loanManagementApiBaseUrl || 'url',
    privateKey: process.env.loanManagementApiPrivateKey || 'private-key',
    publicKey: process.env.loanManagementApiPublicKey || 'public-key'
  },
  auth0: {
    secret: 'secret',
    client: 'client',
    domain: 'https://likelyloans.eu.auth0.com/'
  },
  /**
   * MongoDB configuration options
   */
  mongo: {
    seed: true,
    options: {
      db: {
        safe: true
      }
    }
  },

  /**
   * Security configuation options regarding sessions, authentication and hashing
   */
  security: {
    sessionSecret: process.env.SESSION_SECRET || 'i-am-the-secret-key',
    sessionExpiration: process.env.SESSION_EXPIRATION || 60 * 60 * 24 * 7, // 1 week
    saltRounds: process.env.SALT_ROUNDS || 12
  }
};

// Environment specific overrides
const environmentConfigs = {
  development: {
    mongo: {
      uri: process.env.MONGO_URI || 'mongodb://localhost/development'
    },
    security: {
      saltRounds: 4
    }
  },
  test: {
    port: 5678,
    mongo: {
      uri: process.env.MONGO_URI || 'mongodb://localhost/test'
    },
    security: {
      saltRounds: 4
    }
  },
  production: {
    mongo: {
      seed: false,
      uri: process.env.MONGO_URI
    }
  }
};

// Recursively merge configurations
export default merge(defaultConfig, environmentConfigs[process.env.NODE_ENV] || {});
