import {Route, SessionAuthMiddleware} from '@extollo/lib'
import {Home} from '../controllers/Home.controller'
import {PageView} from '../middlewares/PageView.middleware'
import {Snippets} from '../controllers/Snippets.controller'
import {GoLinks} from '../controllers/GoLinks.controller'
import {Feed} from '../controllers/Feed.controller'
import {LoadSnippet} from '../middlewares/parameters/LoadSnippet.middleware'
import {LoadFeedPosts} from '../middlewares/parameters/LoadFeedPosts.middleware'
import {ValidContactForm} from '../middlewares/parameters/ValidContactForm.middleware'

Route
    .group('/', () => {
        Route.get('/')
            .parameterMiddleware(LoadFeedPosts)
            .calls<Home>(Home, home => home.welcome)
            .alias('home')

        Route.post('/contact')
            .parameterMiddleware(ValidContactForm)
            .calls<Home>(Home, home => home.contact)
            .alias('contact')

        Route.get('/humans.txt')
            .calls<Home>(Home, home => home.humans)

        Route.get('/technical')
            .calls<Home>(Home, home => home.technical)

        Route.get('/analytics/opt-out')
            .calls<Home>(Home, home => home.optOutPrompt)
            .alias('opt-out-prompt')

        Route.post('/analytics/opt-out')
            .calls<Home>(Home, home => home.optOut)
            .alias('opt-out')

        Route.get('/snippet/:slug')
            .parameterMiddleware(LoadSnippet)
            .calls<Snippets>(Snippets, snippets => snippets.viewSnippet)

        Route.any('/go/:short')
            .calls<GoLinks>(GoLinks, go => go.launch)

        Route.get('/favicon.ico')
            .calls<Home>(Home, home => home.favicon)

        Route.group('feed', () => {
            Route.get('/')
                .parameterMiddleware(LoadFeedPosts, {all: true})
                .calls<Feed>(Feed, feed => feed.feed)
                .alias('feed')

            Route.get('/rss.xml')
                .parameterMiddleware(LoadFeedPosts, {all: true})
                .calls<Feed>(Feed, feed => feed.rss)
                .alias('feed.rss')

            Route.get('/atom.xml')
                .parameterMiddleware(LoadFeedPosts, {all: true})
                .calls<Feed>(Feed, feed => feed.atom)
                .alias('feed.atom')

            Route.get('/json.json')
                .parameterMiddleware(LoadFeedPosts, {all: true})
                .calls<Feed>(Feed, feed => feed.json)
                .alias('feed.json')
        })
    })
    .pre(SessionAuthMiddleware)
    .pre(PageView)
