"""
Unit tests for news sources configuration after Google News removal.

Verifies that:
- Google News functions/imports no longer exist
- Finnhub is properly configured as the primary news source
- News analyst uses correct tools for stocks and crypto
"""

import pytest
from unittest.mock import MagicMock, patch


class TestGoogleNewsRemoval:
    """Tests verifying Google News has been removed from the codebase"""

    def test_no_google_news_in_interface(self):
        """Verify get_google_news is not in interface module"""
        from tradingagents.dataflows import interface

        assert not hasattr(interface, 'get_google_news'), \
            "get_google_news should not exist in interface module"

    def test_no_google_news_in_dataflows_init(self):
        """Verify get_google_news is not exported from dataflows"""
        from tradingagents import dataflows

        assert not hasattr(dataflows, 'get_google_news'), \
            "get_google_news should not be exported from dataflows"

    def test_no_googlenews_utils_import(self):
        """Verify googlenews_utils module does not exist"""
        with pytest.raises(ImportError):
            from tradingagents.dataflows import googlenews_utils

    def test_no_getNewsData_in_dataflows(self):
        """Verify getNewsData is not exported from dataflows"""
        from tradingagents import dataflows

        assert not hasattr(dataflows, 'getNewsData'), \
            "getNewsData should not be exported from dataflows"


class TestFinnhubNewsConfiguration:
    """Tests verifying Finnhub is properly configured as news source"""

    def test_finnhub_news_online_exists(self):
        """Verify get_finnhub_news_online exists in interface"""
        from tradingagents.dataflows import interface

        assert hasattr(interface, 'get_finnhub_news_online'), \
            "get_finnhub_news_online should exist in interface"

    def test_finnhub_news_exists(self):
        """Verify get_finnhub_news exists in interface"""
        from tradingagents.dataflows import interface

        assert hasattr(interface, 'get_finnhub_news'), \
            "get_finnhub_news should exist in interface"


class TestNewsAnalystToolConfiguration:
    """Tests for news analyst tool configuration"""

    def test_stock_news_tools_use_finnhub_only(self):
        """Verify stock news analysis uses Finnhub API only"""
        from tradingagents.agents.analysts.news_analyst import create_news_analyst

        # Create a mock toolkit
        mock_toolkit = MagicMock()
        mock_toolkit.get_finnhub_news_online = MagicMock(name='get_finnhub_news_online')
        mock_toolkit.get_coindesk_news = MagicMock(name='get_coindesk_news')

        mock_llm = MagicMock()

        # Create the news analyst node
        news_analyst_node = create_news_analyst(mock_llm, mock_toolkit)

        # Verify the function is created successfully
        assert news_analyst_node is not None
        assert callable(news_analyst_node)

    def test_crypto_news_tools_use_coindesk_and_finnhub(self):
        """Verify crypto news analysis uses CoinDesk and Finnhub"""
        from tradingagents.agents.analysts.news_analyst import create_news_analyst

        mock_toolkit = MagicMock()
        mock_toolkit.get_finnhub_news_online = MagicMock(name='get_finnhub_news_online')
        mock_toolkit.get_coindesk_news = MagicMock(name='get_coindesk_news')

        mock_llm = MagicMock()

        # Create the news analyst node
        news_analyst_node = create_news_analyst(mock_llm, mock_toolkit)

        # Verify the function is created successfully
        assert news_analyst_node is not None
        assert callable(news_analyst_node)


class TestNewsScreenerConfiguration:
    """Tests for news screener after Google News removal"""

    def test_news_screener_imports_without_google_news(self):
        """Verify news_screener imports work without Google News"""
        from tradingagents.scanner.news_screener import (
            analyze_sentiment,
            get_news_for_symbol,
            score_news,
        )

        # All functions should be importable
        assert callable(analyze_sentiment)
        assert callable(get_news_for_symbol)
        assert callable(score_news)

    @patch("tradingagents.scanner.news_screener.FinnhubDataFetcher")
    def test_news_screener_uses_finnhub_only(self, mock_finnhub_class):
        """Verify news screener uses only Finnhub for news"""
        from tradingagents.scanner.news_screener import get_news_for_symbol

        mock_fetcher = MagicMock()
        mock_finnhub_class.return_value = mock_fetcher
        mock_fetcher.get_company_news.return_value = [
            {"headline": "Test news", "source": "Finnhub"},
        ]

        result = get_news_for_symbol("AAPL", use_llm=False)

        # Should get result from Finnhub
        assert len(result) == 1
        assert result[0]["source"] == "Finnhub"

        # Verify Finnhub was called
        mock_fetcher.get_company_news.assert_called_once()


class TestTradingGraphNewsTools:
    """Tests for trading graph news tool configuration"""

    def test_trading_graph_news_tools_import(self):
        """Verify trading graph can be imported without Google News"""
        # This will fail if Google News is still referenced
        from tradingagents.graph.trading_graph import TradingAgentsGraph

        assert TradingAgentsGraph is not None
