from dataactbroker.scripts.setupEmails import setupEmails
from dataactcore.scripts.setupJobTrackerDB import setupJobTrackerDB
from dataactcore.scripts.setupErrorDB import setupErrorDB
from dataactcore.scripts.setupUserDB import setupUserDB
from dataactcore.utils.responseException import ResponseException
from dataactbroker.handlers.userHandler import UserHandler
from dataactbroker.handlers.aws.session import SessionTable
from dataactcore.config import CONFIG_BROKER, CONFIG_DB
import argparse
from flask.ext.bcrypt import Bcrypt


def options():

    parser = argparse.ArgumentParser()
    parser.add_argument("-i", "--initialize", action="store_true", help="Runs all of the setup options")
    parser.add_argument("-db", "--setupDB", action="store_true", help="Creates the database schema")
    parser.add_argument("-a", "--createAdmin", action="store_true", help="Creates admin user")
    parser.add_argument("-s", "--start", action="store_true", help="Starts the broker")
    args = parser.parse_args()
    optionsDict = vars(args)

    noArgs = True
    for arg in optionsDict:
        if(optionsDict[arg]):
            noArgs = False
            globals()[arg]()

    if noArgs:
        print ("Please enter an argument.")


def initialize():
    print ("Setting up databases...")
    setupDB()
    print ("Setting up DynamoDB session table...")
    setupSessionTable()
    print ("Creating admin user...")
    createAdmin()
    print ("The broker has been initialized. You may now run the broker with the --start argument.")


def setupDB():
    setupJobTrackerDB(hardReset=True)
    setupErrorDB(True)
    setupUserDB(True)
    setupEmails()


def createAdmin():
    """Create initial admin user."""
    adminEmail = CONFIG_BROKER['admin_email']
    adminPass = CONFIG_BROKER['admin_password']
    userDb = UserHandler()
    try:
        user = userDb.getUserByEmail(adminEmail)
    except ResponseException as e:
        if "no users" in e.message.lower():
            userDb.createUserWithPassword(
                adminEmail, adminPass, Bcrypt(), admin=True)
            user = userDb.getUserByEmail(adminEmail)
            userDb.addUserInfo(user, "Admin", "System", "System Admin")
    userDb.session.close()


def setupSessionTable():
    SessionTable.createTable(CONFIG_BROKER['local'], CONFIG_DB['dynamo_port'])


def start():
    from dataactbroker.app import runApp
    runApp()

if __name__ == '__main__':
    options()

@@ -692,204 +692,8 @@
 s%22)%0A
-    parser.add_argument(%22-db%22, %22--setupDB%22, action=%22store_true%22, help=%22Creates the database schema%22)%0A    parser.add_argument(%22-a%22, %22--createAdmin%22, action=%22store_true%22, help=%22Creates admin user%22)%0A
     
@@ -1375,22 +1375,8 @@
 rDB(
-hardReset=True
 )%0A  
@@ -1386,28 +1386,24 @@
 etupErrorDB(
-True
 )%0A    setupU
@@ -1408,20 +1408,16 @@
 pUserDB(
-True
 )%0A    se

remove extra params