"""High-value edge case tests for source_management/diversity_manager.py.

Tests pure logic methods (no LLM calls) covering domain extraction,
source type classification, credibility scoring, temporal/geographic
extraction, and diversity metrics calculation.
"""

from unittest.mock import MagicMock

from local_deep_research.advanced_search_system.source_management.diversity_manager import (
    SourceDiversityManager,
    SourceProfile,
)


class TestSourceProfileDefaults:
    """Test SourceProfile dataclass default values."""

    def test_optional_fields_default_to_none(self):
        """temporal_coverage, geographic_focus, last_accessed default to None."""
        p = SourceProfile(
            url="http://example.com",
            domain="example.com",
            source_type="general",
            credibility_score=0.5,
        )
        assert p.temporal_coverage is None
        assert p.geographic_focus is None
        assert p.last_accessed is None
        assert p.evidence_count == 0
        assert p.specialties == []


class TestExtractDomain:
    """Test SourceDiversityManager._extract_domain."""

    def _mgr(self):
        return SourceDiversityManager(model=MagicMock())

    def test_https_url(self):
        assert (
            self._mgr()._extract_domain("https://example.com/path")
            == "example.com"
        )

    def test_http_url(self):
        assert self._mgr()._extract_domain("http://test.org/page") == "test.org"

    def test_strips_www(self):
        assert (
            self._mgr()._extract_domain("https://www.example.com/p")
            == "example.com"
        )

    def test_bare_domain_no_path(self):
        """URL with no trailing slash or path."""
        assert (
            self._mgr()._extract_domain("https://example.com") == "example.com"
        )

    def test_fallback_for_non_url(self):
        """Non-URL string is returned as-is."""
        assert self._mgr()._extract_domain("not-a-url") == "not-a-url"


class TestDetermineSourceType:
    """Test _determine_source_type with known domain patterns."""

    def _mgr(self):
        return SourceDiversityManager(model=MagicMock())

    def test_edu_domain_is_academic(self):
        result = self._mgr()._determine_source_type(
            "https://mit.edu/paper", "mit.edu", None
        )
        assert result == "academic"

    def test_arxiv_is_academic(self):
        result = self._mgr()._determine_source_type(
            "https://arxiv.org/abs/123", "arxiv.org", None
        )
        assert result == "academic"

    def test_jstor_is_academic(self):
        result = self._mgr()._determine_source_type(
            "https://jstor.org/stable/123", "jstor.org", None
        )
        assert result == "academic"

    def test_gov_domain_is_government(self):
        result = self._mgr()._determine_source_type(
            "https://nasa.gov/data", "nasa.gov", None
        )
        assert result == "government"

    def test_wikipedia_is_wiki(self):
        result = self._mgr()._determine_source_type(
            "https://en.wikipedia.org/wiki/Test", "en.wikipedia.org", None
        )
        assert result == "wiki"

    def test_bbc_is_news(self):
        result = self._mgr()._determine_source_type(
            "https://bbc.co.uk/news", "bbc.co.uk", None
        )
        assert result == "news"

    def test_nytimes_is_news(self):
        result = self._mgr()._determine_source_type(
            "https://nytimes.com/article", "nytimes.com", None
        )
        assert result == "news"

    def test_no_match_no_content_is_general(self):
        """Unknown domain without content returns 'general' without LLM call."""
        mgr = self._mgr()
        result = mgr._determine_source_type(
            "https://random-site.io/page", "random-site.io", None
        )
        assert result == "general"
        mgr.model.invoke.assert_not_called()


class TestCalculateCredibility:
    """Test _calculate_credibility scoring logic."""

    def _mgr(self):
        return SourceDiversityManager(model=MagicMock())

    def test_edu_domain_bonus(self):
        """'.edu' in domain adds 0.1 to base score."""
        score = self._mgr()._calculate_credibility(
            "https://mit.edu", "mit.edu", "academic", None
        )
        # academic base=0.9, +0.1 capped at 1.0
        assert score == 1.0

    def test_https_bonus(self):
        """HTTPS URL adds 0.05 to base score."""
        score = self._mgr()._calculate_credibility(
            "https://example.com", "example.com", "general", None
        )
        # general base=0.5, +0.05=0.55
        assert score == 0.55

    def test_bonuses_stack(self):
        """Domain and HTTPS bonuses compound on top of base score."""
        score = self._mgr()._calculate_credibility(
            "https://example.edu/page", "example.edu", "general", None
        )
        # general=0.5 + edu=0.1 + https=0.05 = 0.65
        assert abs(score - 0.65) < 0.01

    def test_citation_pattern_bonus(self):
        """Content with citation patterns adds 0.1."""
        score = self._mgr()._calculate_credibility(
            "http://example.com",
            "example.com",
            "general",
            "See reference [1] and (2024)",
        )
        # general=0.5 + citation=0.1 = 0.6
        assert score >= 0.6

    def test_year_only_citation_pattern(self):
        """Year-in-parens pattern alone triggers the bonus."""
        score = self._mgr()._calculate_credibility(
            "http://example.com",
            "example.com",
            "general",
            "Published (2021) in journal",
        )
        assert score >= 0.6


class TestExtractTemporalCoverage:
    """Test _extract_temporal_coverage."""

    def _mgr(self):
        return SourceDiversityManager(model=MagicMock())

    def test_none_content(self):
        assert self._mgr()._extract_temporal_coverage(None) is None

    def test_no_years_in_content(self):
        assert self._mgr()._extract_temporal_coverage("No years here") is None

    def test_single_year(self):
        result = self._mgr()._extract_temporal_coverage("Published in 2020")
        assert result is not None
        assert result[0].year == 2020
        assert result[1].year == 2020

    def test_multiple_years_span(self):
        result = self._mgr()._extract_temporal_coverage(
            "From 1990 to 2020 data"
        )
        assert result[0].year == 1990
        assert result[1].year == 2020


class TestExtractGeographicFocus:
    """Test _extract_geographic_focus."""

    def _mgr(self):
        return SourceDiversityManager(model=MagicMock())

    def test_uk_tld(self):
        result = self._mgr()._extract_geographic_focus(
            "https://example.uk/page", None
        )
        assert result == "United Kingdom"

    def test_au_tld(self):
        result = self._mgr()._extract_geographic_focus(
            "https://example.com.au/page", None
        )
        assert result == "Australia"

    def test_us_path_segment(self):
        result = self._mgr()._extract_geographic_focus(
            "https://example.com/us/policy", None
        )
        assert result == "United States"

    def test_no_geographic_info(self):
        result = self._mgr()._extract_geographic_focus(
            "https://example.com", None
        )
        assert result is None

    def test_content_location_detected(self):
        result = self._mgr()._extract_geographic_focus(
            "https://example.com", "The United States leads in research"
        )
        assert result is not None


class TestCalculateDiversityMetrics:
    """Test calculate_diversity_metrics."""

    def test_empty_sources_returns_zeros(self):
        """Empty source list returns all-zero metrics."""
        mgr = SourceDiversityManager(model=MagicMock())
        metrics = mgr.calculate_diversity_metrics([])
        assert metrics.type_diversity == 0.0
        assert metrics.overall_score == 0.0
        assert metrics.credibility_distribution == {}

    def test_geographic_diversity_capped_at_one(self):
        """Geographic diversity caps at 1.0 with many regions."""
        mgr = SourceDiversityManager(model=MagicMock())
        for i, region in enumerate(["US", "UK", "Canada", "Australia"]):
            url = f"https://site{i}.com"
            mgr.source_profiles[url] = SourceProfile(
                url=url,
                domain=f"site{i}.com",
                source_type="general",
                credibility_score=0.5,
                geographic_focus=region,
            )
        metrics = mgr.calculate_diversity_metrics(
            [f"https://site{i}.com" for i in range(4)]
        )
        assert metrics.geographic_diversity == 1.0

    def test_credibility_boundary_0_8_is_high(self):
        """Score of exactly 0.8 is classified as 'high'."""
        mgr = SourceDiversityManager(model=MagicMock())
        url = "https://test.com"
        mgr.source_profiles[url] = SourceProfile(
            url=url,
            domain="test.com",
            source_type="academic",
            credibility_score=0.8,
        )
        metrics = mgr.calculate_diversity_metrics([url])
        assert metrics.credibility_distribution.get("high", 0) == 1


class TestSelectDiverseSources:
    """Test select_diverse_sources."""

    def test_fewer_than_target_returns_all(self):
        """When fewer sources than target, all are returned."""
        mgr = SourceDiversityManager(model=MagicMock())
        sources = ["https://a.com", "https://b.com"]
        result = mgr.select_diverse_sources(sources, target_count=5)
        assert result == sources

    def test_empty_source_list(self):
        """Empty source list returns empty list."""
        mgr = SourceDiversityManager(model=MagicMock())
        result = mgr.select_diverse_sources([], target_count=5)
        assert result == []


class TestHelperMethods:
    """Test _get_source_type_modifier and _get_region_domain."""

    def _mgr(self):
        return SourceDiversityManager(model=MagicMock())

    def test_academic_modifier_nonempty(self):
        result = self._mgr()._get_source_type_modifier("academic")
        assert len(result) > 0
        assert ".edu" in result

    def test_government_modifier_nonempty(self):
        result = self._mgr()._get_source_type_modifier("government")
        assert ".gov" in result

    def test_unknown_modifier_empty(self):
        result = self._mgr()._get_source_type_modifier("unknown_type")
        assert result == ""

    def test_united_states_domain(self):
        result = self._mgr()._get_region_domain("United States")
        assert ".us" in result
        assert ".com" in result

    def test_unknown_region_fallback(self):
        result = self._mgr()._get_region_domain("Antarctica")
        assert result == ".com"
