var express = require('express');
var router = express.Router();
var db = require('../models');
var User = db.User;
var Messages = db.Messages;
var Session = db.Session;
var lib = require('../lib');

/* GET home page. */
router.get('/', async function(req, res, next) {
  console.log(req.cookies.sessionid);
  var sessionId = req.cookies.sessionid;
  var user = await lib.getAuthUser(sessionId);
  if(user) {
    var messages = await user.getMessages({
      order: [
        ['id', 'DESC'],
      ],
      limit: 10
    });
    var messageCount = await user.countMessages();
    res.render('index', { auth:user, messages:messages, messageCount:messageCount });
  } else {
    res.redirect('/login');
  }
  
});

module.exports = router;
