const respondOnError = require('./src/respond-on-error');
const routeMessage = require('./src/route-message');
const sendToSlack = require('./src/slack-send-message');

// const validateTeam = require('./src/validate-team');
var Sequelize = require('sequelize');
var db        = require('./lib/models/db')(Sequelize);


exports.handler =  function (event, context, callback) {
    db.Team.findOne({ where: { slack_id: event.team_id } })
    .then( (team) => {
        if (team === null) {
            // bail.  We somehow got a message from a team
            // that didn't install the bot.
            console.log("We somehow got a message from a team that didn't install Quackbot.");
            return null;
        } else {
            team.latestAuthorization().then(
                (authorization) => {
                    
                    console.log("Authorization is \n", JSON.stringify(authorization));
                    
                    // add the authorization info to the event
                    event.authorization = authorization[0].details.bot;
                    
                    // Tell the team they're not cool enough.
                    if (!team.verified) {
                        console.log('Team not yet validated by DocumentCloud. Informing user ...');
                        var message = "Hi! I'm still waiting for the folks at DocumentCloud to say you can use my services. I'll let you know when we're ready to go.";
                        return sendToSlack(event, message);
                    } else {

                        console.log('Team Verified, handling message');

                        // Extract command words.
                        const commandWords = event.text.trim().split(/\s+/);
                        
                        // To reach the bot, it must be a DM (in a "D" channel)
                        // or an @-mention at the start of a line.
                        
                        var is_direct_message_to_me = event.channel.match(/^D*/)[0] == "D";
                        var command_starts_with_me = (commandWords[0] == `<@${event.authorization.bot_user_id}>`);
                        
                        if (!is_direct_message_to_me && !command_starts_with_me) {
                            return 'Ignoring message that is none of my beeswax, bye!';
                        }

                        if (command_starts_with_me) {
                            event.command = {
                                verb: commandWords[1].toLowerCase(),
                                predicate: commandWords.splice(2).join(' '),
                            };
                        } else {
                            event.command = {
                                verb: commandWords[0].toLowerCase(),
                                predicate: commandWords.splice(1).join(' '),
                            };
                        }

                        console.log(`Event posted to ${event.stage} stage with verb '${event.command.verb}' and predicate '${event.command.predicate}'.`);

                        return routeMessage(event).catch((message) => respondOnError(event, message) );
                    }
                }
            );
        }
    })
    .then(message => {
        console.log(message);
        callback(null);
    })
    .catch(error => {
        console.error(error.message);
        callback(null);
    });
};


@@ -260,64 +260,8 @@
 ');%0A
-var db        = require('./lib/models/db')(Sequelize);%0A%0A
 %0Aexp
@@ -310,24 +310,83 @@
 callback) %7B%0A
+    var db        = require('./lib/models/db')(Sequelize);%0A
     db.Team.
@@ -3269,24 +3269,390 @@
    %7D%0A    %7D)%0A
+    .then(%0A        function()%7B%0A            db.sequelize.sync().then(function() %7B%0A                console.log(%22handles before:%22, process._getActiveHandles().length);%0A                return db.sequelize.close().then(function() %7B%0A                  console.log(%22handles after:%22, process._getActiveHandles().length);%0A                %7D);%0A              %7D);%0A        %7D%0A    )%0A
     .then(me

Put database instance in the correct place and close out connections.