import pytest
import json
import rusty_jq


@pytest.mark.parametrize("query,data,expected", [
    # Bug 1: != with mismatched types should return true
    (
        '.users | .[] | select(.name != null) | .name',
        {"users": [{"name": "John"}, {"name": None}]},
        ["John"],
    ),
    (
        '.users | .[] | select(.age != null) | .age',
        {"users": [{"age": 30}, {"age": None}]},
        [30],
    ),
    # != between int and string (mismatched) should pass
    (
        '.items | .[] | select(.val != "hello") | .val',
        {"items": [{"val": 42}, {"val": "hello"}]},
        [42],
    ),

    # Bug 3: escaped quotes in string literals
    (
        '.items | .[] | select(.name == "O\\"Brien") | .name',
        {"items": [{"name": 'O"Brien'}, {"name": "Smith"}]},
        ['O"Brien'],
    ),
    # escaped backslash
    (
        '.items | .[] | select(.path == "C:\\\\Users") | .path',
        {"items": [{"path": "C:\\Users"}, {"path": "/home"}]},
        ["C:\\Users"],
    ),
    # escaped newline
    (
        '.items | .[] | select(.text == "line1\\nline2") | .text',
        {"items": [{"text": "line1\nline2"}, {"text": "other"}]},
        ["line1\nline2"],
    ),

    # Bug 4: field names starting with digits should be rejected (tested separately below)

    # Unicode escape \uXXXX
    (
        '.items | .[] | select(.name == "\\u0041lice") | .name',
        {"items": [{"name": "Alice"}, {"name": "Bob"}]},
        ["Alice"],
    ),
    # Unicode escape for non-ASCII
    (
        '.items | .[] | select(.emoji == "\\u00e9") | .emoji',
        {"items": [{"emoji": "\u00e9"}, {"emoji": "x"}]},
        ["\u00e9"],
    ),
])
def test_bugfixes(query, data, expected):
    program = rusty_jq.compile(query)
    result = list(program.input(json.dumps(data)))
    assert result == expected


def test_reject_leading_digit_field():
    """Bug 4: field names starting with digits should be rejected."""
    with pytest.raises(ValueError):
        rusty_jq.compile(".123abc")


def test_underscore_field_still_works():
    """Sanity: underscore-leading field names must still work."""
    p = rusty_jq.compile("._private")
    assert p.first(json.dumps({"_private": "ok"})) == "ok"


def test_hyphenated_field_still_works():
    """Sanity: hyphenated field names must still work."""
    p = rusty_jq.compile(".some-field")
    assert p.first(json.dumps({"some-field": "ok"})) == "ok"
