"""Test API key environment variable handling."""

import os
from unittest.mock import patch

import pytest


def test_api_key_from_veritell_api_key():
    """Test that API key is read from VERITELL_API_KEY environment variable."""
    from veritell_langchain.config import get_api_key

    with patch.dict(os.environ, {"VERITELL_API_KEY": "test-key-123"}):
        api_key = get_api_key()
        assert api_key == "test-key-123"


def test_api_key_from_veritell_key():
    """Test that API key is read from VERITELL_KEY as fallback."""
    from veritell_langchain.config import get_api_key

    with patch.dict(os.environ, {"VERITELL_KEY": "test-key-456"}, clear=True):
        api_key = get_api_key()
        assert api_key == "test-key-456"


def test_api_key_priority():
    """Test that VERITELL_API_KEY takes priority over VERITELL_KEY."""
    from veritell_langchain.config import get_api_key

    with patch.dict(
        os.environ, {"VERITELL_API_KEY": "primary-key", "VERITELL_KEY": "fallback-key"}
    ):
        api_key = get_api_key()
        assert api_key == "primary-key"


def test_api_key_missing():
    """Test that None is returned when no API key is set."""
    from veritell_langchain.config import get_api_key

    with patch.dict(os.environ, {}, clear=True):
        api_key = get_api_key()
        assert api_key is None


def test_evaluator_requires_api_key():
    """Test that VeritellEvaluator raises error when API key is missing."""
    from veritell_langchain import VeritellAuthError, VeritellEvaluator

    with patch.dict(os.environ, {}, clear=True):
        with pytest.raises(VeritellAuthError) as exc_info:
            VeritellEvaluator()
        assert "API key not found" in str(exc_info.value)


def test_evaluator_with_explicit_api_key():
    """Test that VeritellEvaluator accepts explicit API key."""
    from veritell_langchain import VeritellEvaluator

    with patch.dict(os.environ, {}, clear=True):
        evaluator = VeritellEvaluator(api_key="explicit-key")
        assert evaluator.api_key == "explicit-key"


def test_evaluator_uses_env_api_key():
    """Test that VeritellEvaluator uses API key from environment."""
    from veritell_langchain import VeritellEvaluator

    with patch.dict(os.environ, {"VERITELL_API_KEY": "env-key"}):
        evaluator = VeritellEvaluator()
        assert evaluator.api_key == "env-key"


def test_api_base_url_default():
    """Test default API base URL."""
    from veritell_langchain.config import get_api_base_url

    with patch.dict(os.environ, {}, clear=True):
        url = get_api_base_url()
        assert url == "https://api.veritell.ai/v1"


def test_api_base_url_custom():
    """Test custom API base URL from environment."""
    from veritell_langchain.config import get_api_base_url

    with patch.dict(os.environ, {"VERITELL_API_BASE_URL": "https://custom.api.com"}):
        url = get_api_base_url()
        assert url == "https://custom.api.com"


def test_timeout_default():
    """Test default timeout value."""
    from veritell_langchain.config import get_timeout

    with patch.dict(os.environ, {}, clear=True):
        timeout = get_timeout()
        assert timeout == 30


def test_timeout_custom():
    """Test custom timeout from environment."""
    from veritell_langchain.config import get_timeout

    with patch.dict(os.environ, {"VERITELL_TIMEOUT": "60"}):
        timeout = get_timeout()
        assert timeout == 60


def test_timeout_invalid():
    """Test that invalid timeout falls back to default."""
    from veritell_langchain.config import get_timeout

    with patch.dict(os.environ, {"VERITELL_TIMEOUT": "invalid"}):
        timeout = get_timeout()
        assert timeout == 30
