import pytest

from veritell_langchain.schema import (
    SCHEMA_VERSION,
    SchemaError,
    VeritellEvaluationResult,
)


def test_X10_positive_minimal_valid_payload_parses() -> None:
    payload = {
        "schema_version": SCHEMA_VERSION,
        "hallucination": 2,
        "bias": 1,
        "safety": 5,
        "risk_label": "low",
    }

    result = VeritellEvaluationResult.from_api(payload)
    assert result.schema_version == SCHEMA_VERSION
    assert result.hallucination == 2
    assert result.bias == 1
    assert result.safety == 5
    assert result.risk_label == "low"
    assert result.judge_comments is None
    assert result.extra == {}


def test_X10_positive_optional_fields_and_unknown_fields_roundtrip_to_extra() -> None:
    payload = {
        "schema_version": SCHEMA_VERSION,
        "hallucination": 3,
        "bias": 2,
        "safety": 4,
        "risk_label": "medium",
        "judge_comments": [{"judge_id": "j1", "comment": "ok"}],
        "new_server_field": {"foo": "bar"},
    }

    result = VeritellEvaluationResult.from_api(payload)
    assert result.judge_comments == [{"judge_id": "j1", "comment": "ok"}]
    assert result.extra.get("new_server_field") == {"foo": "bar"}


def test_X10_negative_missing_required_field_raises() -> None:
    payload = {
        "schema_version": SCHEMA_VERSION,
        "hallucination": 2,
        "bias": 1,
        # safety missing
        "risk_label": "low",
    }

    with pytest.raises(SchemaError) as exc:
        VeritellEvaluationResult.from_api(payload)

    assert "safety" in str(exc.value)


def test_X10_negative_invalid_score_type_raises() -> None:
    payload = {
        "schema_version": SCHEMA_VERSION,
        "hallucination": "2",
        "bias": 1,
        "safety": 5,
        "risk_label": "low",
    }

    with pytest.raises(SchemaError) as exc:
        VeritellEvaluationResult.from_api(payload)

    assert "hallucination" in str(exc.value)


def test_X10_negative_score_out_of_range_raises() -> None:
    payload = {
        "schema_version": SCHEMA_VERSION,
        "hallucination": 0,
        "bias": 1,
        "safety": 5,
        "risk_label": "low",
    }

    with pytest.raises(SchemaError) as exc:
        VeritellEvaluationResult.from_api(payload)

    assert "range" in str(exc.value).lower()


def test_X10_negative_invalid_risk_label_raises() -> None:
    payload = {
        "schema_version": SCHEMA_VERSION,
        "hallucination": 2,
        "bias": 1,
        "safety": 5,
        "risk_label": "unknown_label",
    }

    with pytest.raises(SchemaError) as exc:
        VeritellEvaluationResult.from_api(payload)

    assert "risk_label" in str(exc.value)


def test_X10_negative_unsupported_schema_version_raises() -> None:
    payload = {
        "schema_version": 999,
        "hallucination": 2,
        "bias": 1,
        "safety": 5,
        "risk_label": "low",
    }

    with pytest.raises(SchemaError) as exc:
        VeritellEvaluationResult.from_api(payload)

    assert "schema_version" in str(exc.value)
