"""
Package: app

Package for the application model and services
This module also sets up the logging to be used with gunicorn
"""
import os
import sys
import logging
from flask import Flask

# Create Flask application
app = Flask(__name__)

app.config.from_object("config")
app.config['LOGGING_LEVEL'] = logging.INFO
app.config['API_KEY'] = os.getenv('API_KEY')

# Import the service After the Flask app is created
from service import routes, keys

# Set up logging for production
print("Setting up logging for {}...".format(__name__))
app.logger.propagate = False
if __name__ != "__main__":
    GUNICORN_LOGGER = logging.getLogger("gunicorn.error")
    if GUNICORN_LOGGER:
        app.logger.handlers = GUNICORN_LOGGER.handlers
        app.logger.setLevel(GUNICORN_LOGGER.level)
    app.logger.info("Logging established")

app.logger.info(70 * "*")
app.logger.info("  I N V E N T O R Y   S T O R E   S E R V I C E  ".center(70, "*"))
app.logger.info(70 * "*")

# make our sqlalchemy tables
routes.init_db()

app.logger.info("Service inititalized!")

# If an API Key was not provided, autogenerate one
if not app.config[keys.KEY_API]:
    app.config[keys.KEY_API] = routes.generate_apikey()
    app.logger.info('Missing API Key! Autogenerated: {}'.format(app.config[keys.KEY_API]))
