import pytest
from project_brain.graph.json_store import JsonStore
from project_brain.build.link_engine import LinkEngine


@pytest.fixture
def store_with_data(tmp_dir):
    store = JsonStore(tmp_dir)
    store.add_node({"id": "doc_1", "type": "document", "title": "Design Doc", "path": "docs/design.md"})
    store.add_node({"id": "sec_1", "type": "section", "title": "OrderService", "level": 2, "doc_id": "doc_1", "content": "The OrderService handles orders"})
    store.add_node({"id": "fn_1", "type": "function", "name": "OrderService", "signature": "class OrderService"})
    store.add_node({"id": "fn_2", "type": "function", "name": "create_order", "signature": "def create_order()"})
    store.add_node({"id": "cls_1", "type": "class", "name": "PaymentProcessor"})
    return store


def test_build_explicit_links(store_with_data):
    engine = LinkEngine(store_with_data)
    edges = engine.build_explicit_links()

    assert len(edges) >= 1
    describe_edges = [e for e in edges if e.type == "DESCRIBE"]
    assert len(describe_edges) >= 1


def test_build_all_links(store_with_data):
    engine = LinkEngine(store_with_data)
    edges = engine.build_all_links()
    assert len(edges) >= 1


def test_no_links_when_no_docs(tmp_dir):
    store = JsonStore(tmp_dir)
    store.add_node({"id": "fn_1", "type": "function", "name": "foo"})

    engine = LinkEngine(store)
    edges = engine.build_explicit_links()
    assert len(edges) == 0


def test_build_section_describe_edges(store_with_data):
    engine = LinkEngine(store_with_data)
    edges = engine.build_explicit_links()
    section_edges = [e for e in edges if e.source == "sec_1" and e.type == "DESCRIBE"]
    assert len(section_edges) >= 1
    targets = {e.target for e in section_edges}
    assert "fn_1" in targets
    for e in section_edges:
        assert e.properties.get("method") == "section"


def test_replace_all_links_removes_stale_describe_edges(store_with_data):
    from project_brain.graph.models import Edge

    store_with_data.add_edge(Edge(
        id="doc_1->missing:DESCRIBE",
        type="DESCRIBE",
        source="doc_1",
        target="missing",
    ))
    engine = LinkEngine(store_with_data)
    replacements = engine.replace_all_links()

    describe_edges = [
        edge for edge in store_with_data.get_all_edges()
        if edge.type == "DESCRIBE"
    ]
    assert {edge.id for edge in describe_edges} == {edge.id for edge in replacements}
    assert all(edge.target != "missing" for edge in describe_edges)


def test_short_symbols_require_identifier_boundaries(tmp_dir):
    store = JsonStore(tmp_dir)
    store.add_node({"id": "doc", "type": "document", "title": "invalid runtime"})
    store.add_node({
        "id": "section",
        "type": "section",
        "doc_id": "doc",
        "title": "Identifiers",
        "content": "id and run are APIs; invalid and runtime are ordinary words",
    })
    store.add_node({"id": "fn_id", "type": "function", "name": "id"})
    store.add_node({"id": "fn_run", "type": "function", "name": "run"})
    store.add_node({"id": "fn_time", "type": "function", "name": "time"})

    edges = LinkEngine(store).build_explicit_links()
    targets = {edge.target for edge in edges}

    assert {"fn_id", "fn_run"} <= targets
    assert "fn_time" not in targets


def test_qualified_symbol_text_matches_identifier_component(tmp_dir):
    store = JsonStore(tmp_dir)
    store.add_node({"id": "doc", "type": "document", "title": "ns::PaymentProcessor"})
    store.add_node({"id": "cls", "type": "class", "name": "PaymentProcessor"})

    edges = LinkEngine(store).build_explicit_links()

    assert [(edge.source, edge.target) for edge in edges] == [("doc", "cls")]


def test_han_symbol_matches_inside_unsegmented_prose(tmp_dir):
    store = JsonStore(tmp_dir)
    store.add_node({"id": "doc", "type": "document", "title": "介绍图谱功能"})
    store.add_node({"id": "fn", "type": "function", "name": "图谱"})

    edges = LinkEngine(store).build_explicit_links()

    assert [(edge.source, edge.target) for edge in edges] == [("doc", "fn")]