"""
Tests for advanced_search_system/evidence/requirements.py

Tests cover:
- EvidenceRequirements.get_requirements method
- EvidenceRequirements.get_minimum_confidence method
- Different constraint types
"""


class TestGetRequirements:
    """Tests for EvidenceRequirements.get_requirements method."""

    def test_property_constraint_requirements(self):
        """Test requirements for PROPERTY constraint."""
        from local_deep_research.advanced_search_system.evidence.requirements import (
            EvidenceRequirements,
        )
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            ConstraintType,
        )

        reqs = EvidenceRequirements.get_requirements(ConstraintType.PROPERTY)

        assert "preferred" in reqs
        assert "acceptable" in reqs
        assert "sources" in reqs
        assert "direct_statement" in reqs["preferred"]
        assert "official_record" in reqs["preferred"]

    def test_name_pattern_constraint_requirements(self):
        """Test requirements for NAME_PATTERN constraint."""
        from local_deep_research.advanced_search_system.evidence.requirements import (
            EvidenceRequirements,
        )
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            ConstraintType,
        )

        reqs = EvidenceRequirements.get_requirements(
            ConstraintType.NAME_PATTERN
        )

        assert "etymology sources" in reqs["sources"]
        assert "naming databases" in reqs["sources"]

    def test_event_constraint_requirements(self):
        """Test requirements for EVENT constraint."""
        from local_deep_research.advanced_search_system.evidence.requirements import (
            EvidenceRequirements,
        )
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            ConstraintType,
        )

        reqs = EvidenceRequirements.get_requirements(ConstraintType.EVENT)

        assert "news_report" in reqs["preferred"]
        assert "official_record" in reqs["preferred"]
        assert "news archives" in reqs["sources"]

    def test_statistic_constraint_requirements(self):
        """Test requirements for STATISTIC constraint."""
        from local_deep_research.advanced_search_system.evidence.requirements import (
            EvidenceRequirements,
        )
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            ConstraintType,
        )

        reqs = EvidenceRequirements.get_requirements(ConstraintType.STATISTIC)

        assert "statistical_data" in reqs["preferred"]
        assert "government databases" in reqs["sources"]

    def test_temporal_constraint_requirements(self):
        """Test requirements for TEMPORAL constraint."""
        from local_deep_research.advanced_search_system.evidence.requirements import (
            EvidenceRequirements,
        )
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            ConstraintType,
        )

        reqs = EvidenceRequirements.get_requirements(ConstraintType.TEMPORAL)

        assert "official_record" in reqs["preferred"]
        assert "archives" in reqs["sources"]

    def test_location_constraint_requirements(self):
        """Test requirements for LOCATION constraint."""
        from local_deep_research.advanced_search_system.evidence.requirements import (
            EvidenceRequirements,
        )
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            ConstraintType,
        )

        reqs = EvidenceRequirements.get_requirements(ConstraintType.LOCATION)

        assert "geographical_data" in reqs["preferred"]
        assert "maps" in reqs["sources"]

    def test_comparison_constraint_requirements(self):
        """Test requirements for COMPARISON constraint."""
        from local_deep_research.advanced_search_system.evidence.requirements import (
            EvidenceRequirements,
        )
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            ConstraintType,
        )

        reqs = EvidenceRequirements.get_requirements(ConstraintType.COMPARISON)

        assert "research_finding" in reqs["preferred"]
        assert "comparative studies" in reqs["sources"]

    def test_existence_constraint_requirements(self):
        """Test requirements for EXISTENCE constraint."""
        from local_deep_research.advanced_search_system.evidence.requirements import (
            EvidenceRequirements,
        )
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            ConstraintType,
        )

        reqs = EvidenceRequirements.get_requirements(ConstraintType.EXISTENCE)

        assert "direct_statement" in reqs["preferred"]
        assert "official registries" in reqs["sources"]

    def test_unknown_constraint_returns_default(self):
        """Test that unknown constraint returns default requirements."""
        from local_deep_research.advanced_search_system.evidence.requirements import (
            EvidenceRequirements,
        )
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            ConstraintType,
        )

        # Use a constraint type that might not have specific requirements
        # Since all defined types are covered, test with CUSTOM if it exists
        # or verify the default fallback structure
        reqs = EvidenceRequirements.get_requirements(ConstraintType.PROPERTY)

        # Verify structure
        assert isinstance(reqs, dict)
        assert "preferred" in reqs
        assert "acceptable" in reqs
        assert "sources" in reqs


class TestGetMinimumConfidence:
    """Tests for EvidenceRequirements.get_minimum_confidence method."""

    def test_statistic_requires_high_confidence(self):
        """Test STATISTIC requires highest confidence."""
        from local_deep_research.advanced_search_system.evidence.requirements import (
            EvidenceRequirements,
        )
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            ConstraintType,
        )

        confidence = EvidenceRequirements.get_minimum_confidence(
            ConstraintType.STATISTIC
        )

        assert confidence == 0.8

    def test_event_requires_moderate_confidence(self):
        """Test EVENT requires moderate confidence."""
        from local_deep_research.advanced_search_system.evidence.requirements import (
            EvidenceRequirements,
        )
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            ConstraintType,
        )

        confidence = EvidenceRequirements.get_minimum_confidence(
            ConstraintType.EVENT
        )

        assert confidence == 0.7

    def test_property_requires_some_flexibility(self):
        """Test PROPERTY has some flexibility."""
        from local_deep_research.advanced_search_system.evidence.requirements import (
            EvidenceRequirements,
        )
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            ConstraintType,
        )

        confidence = EvidenceRequirements.get_minimum_confidence(
            ConstraintType.PROPERTY
        )

        assert confidence == 0.6

    def test_name_pattern_is_interpretive(self):
        """Test NAME_PATTERN allows interpretation."""
        from local_deep_research.advanced_search_system.evidence.requirements import (
            EvidenceRequirements,
        )
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            ConstraintType,
        )

        confidence = EvidenceRequirements.get_minimum_confidence(
            ConstraintType.NAME_PATTERN
        )

        assert confidence == 0.5

    def test_unknown_constraint_returns_default(self):
        """Test unknown constraint returns default confidence."""
        from local_deep_research.advanced_search_system.evidence.requirements import (
            EvidenceRequirements,
        )
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            ConstraintType,
        )

        # For constraints not in the thresholds dict
        confidence = EvidenceRequirements.get_minimum_confidence(
            ConstraintType.TEMPORAL
        )

        assert confidence == 0.6  # Default

    def test_confidence_ordering(self):
        """Test that confidence requirements are logically ordered."""
        from local_deep_research.advanced_search_system.evidence.requirements import (
            EvidenceRequirements,
        )
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            ConstraintType,
        )

        statistic = EvidenceRequirements.get_minimum_confidence(
            ConstraintType.STATISTIC
        )
        event = EvidenceRequirements.get_minimum_confidence(
            ConstraintType.EVENT
        )
        property_conf = EvidenceRequirements.get_minimum_confidence(
            ConstraintType.PROPERTY
        )
        name_pattern = EvidenceRequirements.get_minimum_confidence(
            ConstraintType.NAME_PATTERN
        )

        # Statistics need highest accuracy
        assert statistic > event
        assert event > property_conf
        assert property_conf > name_pattern


class TestRequirementsStructure:
    """Tests for the structure of requirements."""

    def test_all_requirements_have_three_keys(self):
        """Test all requirements have preferred, acceptable, sources."""
        from local_deep_research.advanced_search_system.evidence.requirements import (
            EvidenceRequirements,
        )
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            ConstraintType,
        )

        constraint_types = [
            ConstraintType.PROPERTY,
            ConstraintType.NAME_PATTERN,
            ConstraintType.EVENT,
            ConstraintType.STATISTIC,
            ConstraintType.TEMPORAL,
            ConstraintType.LOCATION,
            ConstraintType.COMPARISON,
            ConstraintType.EXISTENCE,
        ]

        for ct in constraint_types:
            reqs = EvidenceRequirements.get_requirements(ct)
            assert "preferred" in reqs, f"Missing 'preferred' for {ct}"
            assert "acceptable" in reqs, f"Missing 'acceptable' for {ct}"
            assert "sources" in reqs, f"Missing 'sources' for {ct}"

    def test_preferred_and_acceptable_are_lists(self):
        """Test that preferred and acceptable are lists."""
        from local_deep_research.advanced_search_system.evidence.requirements import (
            EvidenceRequirements,
        )
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            ConstraintType,
        )

        reqs = EvidenceRequirements.get_requirements(ConstraintType.PROPERTY)

        assert isinstance(reqs["preferred"], list)
        assert isinstance(reqs["acceptable"], list)
        assert isinstance(reqs["sources"], list)

    def test_sources_are_non_empty(self):
        """Test that sources lists are non-empty."""
        from local_deep_research.advanced_search_system.evidence.requirements import (
            EvidenceRequirements,
        )
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            ConstraintType,
        )

        constraint_types = [
            ConstraintType.PROPERTY,
            ConstraintType.EVENT,
            ConstraintType.STATISTIC,
        ]

        for ct in constraint_types:
            reqs = EvidenceRequirements.get_requirements(ct)
            assert len(reqs["sources"]) > 0, f"Empty sources for {ct}"
