from __future__ import annotations

from solutions.graph.models import DbType

KUZU_CONCURRENT_RECIPE_ERROR_MESSAGE = (
    "This recipe cannot access the built-in Kuzu graph because another recipe is already using it "
    "with a conflicting file lock. "
    "Kuzu graphs stored in managed folders do not support concurrent downstream recipe runs when one "
    "of them needs write access, such as Compute PageRank. "
    "Run the recipes sequentially, or export the graph to Neo4j if you need concurrent execution."
)


def rewrite_kuzu_recipe_runtime_error(error: RuntimeError, database_type: DbType | None) -> RuntimeError:
    if database_type != "kuzu":
        return error

    error_message = str(error)
    if "IO exception" not in error_message or "lock on file" not in error_message:
        return error

    return RuntimeError(KUZU_CONCURRENT_RECIPE_ERROR_MESSAGE)
