'use strict'
require('newrelic')
const express = require('express')
const Alexa = require('alexa-app')
Alexa.App = Alexa.app // this is stupid
const Redis = require('ioredis')

const redis = new Redis(process.env.REDIS_URL || 'localhost:6379')
const handlers = require('./handlers')(redis)

const app = express()
const alexaApp = new Alexa.App(process.env.APP_NAME || 'alexa')

app.set('view engine', 'ejs')
alexaApp.express({
  endpoint: 'alexa',
  expressApp: app,
  checkCert: process.env.ENV === 'production',
  debug: !(process.env.ENV === 'production')
})

alexaApp.launch((req, res) => res.say('Try asking about the next train on an operator from a station.'))
alexaApp.intent('trainIntent', {
  'slots': {
    'ON': 'LITERAL',
    'FROM': 'LITERAL',
    'TO': 'LITERAL'
  },
  'utterances': [
    'when {is|are} the next {train|trains} on {patco|ON} from {haddonfield|FROM}',
    'when {is|are} the next {train|trains} on {patco|ON} from {haddonfield|FROM} to {philadelphia|TO}',
    'next {train|trains} on {patco|ON} from {haddonfield|FROM}',
    'next {train|trains} on {patco|ON} from {haddonfield|FROM} to {philadelphia|TO}'
  ]
}, (req, res) => new Promise((resolve, reject) => handlers.alexa(req)
  .errors(err => res.say(err.message))
  .tap(response => res.say(response))
  .done(resolve)))

app.get('/next/:ON/:FROM/:TO', (req, res, next) => handlers.next(req.params)
  .tap(json => res.json(json))
  .toCallback(err => next(err)))

app.get('/next/:ON/:FROM', (req, res, next) => handlers.next(req.params)
  .tap(json => res.json(json))
  .toCallback(err => next(err)))

app.listen(process.env.PORT || 8080, () =>
  console.log(`listening on ${process.env.PORT || 8080} in ${process.env.ENV || 'development'} mode`))

@@ -6,16 +6,54 @@
 strict'%0A
+if (process.env.ENV === 'production') 
 require(
@@ -966,58 +966,87 @@
 on %7B
-patco%7CON%7D from %7Bhaddonfield%7CFROM%7D to %7Bphiladelphia
+redwood transit system%7CON%7D from %7Bfourteenth at b street%7CFROM%7D to %7Bbayshore mall
 %7CTO%7D
@@ -1146,58 +1146,87 @@
 on %7B
-patco%7CON%7D from %7Bhaddonfield%7CFROM%7D to %7Bphiladelphia
+redwood transit system%7CON%7D from %7Bfourteenth at b street%7CFROM%7D to %7Bbayshore mall
 %7CTO%7D

Include multi-word utterances