"""
Main package for Rookeries.

:copyright: Copyright 2013-2017, Dorian Pula <dorian.pula@amber-penguin-software.ca>
:license: AGPL v3+
"""

import logging

import flask

from rookeries import config, errors, main, logging_config, security

__author__ = 'Dorian Pula'
__version__ = '0.6.3'


logging_config.configure_logging()
logger = logging.getLogger(__name__)


def make_rookeries_app(quiet_log=False):
    """
    Creates and setups a Rookeries app.

    :param quiet_log: Silence the logging when the app starts.  Used by utilities that use the app but not as server.
    :return: A Rookeries app.
    """
    if quiet_log:
        logging_config.configure_logging('CRITICAL')
    logger.info("*** Rookeries API Server - version %s ***", __version__)
    logger.info("Starting Rookeries API server...")

    rookeries_app = flask.Flask(
        import_name=__name__,
        template_folder='../templates',
        static_folder='../static')
    errors.attach_error_handlers_to_api_app(rookeries_app)
    rookeries_app.register_blueprint(main.rookeries_app)

    logger.info("Configuring Rookeries:")
    rookeries_app = config.configure_app(rookeries_app)

    for config_key, value in rookeries_app.config.items():
        logger.info(f"\t{config_key}: {value}")

    logger.info('Initialize components...')
    security.jwt.init_app(rookeries_app)

    logger.info("Rookeries API started!")
    return rookeries_app
