import pytest
import json
import rusty_jq


@pytest.fixture
def data():
    return {"name": "John", "age": 30, "city": "Paris"}


@pytest.fixture
def json_str(data):
    return json.dumps(data)


# --- Basic comma ---

@pytest.mark.parametrize("query,expected", [
    # two fields
    (".name, .age", ["John", 30]),
    # three fields
    (".name, .age, .city", ["John", 30, "Paris"]),
])
def test_basic_comma(json_str, query, expected):
    assert list(rusty_jq.compile(query).input(json_str)) == expected


# --- Comma with pipe ---

def test_comma_after_pipe():
    """`.users[] | .name, .id` produces interleaved results."""
    data = json.dumps({
        "users": [
            {"id": 1, "name": "Alice"},
            {"id": 2, "name": "Bob"},
        ]
    })
    result = list(rusty_jq.compile(".users | .[] | .name, .id").input(data))
    assert result == ["Alice", 1, "Bob", 2]


def test_comma_pipe_precedence():
    """.name, .age | . means (.name, .age) | . — pipe binds looser."""
    data = json.dumps({"name": "John", "age": 30})
    result = list(rusty_jq.compile(".name, .age").input(data))
    assert result == ["John", 30]


# --- Comma does not break existing features ---

def test_object_construction_still_works():
    """Commas inside {k: v, k: v} are object separators, not comma operator."""
    data = json.dumps({"x": 1, "y": 2})
    result = rusty_jq.compile("{a: .x, b: .y}").first(data)
    assert result == {"a": 1, "b": 2}


def test_select_still_works():
    """select() with and/or still works — no interference from comma."""
    data = json.dumps([{"id": 1, "ok": True}, {"id": 2, "ok": False}])
    result = list(rusty_jq.compile(".[] | select(.ok) | .id").input(data))
    assert result == [1]


def test_comma_with_select():
    """.[] | .name, .score with select in the pipeline."""
    data = json.dumps([
        {"name": "Alice", "score": 90},
        {"name": "Bob", "score": 60},
    ])
    result = list(rusty_jq.compile(".[] | select(.score > 70) | .name, .score").input(data))
    assert result == ["Alice", 90]


def test_comma_with_deep_paths():
    """.profile.name, .profile.age — each branch is a filter chain."""
    data = json.dumps({"profile": {"name": "Eve", "age": 25}})
    result = list(rusty_jq.compile(".profile.name, .profile.age").input(data))
    assert result == ["Eve", 25]
