import pytest
from project_brain.graph.json_store import JsonStore
from project_brain.query.index import KeywordIndex, SymbolIndex, FileIndex
from project_brain.query.engine import QueryEngine
from project_brain.reasoning.engine import ReasoningEngine
from project_brain.graph.models import Edge


@pytest.fixture
def engine(tmp_dir):
    store = JsonStore(tmp_dir)
    kw_idx = KeywordIndex()
    sym_idx = SymbolIndex()
    file_idx = FileIndex()

    # Build a realistic graph
    store.add_node({"id": "fn_create", "type": "function", "name": "create_order", "signature": "def create_order()", "path": "src/order.py", "file_id": "file_order", "line_start": 10, "line_end": 20, "module_id": "mod_order", "visibility": "public"})
    store.add_node({"id": "fn_validate", "type": "function", "name": "validate_order", "signature": "def validate_order()", "path": "src/order.py", "file_id": "file_order", "line_start": 25, "line_end": 35, "module_id": "mod_order", "visibility": "public"})
    store.add_node({"id": "fn_process", "type": "function", "name": "process_payment", "signature": "def process_payment()", "path": "src/payment.py", "file_id": "file_payment", "line_start": 10, "line_end": 20, "module_id": "mod_payment", "visibility": "public"})
    store.add_node({"id": "fn_notify", "type": "function", "name": "notify_user", "signature": "def notify_user()", "path": "src/notification.py", "file_id": "file_notify", "line_start": 5, "line_end": 15, "module_id": "mod_notify", "visibility": "public"})
    store.add_node({"id": "fn_api", "type": "function", "name": "api_handler", "signature": "def api_handler()", "path": "src/api.py", "file_id": "file_api", "line_start": 10, "line_end": 30, "module_id": "mod_api", "visibility": "public"})
    store.add_node({"id": "cls_order", "type": "class", "name": "OrderService", "kind": "class", "path": "src/order.py", "file_id": "file_order", "line_start": 1, "line_end": 50, "module_id": "mod_order"})
    store.add_node({"id": "mod_order", "type": "module", "name": "order", "path": "src/order"})
    store.add_node({"id": "mod_payment", "type": "module", "name": "payment", "path": "src/payment"})
    store.add_node({"id": "mod_notify", "type": "module", "name": "notification", "path": "src/notification"})
    store.add_node({"id": "mod_api", "type": "module", "name": "api", "path": "src/api"})
    store.add_node({"id": "test_create", "type": "test", "name": "test_create_order", "path": "tests/test_order.py", "file_id": "file_test", "line_start": 5, "framework": "pytest"})
    store.add_node({"id": "test_validate", "type": "test", "name": "test_validate", "path": "tests/test_order.py", "file_id": "file_test", "line_start": 15, "framework": "pytest"})
    store.add_node({"id": "doc_1", "type": "document", "title": "Order Design", "path": "docs/order.md"})

    # Edges
    store.add_edge(Edge(id="e1", type="CALL", source="fn_api", target="fn_create"))
    store.add_edge(Edge(id="e2", type="CALL", source="fn_create", target="fn_validate"))
    store.add_edge(Edge(id="e3", type="CALL", source="fn_create", target="fn_process"))
    store.add_edge(Edge(id="e4", type="CALL", source="fn_create", target="fn_notify"))
    store.add_edge(Edge(id="e5", type="CONTAIN", source="mod_order", target="fn_create"))
    store.add_edge(Edge(id="e6", type="CONTAIN", source="mod_order", target="fn_validate"))
    store.add_edge(Edge(id="e7", type="CONTAIN", source="mod_order", target="cls_order"))
    store.add_edge(Edge(id="e8", type="TEST", source="test_create", target="fn_create"))
    store.add_edge(Edge(id="e9", type="TEST", source="test_validate", target="fn_validate"))
    store.add_edge(Edge(id="e10", type="DESCRIBE", source="doc_1", target="cls_order", properties={"method": "explicit"}))
    store.add_edge(Edge(id="e11", type="DEPEND", source="mod_order", target="mod_payment"))

    # Indexes
    sym_idx.add("create_order", "fn_create")
    sym_idx.add("validate_order", "fn_validate")
    sym_idx.add("process_payment", "fn_process")
    sym_idx.add("notify_user", "fn_notify")
    sym_idx.add("api_handler", "fn_api")
    sym_idx.add("OrderService", "cls_order")

    file_idx.add("src/order.py", {"file_id": "file_order", "language": "python", "nodes": ["fn_create", "fn_validate", "cls_order"]})
    file_idx.add("src/payment.py", {"file_id": "file_payment", "language": "python", "nodes": ["fn_process"]})

    query_engine = QueryEngine(store, kw_idx, sym_idx, file_idx)
    return ReasoningEngine(store, query_engine)


def test_test_recommendation(engine):
    result = engine.test_recommendation(["src/order.py"])
    assert len(result.modified_files) == 1
    assert len(result.direct_tests) >= 1  # test_create_order, test_validate
    # Check all recommended tests have reasons
    for test in result.recommended_tests:
        assert "reason" in test


def test_test_recommendation_indirect(engine):
    result = engine.test_recommendation(["src/order.py"])
    # Modifying order.py should recommend tests for callers too
    all_test_names = [t["name"] for t in result.recommended_tests]
    assert "test_create_order" in all_test_names


def test_test_recommendation_empty(engine):
    result = engine.test_recommendation(["nonexistent.py"])
    assert len(result.recommended_tests) == 0