// GLOBAL
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const logger = require('morgan');
const path = require('path');
const open = require('open');

// LOCAL
const tools = require('../utils/tools');

// SERVER
const app = express();
app.set('port', (process.env.PORT || 3000));
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(logger('dev'));
app.use(express.static(path.join(__dirname, 'public')));

// ROUTES DEFS
app.use('/api', require('./routes'));

// STARTING - opening html page if prod flag is passed
app.listen(app.get('port'), () => {
  const { prod, stats, packageInit } = tools.getArgs();
  if (prod) {
    if (stats) {
      open('http://localhost:3000/stats.html');
    } else if (packageInit) {
      open('http://localhost:3000/package-init.html');
      console.log('open your browser at : http://localhost:3000/package-init.html');
    } else {
      open('http://localhost:3000');
    }
  }
  console.log('We are live on ' + app.get('port'));
});
