# Standard Library
from pathlib import Path

# Pip
from dotenv import load_dotenv
from flask import Flask

# Local
from .homescreen import homescreen
from .mainapp import mainapp

load_dotenv()


def create_app():
    """Creates the Flask App"""
    # create and configure the app
    app = Flask(__name__, instance_relative_config=True)
    app.config["APPLICATION_ROOT"] = "/hskankicreator"

    # ensure the instance folder exists
    instance_path = Path(app.instance_path)
    instance_path.mkdir(parents=True, exist_ok=True)

    # configs
    app.config.from_mapping(
        SECRET_KEY="dev",
        DATABASE=instance_path / "flaskr.sqlite",
    )

    app.config.from_pyfile("config.py", silent=True)

    for blueprint in (homescreen, mainapp):
        # for blueprint in (homescreen,):
        app.register_blueprint(blueprint)

    return app
