"""Tests for custom exceptions."""

import pytest

from ai_qa.exceptions import (
    AIQAError,
    CacheError,
    CheckNotFoundError,
    ContextValidationError,
    DatasetError,
    ProviderAPIError,
    ProviderNotConfiguredError,
)


def test_aiqaerror_is_exception() -> None:
    """Test that AIQAError is an Exception."""
    assert issubclass(AIQAError, Exception)


def test_provider_not_configured_error() -> None:
    """Test ProviderNotConfiguredError can be raised and caught."""
    with pytest.raises(ProviderNotConfiguredError):
        raise ProviderNotConfiguredError(
            "Anthropic provider requires the anthropic package.\n"
            "Install it with: pip install ai-qa-sdk[anthropic]"
        )


def test_provider_api_error() -> None:
    """Test ProviderAPIError can be raised and caught."""
    with pytest.raises(ProviderAPIError):
        raise ProviderAPIError(
            "Anthropic API returned 429 (rate limit). "
            "Try again later or increase max_retries."
        )


def test_check_not_found_error() -> None:
    """Test CheckNotFoundError can be raised and caught."""
    with pytest.raises(CheckNotFoundError):
        raise CheckNotFoundError(
            "Check 'unknown_check' not found. "
            "Available checks: hallucination, factuality, relevance, toxicity"
        )


def test_context_validation_error() -> None:
    """Test ContextValidationError can be raised and caught."""
    with pytest.raises(ContextValidationError):
        raise ContextValidationError(
            "HallucinationCheck requires either 'context' or 'ground_truth' "
            "in Context. Example: Context(output='...', context=['...'])"
        )


def test_dataset_error() -> None:
    """Test DatasetError can be raised and caught."""
    with pytest.raises(DatasetError):
        raise DatasetError("Failed to load dataset from path")


def test_cache_error() -> None:
    """Test CacheError can be raised and caught."""
    with pytest.raises(CacheError):
        raise CacheError("Failed to write to cache")


def test_exception_inheritance() -> None:
    """Test that all custom exceptions inherit from AIQAError."""
    assert issubclass(ProviderNotConfiguredError, AIQAError)
    assert issubclass(ProviderAPIError, AIQAError)
    assert issubclass(CheckNotFoundError, AIQAError)
    assert issubclass(ContextValidationError, AIQAError)
    assert issubclass(DatasetError, AIQAError)
    assert issubclass(CacheError, AIQAError)


def test_exception_messages_included() -> None:
    """Test that exception messages can include actionable instructions."""
    msg = (
        "Anthropic provider requires the anthropic package.\n"
        "Install it with: pip install ai-qa-sdk[anthropic]\n"
        "Or: pip install ai-qa-sdk[all]\n"
        "After installing, configure: from ai_qa import configure\n"
        "configure(provider='anthropic', api_key='sk-...')"
    )
    exc = ProviderNotConfiguredError(msg)
    assert "anthropic" in str(exc)
    assert "install" in str(exc).lower()
