"""SQLite-backed GraphStore — wraps SQLiteStore entity methods."""
from __future__ import annotations

from .base import GraphStore


class SQLiteGraphStore(GraphStore):
    """GraphStore backed by SQLiteStore's entity_nodes/entity_edges tables.

    This is an adapter: it receives a pre-initialised SQLiteStore
    and delegates all graph operations to its existing entity methods.
    """

    def __init__(self, sqlite_store) -> None:  # type: ignore[type-arg]
        self._store = sqlite_store

    # --- Node CRUD ---

    def put_node(self, node: dict) -> None:
        self._store.put_entity(node)

    def get_node(self, entity_id: str) -> dict | None:
        return self._store.get_entity(entity_id)

    def get_node_by_name(self, name: str, user_id: str | None = None) -> dict | None:
        return self._store.get_entity_by_name(name, user_id)

    def delete_node(self, entity_id: str) -> bool:
        return self._store.delete_entity(entity_id)

    # --- Edge CRUD ---

    def put_edge(self, edge: dict) -> None:
        self._store.put_entity_edge(edge)

    def get_edge(self, edge_id: str) -> dict | None:
        return self._store.get_entity_edge(edge_id)

    def delete_edge(self, edge_id: str) -> bool:
        return self._store.delete_entity_edge(edge_id)

    # --- Search & Traversal ---

    def search_edges(
        self,
        user_id: str | None = None,
        entity_name: str = "",
        predicate: str | None = None,
        direction: str = "both",
        limit: int = 20,
        partial_match: bool = False,
    ) -> list[dict]:
        return self._store.search_entity_edges(
            user_id=user_id,
            entity_name=entity_name,
            predicate=predicate,
            direction=direction,
            limit=limit,
            partial_match=partial_match,
        )

    def traverse(
        self,
        user_id: str | None = None,
        start_name: str = "",
        max_hops: int = 2,
        limit_edges_per_hop: int = 100,
    ) -> tuple[list[dict], list[dict]]:
        return self._default_bfs_traverse(
            user_id=user_id,
            start_name=start_name,
            max_hops=max_hops,
            limit_edges_per_hop=limit_edges_per_hop,
        )

    def list_edges(
        self,
        user_id: str | None = None,
        limit: int = 100,
    ) -> list[dict]:
        return self._store.list_entity_edges(user_id=user_id, limit=limit)

    # --- Stats ---

    def get_stats(self, user_id: str | None = None) -> dict:
        return self._store.get_entity_stats(user_id)
