"""Unit tests for AzureChatService."""

import pytest
from unittest.mock import Mock, patch, MagicMock

from src.extraction_v2.azure_chat_service import AzureChatService, get_azure_chat_service


class TestAzureChatService:
    """Tests for AzureChatService class."""

    def test_service_can_be_imported(self):
        """Test that service can be imported."""
        assert AzureChatService is not None

    def test_factory_function_returns_cached_instance(self):
        """Test that factory function returns cached instance."""
        # Clear cache
        get_azure_chat_service.cache_clear()

        service1 = get_azure_chat_service()
        service2 = get_azure_chat_service()

        assert service1 is service2  # Same instance (cached)

    @patch('src.extraction_v2.azure_chat_service.settings')
    def test_service_initializes_with_settings(self, mock_settings):
        """Test that service initializes with correct settings."""
        # Mock settings
        mock_settings.azure_openai_api_key = "test-key"
        mock_settings.azure_openai_endpoint = "https://test.openai.azure.com"
        mock_settings.azure_openai_api_version = "2024-02-15-preview"
        mock_settings.azure_openai_deployment = "gpt-4o"
        mock_settings.azure_openai_temperature = 0.0
        mock_settings.azure_openai_max_tokens = 4096

        with patch('src.extraction_v2.azure_chat_service.AzureChatOpenAI'):
            service = AzureChatService()

            assert service.api_key == "test-key"
            assert service.endpoint == "https://test.openai.azure.com"
            assert service.deployment == "gpt-4o"
            assert service.temperature == 0.0
            assert service.max_tokens == 4096

    def test_invoke_and_ainvoke_methods_exist(self):
        """Test that invoke and ainvoke methods exist."""
        with patch('src.extraction_v2.azure_chat_service.settings'), \
             patch('src.extraction_v2.azure_chat_service.AzureChatOpenAI'):
            service = AzureChatService()

            assert hasattr(service, 'invoke')
            assert hasattr(service, 'ainvoke')
            assert callable(service.invoke)
            assert callable(service.ainvoke)
