import pytest

from blends.models import SyntaxGraph


@pytest.mark.blends_test_group("syntax_unittesting")
def test_update_node_attribute_valid_key_accepted() -> None:
    graph = SyntaxGraph()
    graph.add_node("1", label_type="IfStatement")
    graph.update_node_attribute("1", "condition_id", "2")
    assert graph.nodes["1"]["condition_id"] == "2"


@pytest.mark.blends_test_group("syntax_unittesting")
def test_update_node_attribute_invalid_key_raises() -> None:
    graph = SyntaxGraph()
    graph.add_node("1", label_type="IfStatement")
    with pytest.raises(ValueError, match="Invalid node attribute key"):
        graph.update_node_attribute("1", "unknown_key", "bad")


@pytest.mark.blends_test_group("syntax_unittesting")
def test_add_node_invalid_key_raises() -> None:
    graph = SyntaxGraph()
    with pytest.raises(ValueError, match="Invalid node attribute keys"):
        graph.add_node("1", label_type="Return", unknown_key="bad")


@pytest.mark.blends_test_group("syntax_unittesting")
def test_update_node_invalid_key_raises() -> None:
    graph = SyntaxGraph()
    graph.add_node("1", label_type="Assignment")
    with pytest.raises(ValueError, match="Invalid node attribute keys"):
        graph.update_node("1", {"unknown_key": "bad"})


@pytest.mark.blends_test_group("syntax_unittesting")
def test_update_node_with_valid_keys_succeeds() -> None:
    graph = SyntaxGraph()
    graph.add_node("1", label_type="Return")
    graph.update_node("1", {"value_id": "2", "label_type": "Literal"})
    assert graph.nodes["1"]["value_id"] == "2"
    assert graph.nodes["1"]["label_type"] == "Literal"


@pytest.mark.blends_test_group("syntax_unittesting")
def test_add_node_populates_nodes_by_type_index() -> None:
    graph = SyntaxGraph()
    graph.add_node("1", label_type="IfStatement")
    assert "1" in graph.nodes_by_type["IfStatement"]


@pytest.mark.blends_test_group("syntax_unittesting")
def test_update_node_with_multiple_invalid_keys_raises() -> None:
    graph = SyntaxGraph()
    graph.add_node("1")
    with pytest.raises(ValueError, match="Invalid node attribute keys"):
        graph.update_node("1", {"bad_key_1": "x", "bad_key_2": "y"})
