"""Integration tests for NetworkX conversion — requires a running PostgreSQL."""

from __future__ import annotations

import pandas as pd
import pytest

from piggie._integrations.networkx import to_networkx

pytestmark = pytest.mark.integration


class TestToNetworkx:
    @pytest.fixture(autouse=True)
    def _setup_graph(self, fresh_graph):
        db = fresh_graph
        self.db = db
        # Create a small social graph
        nodes = pd.DataFrame([
            {"name": "Alice"}, {"name": "Bob"}, {"name": "Carol"},
        ])
        db.load_nodes(nodes, "Person")
        edges = pd.DataFrame([
            {"name": "Alice", "friend": "Bob"},
            {"name": "Bob", "friend": "Carol"},
            {"name": "Carol", "friend": "Alice"},
        ])
        db.load_edges(
            edges, "KNOWS",
            source_col="name", source_label="Person",
            target_col="friend", target_label="Person",
            target_key="name",
        )

    def test_basic_conversion(self):
        G = to_networkx(self.db, self.db.graph)
        assert G.number_of_nodes() == 3
        assert G.number_of_edges() == 3

    def test_directed(self):
        import networkx as nx
        G = to_networkx(self.db, self.db.graph, directed=True)
        assert isinstance(G, nx.DiGraph)

    def test_undirected(self):
        import networkx as nx
        G = to_networkx(self.db, self.db.graph, directed=False)
        assert isinstance(G, nx.Graph)

    def test_node_properties(self):
        G = to_networkx(self.db, self.db.graph, node_properties=True)
        for _, data in G.nodes(data=True):
            assert "name" in data

    def test_label_filter(self):
        G = to_networkx(self.db, self.db.graph, labels=["Person"])
        assert G.number_of_nodes() == 3

    def test_rel_type_filter(self):
        G = to_networkx(self.db, self.db.graph, rel_types=["KNOWS"])
        assert G.number_of_edges() == 3
