"""Tests for JsonFormatCheck."""

from __future__ import annotations


from ai_qa.checks.json_format import JsonFormatCheck
from ai_qa.core.context import Context


def test_json_format_check_valid_json_string():
    """Test JsonFormatCheck with valid JSON string."""
    check = JsonFormatCheck()
    ctx = Context(output='{"key": "value"}')
    result = check.evaluate(ctx)
    assert result.passed is True
    assert result.score == 1.0


def test_json_format_check_valid_dict():
    """Test JsonFormatCheck with dict output."""
    check = JsonFormatCheck()
    ctx = Context(output={"key": "value"})
    result = check.evaluate(ctx)
    assert result.passed is True
    assert result.score == 1.0


def test_json_format_check_valid_list():
    """Test JsonFormatCheck with list output."""
    check = JsonFormatCheck()
    ctx = Context(output=[1, 2, 3])
    result = check.evaluate(ctx)
    assert result.passed is True
    assert result.score == 1.0


def test_json_format_check_invalid_json_string():
    """Test JsonFormatCheck with invalid JSON string."""
    check = JsonFormatCheck()
    ctx = Context(output='{"invalid": json}')
    result = check.evaluate(ctx)
    assert result.passed is False
    assert result.score == 0.0
    assert "not valid JSON" in result.evidence


def test_json_format_check_empty_json_object():
    """Test JsonFormatCheck with empty JSON object."""
    check = JsonFormatCheck()
    ctx = Context(output='{}')
    result = check.evaluate(ctx)
    assert result.passed is True


def test_json_format_check_empty_json_array():
    """Test JsonFormatCheck with empty JSON array."""
    check = JsonFormatCheck()
    ctx = Context(output='[]')
    result = check.evaluate(ctx)
    assert result.passed is True


def test_json_format_check_complex_json():
    """Test JsonFormatCheck with complex nested JSON."""
    json_str = '''
    {
        "users": [
            {"id": 1, "name": "Alice"},
            {"id": 2, "name": "Bob"}
        ],
        "count": 2
    }
    '''
    check = JsonFormatCheck()
    ctx = Context(output=json_str)
    result = check.evaluate(ctx)
    assert result.passed is True


def test_json_format_check_plain_string():
    """Test JsonFormatCheck with plain string (not JSON)."""
    check = JsonFormatCheck()
    ctx = Context(output="hello world")
    result = check.evaluate(ctx)
    assert result.passed is False


def test_json_format_check_number_string():
    """Test JsonFormatCheck with plain number string."""
    check = JsonFormatCheck()
    ctx = Context(output="42")
    result = check.evaluate(ctx)
    assert result.passed is True  # Valid JSON


def test_json_format_check_boolean_string():
    """Test JsonFormatCheck with boolean string."""
    check = JsonFormatCheck()
    ctx = Context(output="true")
    result = check.evaluate(ctx)
    assert result.passed is True  # Valid JSON


def test_json_format_check_null_string():
    """Test JsonFormatCheck with null string."""
    check = JsonFormatCheck()
    ctx = Context(output="null")
    result = check.evaluate(ctx)
    assert result.passed is True  # Valid JSON




def test_json_format_check_whitespace():
    """Test JsonFormatCheck handles whitespace correctly."""
    check = JsonFormatCheck()
    ctx = Context(output='  {"key"  :  "value"}  ')
    result = check.evaluate(ctx)
    assert result.passed is True


def test_json_format_check_custom_threshold():
    """Test JsonFormatCheck with custom threshold."""
    check = JsonFormatCheck(threshold=0.8)
    assert check.threshold == 0.8


def test_json_format_check_version():
    """Test JsonFormatCheck has correct version."""
    check = JsonFormatCheck()
    assert check.version == "1.0.0"


def test_json_format_check_name():
    """Test JsonFormatCheck has correct name."""
    check = JsonFormatCheck()
    assert check.name == "json_format"


def test_json_format_check_requires_llm():
    """Test JsonFormatCheck does not require LLM."""
    check = JsonFormatCheck()
    assert check.requires_llm is False
