'use strict';

var server = require('server');
//Use super module to extend existing home route
server.extend(module.superModule);
//import userLoggedIn middleware
var userLoggedIn = require('*/cartridge/scripts/middleware/userLoggedIn');

//Use prepend to check for logged in user
server.prepend('Show', userLoggedIn.validateLoggedIn, function (req, res, next) {

    var viewData = res.getViewData();

    viewData.detailText = 'Please log in'; //One method to set view data

    if (req.currentCustomer.profile) {
        viewData.detailText = 'Welcome ' + customer.getProfile().getFirstName();
    }
    
    
    next();
});

//Use append to check for promotion
server.append('Show', function (req, res, next) {

    var promoText = 'There are no Promotions at this time';

    //read query string to check for fromotions
    if (req.querystring.promo == 1) {
		promoText = 'All Electronics are 98% off! Practically free!';
    }
    if (req.querystring.promo == 2) {
		promoText = 'Overnight shipping is free!';
    }

    res.setViewData(
        { promoText: promoText } //Another way to set view data
    );

    next();
});

module.exports = server.exports();
