"""Tests for RelevanceCheck."""

from __future__ import annotations

from unittest.mock import MagicMock, patch

import pytest

from ai_qa.checks.relevance import RelevanceCheck
from ai_qa.core.context import Context
from ai_qa.exceptions import ContextValidationError


def test_relevance_check_name():
    """Test RelevanceCheck has correct name."""
    check = RelevanceCheck()
    assert check.name == "relevance"


def test_relevance_check_requires_input():
    """Test RelevanceCheck requires input to be set."""
    check = RelevanceCheck()
    ctx = Context(output="Some output")  # No input
    with pytest.raises(ContextValidationError):
        check.evaluate(ctx)


def test_relevance_check_requires_output():
    """Test RelevanceCheck requires output to be set."""
    check = RelevanceCheck()
    # Context requires output field, so we test validate_context directly
    from unittest.mock import MagicMock
    ctx = MagicMock()
    ctx.input = "What is X?"
    ctx.output = None
    with pytest.raises(ContextValidationError):
        check.validate_context(ctx)


@patch("ai_qa.checks.relevance.get_provider")
def test_relevance_check_highly_relevant(mock_get_provider):
    """Test RelevanceCheck with highly relevant output."""
    mock_provider = MagicMock()
    mock_provider.judge.return_value = {
        "score": 1.0,
        "evidence": "Output directly addresses the query",
    }
    mock_get_provider.return_value = mock_provider

    check = RelevanceCheck()
    ctx = Context(
        input="What is the capital of France?",
        output="Paris is the capital of France",
    )
    result = check.evaluate(ctx)

    assert result.passed is True
    assert result.score == 1.0


@patch("ai_qa.checks.relevance.get_provider")
def test_relevance_check_irrelevant_output(mock_get_provider):
    """Test RelevanceCheck with irrelevant output."""
    mock_provider = MagicMock()
    mock_provider.judge.return_value = {
        "score": 0.1,
        "evidence": "Output does not address the query",
    }
    mock_get_provider.return_value = mock_provider

    check = RelevanceCheck()
    ctx = Context(
        input="What is the capital of France?",
        output="The Eiffel Tower is tall and made of iron",
    )
    result = check.evaluate(ctx)

    assert result.passed is False
    assert result.score == 0.1


@patch("ai_qa.checks.relevance.get_provider")
def test_relevance_check_partially_relevant(mock_get_provider):
    """Test RelevanceCheck with partially relevant output."""
    mock_provider = MagicMock()
    mock_provider.judge.return_value = {
        "score": 0.6,
        "evidence": "Partially addresses the query with some off-topic content",
    }
    mock_get_provider.return_value = mock_provider

    check = RelevanceCheck(threshold=0.7)
    ctx = Context(
        input="What is machine learning?",
        output="Machine learning is a subset of AI. The weather is nice today.",
    )
    result = check.evaluate(ctx)

    assert result.passed is False
    assert result.score == 0.6
