from flask import Blueprint, request

from backends.editor.queries.graph_data import RunCypherParams
from backends.editor.queries.graph_data import run_cypher as run_cypher_query
from backends.editor.utils.api import use_db_handler, use_graph_metadata_store
from backends.editor.utils.webapp_config import webapp_config
from backends.utils import return_ok
from graph.connections.db_handler import DbHandler
from stores.graph_metadata_store import GraphMetadataStore

graph_cypher = Blueprint("cypher", __name__, url_prefix="/cypher")


@graph_cypher.route("/run", methods=["POST"])
@use_graph_metadata_store
@use_db_handler(id_getter=lambda: request.get_json().get("graph_id"))
def run_cypher(graph_store: GraphMetadataStore, db_handler: DbHandler):
    params = RunCypherParams(**request.get_json())

    result = run_cypher_query(graph_store, db_handler, params, webapp_config.db_query_timeout_seconds)

    return return_ok(data=result)
