from flask import Blueprint

from backends.editor.utils.api import use_graph_snapshot_store
from backends.editor.utils.presets import get_webapp_neo4j_preset
from backends.editor.utils.webapp_config import webapp_config
from backends.utils import return_ok
from solutions.graph.models import ConnectionConfiguration
from stores.graph_metadata_snapshot_store import GraphMetadataSnapshotStore

config = Blueprint("config", __name__, url_prefix="/config")


@config.route("/webapp", methods=["GET"])
@use_graph_snapshot_store
def get_webapp_config(snapshot_store: GraphMetadataSnapshotStore):
    data = {
        "llm_id": webapp_config.llm_id,
        "snapshot_feature_available": snapshot_store is not None,
        "publishFeatureAvailable": webapp_config.build_graph_recipe_output_connection is not None,
        "neo4jPresetAvailable": get_webapp_neo4j_preset() is not None,
    }

    return return_ok(data=data)


@config.route("/connections", methods=["GET"])
def get_available_connections():
    neo4j_preset = get_webapp_neo4j_preset()
    connections = [
        ConnectionConfiguration(type="neo4j") if neo4j_preset else None,
        ConnectionConfiguration(type="kuzu"),
    ]

    conn_data = [
        {
            "type": conn["type"],
        }
        for conn in connections
        if conn
    ]

    return return_ok(data=conn_data)
