const Sequelize = require('sequelize');
const express = require("express");
const bodyParser = require("body-parser");
require('dotenv').config({ path: '.env.local' });

const database = new Sequelize({
  dialect: 'sqlite',
  storage: './xmas.sqlite',
});

const Attendee = database.define('attendee', {
  name: Sequelize.STRING,
  guest: Sequelize.BOOLEAN,
});

class App {
    //Run configuration methods on the Express instance.
    constructor() {
        this.express = express();
        this.middleware();
        this.routes();
    }
    // Configure Express middleware.
    middleware() {
        // this.express.use(logger('dev'));
        this.express.use(bodyParser.json());
        this.express.use(bodyParser.urlencoded({ extended: false }));
        // this.express.engine('html', require('ejs').renderFile);
    }
    // Configure API endpoints.
    routes() {
        /* This is just to get up and running, and to make sure what we've got is
         * working so far. This function will change when we start to add more
         * API endpoints */
        this.express.get('/test', (req, res) => {
          console.log(req)
          res.json({response: 'yo mamma'})
        })

        this.express.post('/attendee', (req, res) => {
          console.log(req.body)
          // Attendee.findOrCreate({ name: 'Emmanuel', guest: true }).then(() =>
          Attendee.findOrCreate({
            where: {
              name: 'Emmanuel'
            }, defaults: {name: 'Emmanuel', guest: true}
          })
          .spread((param1, param2) => {
            console.log('Param 1')
            console.log(param1)
            console.log('Param 2')
            console.log(param2)
          })
          res.json({response: 'yeah we got you'})
        })
    }
}

const app = new App().express
app.listen(3002)


database.sync().then(() => {
    console.log(`Listening on port ${3002}`);
});
