if __name__ == "__main__":
    print("You are running app.py as the main file. This is not supported and causes import errors. Execute with 'flask run' instead")
    exit(-1)

import os

from flask import Flask, Config

app = Flask("api")
config = app.config

config.from_pyfile(os.path.join(os.getcwd(), os.environ["VIDEOAG_API_CONFIG"]))
if "VIDEOAG_API_TEST_CONFIG_OVERRIDE" in os.environ:
    override_config = Config(config.root_path)
    override_config.from_pyfile(os.path.join(os.getcwd(), os.environ["VIDEOAG_API_TEST_CONFIG_OVERRIDE"]))
    # Merge the config files manually to also merge dicts (instead of one overwriting the other)
    for key, value in override_config.items():
        if key not in config:
            config[key] = value
            continue
        if isinstance(value, dict) and isinstance(config[key], dict):
            config[key] |= value
        else:
            config[key] = value

# A key is required for flask (This key is used to sign the cookies)
if "SECRET_KEY" not in config:
    config["SECRET_KEY"] = os.urandom(24)  # pragma: no cover

# Import routes AFTER initialization
# noinspection PyUnresolvedReferences
import api.routes