const config = require("./config.js");
const express = require('express')
const app = express()

var bodyParser = require('body-parser');
app.use(bodyParser.json())

const cors = require('cors');
app.use(cors());
// Enable preflight requests for all routes
app.options('*', cors());


// Session support
const session = require('express-session');
app.use(session({
  secret: 'jucundus.ses',
  resave: false,
  saveUninitialized: true,
  cookie: { secure: true }
}))
// const MongoStore = require('connect-mongo');

// const keys = require("./.Keys.js");

// const sassionConfig = {
//   ...config.session.sessionConfig,
//   secret: keys.session,
//   store: MongoStore.create({
//     mongoUrl: `${config.db.connectionString}/${config.db.dbName}`,
//     collection: config.session.sessionCollection,
//     stringify: false,
//     autoReconnect: true,
//     autoRemove: 'native'
//   })};

// app.use(session(sassionConfig));

// Authentication
const passport = require('passport');
app.use(passport.initialize());
app.use(passport.session());

app.use('/', require('./routes/auth'));


// Agenda Scheduller
const agenda = require('./services/agenda');
(async function() {

    //lunch sheduller
    await agenda.start();

    //create first user
    const { UserDb } = require('./services/userDb');
    const userDb = await UserDb.init();
    userDb.creatFirstUserifEmpty();

})();

// Agenda UI
var Agendash = require("agendash");
app.use("/dash", Agendash(agenda));

// routes
app.use('/api/user', require('./routes/user'));
app.use('/api/lot', require('./routes/lot'));
app.use('/api/sale', require('./routes/sale'));
app.use('/api/favorite', require('./routes/favorite'));

module.exports = app