"use strict";
const path = require("path");
const serve = require("koa-static");
const session = require("koa-generic-session");
const responseTime = require("koa-response-time");
const logger = require("koa-logger");
const compress = require("koa-compress");
const errorHandler = require("koa-error");
const bodyParser = require("koa-bodyparser");
const koaPassport = require('koa-passport');

const config = require('../config');
const passport = require('./passport');

const STATIC_FILES_MAP = {};
const SERVE_OPTIONS = { maxAge: 365 * 24 * 60 * 60 };

module.exports = function(app) {
    passport(koaPassport, config)

    if (!config.app.keys) {
      throw new Error("Please add session secret key in the config file!")
    }

    app.keys = config.app.keys

    if (config.app.env !== "test") {
      app.use(logger())
    }

    app.use(errorHandler())
    
    app.use(serve(path.join(__dirname, '../../assets/develop')))
    app.use(serve(path.join(__dirname, '../../resources')))

    app.use(session({
      key: "blog.sid"
    }))

    app.use(bodyParser())

    app.use(koaPassport.initialize())
    app.use(koaPassport.session())

    app.use(compress())
    app.use(responseTime())
};
