app.config["SEND_EMAIL"] = False
app.config["EMAIL_RECIPIENT"] = "analytics-dashboard@codeforamerica.org"
app.config["EMAIL_SENDER"] = "mike@codeforamerica.org"

logger = logging.getLogger("noteworthy")
logger.setLevel(logging.DEBUG)

handler1 = logging.StreamHandler(sys.stderr)
handler1.setLevel(logging.DEBUG)
logger.addHandler(handler1)

if "SENDGRID_USERNAME" in environ and "SENDGRID_PASSWORD" in environ:
    app.config["SMTP_USERNAME"] = environ["SENDGRID_USERNAME"]
    app.config["SMTP_PASSWORD"] = environ["SENDGRID_PASSWORD"]
    app.config["SMTP_HOSTNAME"] = "smtp.sendgrid.net"
    app.config["SEND_EMAIL"] = True

    handler2 = SMTPHandler(
        app.config["SMTP_HOSTNAME"],
        app.config["EMAIL_SENDER"],
        (app.config["EMAIL_RECIPIENT"], app.config["EMAIL_SENDER"]),
        "City Analytics Dashboard error report",
        (app.config["SMTP_USERNAME"], app.config["SMTP_PASSWORD"]),
    )

    handler2.setLevel(logging.WARNING)
    logger.addHandler(handler2)


@app.errorhandler(builders.SetupError)
with psycopg2.connect(app.config["SQLALCHEMY_DATABASE_URI"]) as connection:
    with connection.cursor() as cursor:
        cursor.execute(open("schema.pgsql", "r").read())

logger = logging.getLogger("noteworthy")
logger.setLevel(logging.DEBUG)

handler1 = logging.StreamHandler(sys.stderr)
handler1.setLevel(logging.DEBUG)
logger.addHandler(handler1)

if "SMTP_USERNAME" in environ and "SMTP_PASSWORD" in environ:
    app.config["SMTP_USERNAME"] = environ["SMTP_USERNAME"]
    app.config["SMTP_PASSWORD"] = environ["SMTP_PASSWORD"]
    app.config["SMTP_HOSTNAME"] = environ["SMTP_HOSTNAME"]
    app.config["SEND_EMAIL"] = environ["SEND_EMAIL"]

    handler2 = SMTPHandler(
        app.config["SMTP_HOSTNAME"],
        app.config["EMAIL_SENDER"],
        (app.config["EMAIL_RECIPIENT"]),
        "City Analytics Dashboard error report",
        (app.config["SMTP_USERNAME"], app.config["SMTP_PASSWORD"]),
    )

    handler2.setLevel(logging.WARNING)
    logger.addHandler(handler2)


@app.errorhandler(builders.SetupError)