"""Tests for ContradictionCheck."""

from unittest.mock import MagicMock, patch

import pytest

from ai_qa.checks.contradiction import ContradictionCheck
from ai_qa.core.context import Context
from ai_qa.exceptions import ContextValidationError


def test_contradiction_check_name():
    """Test ContradictionCheck has correct name."""
    check = ContradictionCheck()
    assert check.name == "contradiction"


def test_contradiction_check_requires_llm():
    """Test ContradictionCheck requires_llm property."""
    check = ContradictionCheck()
    assert check.requires_llm is True


@patch("ai_qa.checks.contradiction.get_provider")
def test_contradiction_consistent(mock_get_provider):
    """Test consistent (no contradictions)."""
    check = ContradictionCheck()
    ctx = Context(output="test", 
        conversation=[
            {"role": "user", "content": "What is Python?"},
            {"role": "assistant", "content": "Python is a programming language"},
            {"role": "user", "content": "Is it used for web?"},
            {"role": "assistant", "content": "Yes, Python is used for web development"},
        ]
    )

    mock_provider = MagicMock()
    mock_provider.judge.return_value = {
        "score": 1.0,
        "evidence": "No contradictions",
    }
    mock_get_provider.return_value = mock_provider

    result = check.evaluate(ctx)
    assert result.score == 1.0
    assert result.passed


@patch("ai_qa.checks.contradiction.get_provider")
def test_contradiction_contradictory(mock_get_provider):
    """Test contradictory statements."""
    check = ContradictionCheck(threshold=0.8)
    ctx = Context(output="test", 
        conversation=[
            {"role": "user", "content": "Is Python fast?"},
            {"role": "assistant", "content": "Yes, Python is very fast"},
            {"role": "user", "content": "But isn't it slow?"},
            {
                "role": "assistant",
                "content": "Yes, Python is slow",
            },
        ]
    )

    mock_provider = MagicMock()
    mock_provider.judge.return_value = {
        "score": 0.1,
        "evidence": "Contradictions found",
    }
    mock_get_provider.return_value = mock_provider

    result = check.evaluate(ctx)
    assert result.score == 0.1
    assert not result.passed


def test_contradiction_no_conversation():
    """Test error when conversation missing."""
    check = ContradictionCheck()
    ctx = Context(output="test")
    with pytest.raises(ContextValidationError):
        check.evaluate(ctx)


def test_contradiction_single_turn():
    """Test error with single turn."""
    check = ContradictionCheck()
    ctx = Context(output="test", 
        conversation=[
            {"role": "user", "content": "Hello"},
        ]
    )
    with pytest.raises(ContextValidationError):
        check.evaluate(ctx)
