const express = require('express');
const router = express.Router();
const passport = require('passport');
require('../config/passport')(passport);
const userController = require('../controllers/user');
const sessionController = require('../controllers/session');

/* GET home page. */
router.get('/', function(req, res) {
  // console.log(req.user);
  res.render('index', { user: req.user, conect: req.user });
});

router.get('/login', function(req, res) {
  // console.log(req.flash('loginMessage'));
  // console.log(req.user);
  if (req.user)
    res.redirect('/user');
  else
    res.render('user/login', {
      user: req.user,
      conect: req.user,
      message: req.flash('loginMessage')
    });
});

router.get('/logout', function(req, res) {
  req.logout();
  res.redirect('/');
});

router.get('/signup', function(req, res) {
  console.log(req.user);
  // if(req.user !== undefined && req.user.state === 0)
  //   req.logout();
  res.render('user/signup', {
    user: req.user,
    conect: req.user,
    message: req.flash('signupMessage')
  });
});

// router.post('/signup', passport.authenticate('local-signup', {
//   successRedirect: '/signup',
//   failureRedirect: '/signup',
//   failureFlash: true,
//   session: false,
// }));

router.post('/signup', sessionController.newUser);

router.post(
  '/session',
  passport.authenticate('local-login', {
    successRedirect: '/user',
    failureRedirect: '/login',
    failureFlash: true
  })
);

// router.get('/quizes', quizController.index);
// router.get('/quizes/:quizId(\\d+)', quizController.show);
// router.get('/quizes/:quizId(\\d+)/answer', quizController.answer);

module.exports = router;
