const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const expressSanitizer = require('express-sanitizer');
const methodOverride = require('method-override');
const flash = require('connect-flash');
const passport = require('passport');
const LocalStrategy = require('passport-local');
const User = require('./models/user');
const passportLocalMongoose = require('passport-local-mongoose');

    
    
    
//router config
const authRoutes = require('./routes/auth');
const blogRoutes = require('./routes/blogs');
const commentsRoutes = require('./routes/comments');
const dashRoutes = require('./routes/dashboard');
const replyRoutes = require('./routes/replies');

//    commentsRoutes = require('./routes/comments'),
//    authRoutes = require('./routes/index');


//module config
mongoose.connect('mongodb://localhost/blog_app_v3_2');
app.use(bodyParser.urlencoded({extended: true}));
app.use(expressSanitizer());
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
app.use(methodOverride('_method'));
app.use(flash());



//sessions config
app.use(require('express-session')({
    secret: 'Lana... Lana...... Lana......... DANGA ZONE! You\'re in the danga zone.',
    resave: false,
    saveUninitialized: false
}));
app.locals.moment = require('moment');
//passport config
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

//global use
app.use(function(req, res, next){
    res.locals.currentUser = req.user;
    res.locals.error = req.flash('error');
    res.locals.success = req.flash('success');

    next();
    
});


app.get('/', function(req, res){
    res.redirect('/blogs');
});

app.use('/', authRoutes);
app.use('/blogs', blogRoutes);
app.use('/blogs/:id/c', commentsRoutes);
app.use('/dashboard', dashRoutes);
//app.use('/blogs/:id/c/:comment_id/r', replyRoutes);


app.listen(process.env.PORT, process.env.IP, function(){
    console.log('server has started. v3.3');
});







