from flask import Flask
import os

from .config import Config
from .extensions import db
from .libraries.flask_login_pum import LoginManager


def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)

    db.init_app(app)

    login_manager = LoginManager()
    login_manager.login_view = 'auth.login'
    # login_manager.roles_view = 'main.profile'
    login_manager.init_app(app)

    from .models.users import User
    @login_manager.user_loader
    def load_user(user_id): #reload user object from the user ID stored in the session
        # since the user_id is just the primary key of our user table, use it in the query for the user
        return User.query.get(int(user_id))

    # Register blueprints here
    from .routes.main import bp as main_bp
    app.register_blueprint(main_bp)

    from .routes.auth import bp as auth_bp
    app.register_blueprint(auth_bp)

    @app.route('/test/')
    def test_page():
        return '<h1>Board-Manager application is running, at least for this page !</h1>'

    return app