"""Tests for ToxicityCheck."""

from __future__ import annotations

from unittest.mock import MagicMock, patch

from ai_qa.checks.toxicity import ToxicityCheck
from ai_qa.core.context import Context


def test_toxicity_check_name():
    """Test ToxicityCheck has correct name."""
    check = ToxicityCheck()
    assert check.name == "toxicity"


def test_toxicity_check_requires_llm():
    """Test ToxicityCheck requires LLM."""
    check = ToxicityCheck()
    assert check.requires_llm is True


@patch("ai_qa.checks.toxicity.get_provider")
def test_toxicity_check_non_toxic(mock_get_provider):
    """Test ToxicityCheck with non-toxic output."""
    mock_provider = MagicMock()
    mock_provider.judge.return_value = {
        "score": 1.0,
        "evidence": "No toxic content detected",
    }
    mock_get_provider.return_value = mock_provider

    check = ToxicityCheck()
    ctx = Context(output="Hello, how are you today?")
    result = check.evaluate(ctx)

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


@patch("ai_qa.checks.toxicity.get_provider")
def test_toxicity_check_toxic(mock_get_provider):
    """Test ToxicityCheck with toxic output."""
    mock_provider = MagicMock()
    mock_provider.judge.return_value = {
        "score": 0.0,
        "evidence": "Contains offensive language",
    }
    mock_get_provider.return_value = mock_provider

    check = ToxicityCheck()
    ctx = Context(output="This is offensive content")
    result = check.evaluate(ctx)

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


@patch("ai_qa.checks.toxicity.get_provider")
def test_toxicity_check_strict_threshold(mock_get_provider):
    """Test ToxicityCheck with strict threshold."""
    mock_provider = MagicMock()
    mock_provider.judge.return_value = {
        "score": 0.85,
        "evidence": "Minor issue found",
    }
    mock_get_provider.return_value = mock_provider

    check = ToxicityCheck(threshold=0.95)  # Very strict
    ctx = Context(output="Test output")
    result = check.evaluate(ctx)

    assert result.passed is False


@patch("ai_qa.checks.toxicity.get_provider")
def test_toxicity_check_default_threshold(mock_get_provider):
    """Test ToxicityCheck with default threshold."""
    mock_provider = MagicMock()
    mock_provider.judge.return_value = {"score": 0.9, "evidence": "OK"}
    mock_get_provider.return_value = mock_provider

    check = ToxicityCheck()
    ctx = Context(output="Safe content")
    result = check.evaluate(ctx)

    # Default threshold is 0.5, so 0.9 > 0.5 passes
    assert result.passed is True
