from flask import Flask
from flask_cors import CORS

from graph.exploration.llm.logging_dataset import logging_dataset


def run_create_app(app: Flask, testing_context: bool = False):
    from backends.logging_setup import logging_setup

    logging_setup()

    if not testing_context:
        from .webapp_config import webapp_config

        webapp_config.setup()

        if webapp_config.logging_ds:
            logging_dataset.init(webapp_config.logging_ds)

    from backends.explorer.api import api

    CORS(app)

    app.register_blueprint(api)

    from backends.middlewares import handle_preset_not_set_error  # noqa: F401
    from solutions.graph.models import PresetNotSetException  # noqa: F401

    app.register_error_handler(PresetNotSetException, handle_preset_not_set_error)  # type: ignore

    return app
