from flask import Flask



def create_app():
    app = Flask(__name__)
    app.config.from_object('acks.default_settings')
    app.config.from_envvar('FLASK_SETTINGS_FILE')

    # Prep the database
    from acks.models import db
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///../data/acks.db'
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    db.init_app(app)

    # Prep basic auth
    from acks.views import basic_auth, player_auth
    basic_auth.init_app(app)
    player_auth.init_app(app)

    # Load our views
    from acks.views import default_views
    app.register_blueprint(default_views)

    from acks.npc.views import npc_views
    app.register_blueprint(npc_views)

    from acks.quest.views import quest_views
    app.register_blueprint(quest_views)

    # Load our CLI commands
    from acks.commands import default_cli
    app.cli.add_command(default_cli)

    from acks.npc.commands import npc_cli
    app.cli.add_command(npc_cli)

    # Load the Admin views
    from flask_admin import Admin
    from flask_admin.contrib.sqla import ModelView
    admin = Admin(app, name='acks', template_mode='bootstrap3')

    from acks.npc.models import admin_models as npc_admin_models
    for mdl in npc_admin_models:
        admin.add_view(ModelView(mdl, db.session))

    # Initialize API and load resources
    from flask_potion import Api, ModelResource
    api = Api(app, prefix='/api')
    # from acks.api import api
    # api.init_app(app)

    from acks.npc.api import resources as npc_resources
    for resource in npc_resources:
        api.add_resource(resource)

    return app
