const Controller = require('libflitter/controller/Controller')

/*
 * Home Controller
 * -------------------------------------------------------------
 * Controller for the main homepage of this Flitter app. Methods here
 * are used as handlers for routes specified in the route files.
 */
class Home extends Controller {
    static get services() {
        return [...super.services, 'Vue', 'models']
    }

    /*
     * Serve the main welcome page.
     */
    async welcome(req, res){
        const Setting = this.models.get('Setting')
        if ( await Setting.get('home.allow_landing') && !(req.user && await Setting.get('home.redirect_authenticated')) ) {
            return res.page('welcome', {
                user: req.user,
                ...this.Vue.data(),
            })
        }

        return res.redirect('/dash')
    }

    async tmpl(req, res) {
        return res.page('tmpl', {...this.Vue.data(), ...this.Vue.session(req)})
    }

    async log_front_end_error(req, res, next) {
        const FrontEndError = this.models.get('FrontEndError')
        await FrontEndError.log(req)
        return res.api()
    }
}

module.exports = Home
