const express = require('express');
const app = express();

// grab app and node.js process data
const pjson = require('../package.json');
const { versions } = require('node:process');

// register app and node.js versions with views;
app.locals.app_version = pjson.version;
app.locals.node_version = versions.node;

// initialize database
const sequelize = require('./models');
sequelize.sync({}).then(function(){
  console.log('database has been synced');
}).catch(function(){
  console.log('unable to sync database');
});

// enable sessions
const session = require("express-session");
app.use(session({ resave: true, secret: "123456", saveUninitialized: true }));

// enable body-parser to read form data
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));

// enable the Twig template engine
app.set('view engine', 'twig');
app.set('twig options', {
    allowAsync: true,
    strict_variables: false
});

// enable serving static files
app.use(express.static('public'));

// load routes to express
require('./routes')(app);

module.exports = app;
