import pytest
from unittest.mock import MagicMock, patch
from src import ai_client

@pytest.fixture(autouse=True)
def reset_ai_client():
    ai_client.reset_session()
    yield
    ai_client.reset_session()

def test_discussion_compression_gemini():
    ai_client.set_provider("gemini", "gemini-2.5-flash-lite")
    
    mock_gemini = MagicMock()
    mock_response = MagicMock()
    mock_response.text = "This is a compressed summary."
    mock_gemini.models.generate_content.return_value = mock_response
    
    with patch("src.ai_client._gemini_client", mock_gemini), \
         patch("src.ai_client._ensure_gemini_client"):
        result = ai_client.run_discussion_compression("User: Hello\nAI: Hi there!")
        
        assert result == "This is a compressed summary."
        mock_gemini.models.generate_content.assert_called_once()
        args, kwargs = mock_gemini.models.generate_content.call_args
        assert "[HISTORY]" in kwargs["contents"]
        assert "User: Hello" in kwargs["contents"]

def test_discussion_compression_anthropic():
    ai_client.set_provider("anthropic", "claude-3-haiku")
    
    mock_anthropic = MagicMock()
    mock_response = MagicMock()
    mock_block = MagicMock()
    mock_block.text = "Anthropic summary."
    mock_response.content = [mock_block]
    mock_anthropic.messages.create.return_value = mock_response
    
    with patch("src.ai_client._anthropic_client", mock_anthropic), \
         patch("src.ai_client._ensure_anthropic_client"):
        result = ai_client.run_discussion_compression("Some history")
        
        assert result == "Anthropic summary."
        mock_anthropic.messages.create.assert_called_once()
        kwargs = mock_anthropic.messages.create.call_args[1]
        assert "Some history" in kwargs["messages"][0]["content"]

def test_discussion_compression_deepseek():
    ai_client.set_provider("deepseek", "deepseek-chat")
    
    mock_response = MagicMock()
    mock_response.json.return_value = {"choices": [{"message": {"content": "DeepSeek summary."}}]}
    
    # Phase 3 (startup_speedup_20260606) removed the top-level 'import requests'
    # from src/ai_client.py. The module is now resolved via _require_warmed()
    # at call time. We mock _require_warmed to return a fake requests module
    # that has the .post() method.
    mock_requests_mod = MagicMock()
    mock_requests_mod.post.return_value = mock_response
    
    with patch("src.ai_client._require_warmed", return_value=mock_requests_mod), \
         patch("src.ai_client._load_credentials", return_value={"deepseek": {"api_key": "test"}}):
        result = ai_client.run_discussion_compression("DeepSeek history")
        
        assert result == "DeepSeek summary."

def test_discussion_compression_gemini_cli():
    ai_client.set_provider("gemini_cli", "gemini-1.5-flash")
    
    mock_adapter = MagicMock()
    mock_adapter.send.return_value = {"text": "CLI summary."}
    
    with patch("src.ai_client.GeminiCliAdapter", return_value=mock_adapter):
        result = ai_client.run_discussion_compression("CLI history")
        
        assert result == "CLI summary."
