"""Tests for CustomCheck."""

from unittest.mock import MagicMock, patch

import pytest

from ai_qa.checks.custom import CustomCheck
from ai_qa.core.context import Context
from ai_qa.exceptions import ContextValidationError


def test_custom_check_name():
    """Test CustomCheck name from custom_name parameter."""
    check = CustomCheck(
        criteria="Must be professional",
        custom_name="tone_check",
    )
    assert check.name == "tone_check"


def test_custom_check_requires_llm():
    """Test CustomCheck requires_llm property."""
    check = CustomCheck(criteria="Must be clear")
    assert check.requires_llm is True


@patch("ai_qa.checks.custom.get_provider")
def test_custom_check_passes(mock_get_provider):
    """Test custom criteria passing."""
    check = CustomCheck(
        criteria="Output should be professional",
        threshold=0.7,
    )
    ctx = Context(
        input="Write a professional email",
        output="Dear Sir/Madam, Thank you for reaching out.",
    )

    mock_provider = MagicMock()
    mock_provider.judge.return_value = {
        "score": 0.9,
        "evidence": "Very professional tone",
    }
    mock_get_provider.return_value = mock_provider

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


@patch("ai_qa.checks.custom.get_provider")
def test_custom_check_fails(mock_get_provider):
    """Test custom criteria failing."""
    check = CustomCheck(
        criteria="Must use formal language",
        threshold=0.7,
    )
    ctx = Context(input="Write formal text",
        output="Yo, what's up?",
    )

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

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


@patch("ai_qa.checks.custom.get_provider")
def test_custom_check_default_name(mock_get_provider):
    """Test default 'custom' name."""
    check = CustomCheck(criteria="Some criteria")
    assert check.name == "custom"


def test_custom_check_no_output():
    """Test error when output missing."""
    from unittest.mock import MagicMock

    check = CustomCheck(criteria="Some criteria")
    ctx = MagicMock()
    ctx.output = None
    ctx.input = "test"
    with pytest.raises(ContextValidationError):
        check.evaluate(ctx)


@patch("ai_qa.checks.custom.get_provider")
def test_custom_check_with_dict_output(mock_get_provider):
    """Test custom check with dict output."""
    check = CustomCheck(criteria="Must have data field")
    ctx = Context(input="test",
        output={"data": "value"},
    )

    mock_provider = MagicMock()
    mock_provider.judge.return_value = {
        "score": 1.0,
        "evidence": "Has data field",
    }
    mock_get_provider.return_value = mock_provider

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


@patch("ai_qa.checks.custom.get_provider")
def test_custom_check_without_input(mock_get_provider):
    """Test custom check when input is optional."""
    check = CustomCheck(criteria="No specific input needed")
    ctx = Context(output="Some output")

    mock_provider = MagicMock()
    mock_provider.judge.return_value = {
        "score": 0.8,
        "evidence": "Evaluation done",
    }
    mock_get_provider.return_value = mock_provider

    result = check.evaluate(ctx)
    assert result.score == 0.8
