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

Tests cover:
- EvidenceType enum and base confidence values
- Evidence dataclass initialization
- Automatic confidence calculation
"""

from datetime import datetime


class TestEvidenceType:
    """Tests for EvidenceType enum."""

    def test_direct_statement_value(self):
        """Test DIRECT_STATEMENT has correct value."""
        from local_deep_research.advanced_search_system.evidence.base_evidence import (
            EvidenceType,
        )

        assert EvidenceType.DIRECT_STATEMENT.value == "direct_statement"

    def test_official_record_value(self):
        """Test OFFICIAL_RECORD has correct value."""
        from local_deep_research.advanced_search_system.evidence.base_evidence import (
            EvidenceType,
        )

        assert EvidenceType.OFFICIAL_RECORD.value == "official_record"

    def test_research_finding_value(self):
        """Test RESEARCH_FINDING has correct value."""
        from local_deep_research.advanced_search_system.evidence.base_evidence import (
            EvidenceType,
        )

        assert EvidenceType.RESEARCH_FINDING.value == "research_finding"

    def test_news_report_value(self):
        """Test NEWS_REPORT has correct value."""
        from local_deep_research.advanced_search_system.evidence.base_evidence import (
            EvidenceType,
        )

        assert EvidenceType.NEWS_REPORT.value == "news_report"

    def test_statistical_data_value(self):
        """Test STATISTICAL_DATA has correct value."""
        from local_deep_research.advanced_search_system.evidence.base_evidence import (
            EvidenceType,
        )

        assert EvidenceType.STATISTICAL_DATA.value == "statistical_data"

    def test_inference_value(self):
        """Test INFERENCE has correct value."""
        from local_deep_research.advanced_search_system.evidence.base_evidence import (
            EvidenceType,
        )

        assert EvidenceType.INFERENCE.value == "inference"

    def test_correlation_value(self):
        """Test CORRELATION has correct value."""
        from local_deep_research.advanced_search_system.evidence.base_evidence import (
            EvidenceType,
        )

        assert EvidenceType.CORRELATION.value == "correlation"

    def test_speculation_value(self):
        """Test SPECULATION has correct value."""
        from local_deep_research.advanced_search_system.evidence.base_evidence import (
            EvidenceType,
        )

        assert EvidenceType.SPECULATION.value == "speculation"


class TestEvidenceTypeBaseConfidence:
    """Tests for EvidenceType base confidence property."""

    def test_direct_statement_confidence(self):
        """Test DIRECT_STATEMENT has highest confidence."""
        from local_deep_research.advanced_search_system.evidence.base_evidence import (
            EvidenceType,
        )

        assert EvidenceType.DIRECT_STATEMENT.base_confidence == 0.95

    def test_official_record_confidence(self):
        """Test OFFICIAL_RECORD confidence."""
        from local_deep_research.advanced_search_system.evidence.base_evidence import (
            EvidenceType,
        )

        assert EvidenceType.OFFICIAL_RECORD.base_confidence == 0.90

    def test_research_finding_confidence(self):
        """Test RESEARCH_FINDING confidence."""
        from local_deep_research.advanced_search_system.evidence.base_evidence import (
            EvidenceType,
        )

        assert EvidenceType.RESEARCH_FINDING.base_confidence == 0.85

    def test_statistical_data_confidence(self):
        """Test STATISTICAL_DATA confidence."""
        from local_deep_research.advanced_search_system.evidence.base_evidence import (
            EvidenceType,
        )

        assert EvidenceType.STATISTICAL_DATA.base_confidence == 0.85

    def test_news_report_confidence(self):
        """Test NEWS_REPORT confidence."""
        from local_deep_research.advanced_search_system.evidence.base_evidence import (
            EvidenceType,
        )

        assert EvidenceType.NEWS_REPORT.base_confidence == 0.75

    def test_inference_confidence(self):
        """Test INFERENCE confidence."""
        from local_deep_research.advanced_search_system.evidence.base_evidence import (
            EvidenceType,
        )

        assert EvidenceType.INFERENCE.base_confidence == 0.50

    def test_correlation_confidence(self):
        """Test CORRELATION has low confidence."""
        from local_deep_research.advanced_search_system.evidence.base_evidence import (
            EvidenceType,
        )

        assert EvidenceType.CORRELATION.base_confidence == 0.30

    def test_speculation_confidence(self):
        """Test SPECULATION has lowest confidence."""
        from local_deep_research.advanced_search_system.evidence.base_evidence import (
            EvidenceType,
        )

        assert EvidenceType.SPECULATION.base_confidence == 0.10

    def test_confidence_ordering(self):
        """Test that confidence values are logically ordered."""
        from local_deep_research.advanced_search_system.evidence.base_evidence import (
            EvidenceType,
        )

        assert (
            EvidenceType.DIRECT_STATEMENT.base_confidence
            > EvidenceType.OFFICIAL_RECORD.base_confidence
        )
        assert (
            EvidenceType.OFFICIAL_RECORD.base_confidence
            > EvidenceType.RESEARCH_FINDING.base_confidence
        )
        assert (
            EvidenceType.NEWS_REPORT.base_confidence
            > EvidenceType.INFERENCE.base_confidence
        )
        assert (
            EvidenceType.INFERENCE.base_confidence
            > EvidenceType.CORRELATION.base_confidence
        )
        assert (
            EvidenceType.CORRELATION.base_confidence
            > EvidenceType.SPECULATION.base_confidence
        )


class TestEvidenceDataclass:
    """Tests for Evidence dataclass."""

    def test_init_with_required_fields(self):
        """Test initialization with required fields only."""
        from local_deep_research.advanced_search_system.evidence.base_evidence import (
            Evidence,
            EvidenceType,
        )

        evidence = Evidence(
            claim="Test claim",
            type=EvidenceType.NEWS_REPORT,
            source="Test source",
        )

        assert evidence.claim == "Test claim"
        assert evidence.type == EvidenceType.NEWS_REPORT
        assert evidence.source == "Test source"

    def test_init_auto_calculates_confidence(self):
        """Test that confidence is auto-calculated from type."""
        from local_deep_research.advanced_search_system.evidence.base_evidence import (
            Evidence,
            EvidenceType,
        )

        evidence = Evidence(
            claim="Test claim",
            type=EvidenceType.DIRECT_STATEMENT,
            source="Test source",
        )

        assert evidence.confidence == 0.95

    def test_init_with_explicit_confidence(self):
        """Test initialization with explicit confidence."""
        from local_deep_research.advanced_search_system.evidence.base_evidence import (
            Evidence,
            EvidenceType,
        )

        evidence = Evidence(
            claim="Test claim",
            type=EvidenceType.NEWS_REPORT,
            source="Test source",
            confidence=0.8,
        )

        assert evidence.confidence == 0.8

    def test_init_with_reasoning(self):
        """Test initialization with reasoning."""
        from local_deep_research.advanced_search_system.evidence.base_evidence import (
            Evidence,
            EvidenceType,
        )

        evidence = Evidence(
            claim="Test claim",
            type=EvidenceType.INFERENCE,
            source="Test source",
            reasoning="Based on logical deduction",
        )

        assert evidence.reasoning == "Based on logical deduction"

    def test_init_with_raw_text(self):
        """Test initialization with raw_text."""
        from local_deep_research.advanced_search_system.evidence.base_evidence import (
            Evidence,
            EvidenceType,
        )

        evidence = Evidence(
            claim="Test claim",
            type=EvidenceType.NEWS_REPORT,
            source="Test source",
            raw_text="Original quote from source",
        )

        assert evidence.raw_text == "Original quote from source"

    def test_init_creates_timestamp(self):
        """Test that timestamp is automatically created."""
        from local_deep_research.advanced_search_system.evidence.base_evidence import (
            Evidence,
            EvidenceType,
        )

        evidence = Evidence(
            claim="Test claim",
            type=EvidenceType.NEWS_REPORT,
            source="Test source",
        )

        assert evidence.timestamp is not None
        # Should be ISO format
        datetime.fromisoformat(evidence.timestamp.replace("Z", "+00:00"))

    def test_init_with_metadata(self):
        """Test initialization with metadata."""
        from local_deep_research.advanced_search_system.evidence.base_evidence import (
            Evidence,
            EvidenceType,
        )

        metadata = {"author": "John Doe", "publication": "Science Journal"}
        evidence = Evidence(
            claim="Test claim",
            type=EvidenceType.RESEARCH_FINDING,
            source="Test source",
            metadata=metadata,
        )

        assert evidence.metadata == metadata
        assert evidence.metadata["author"] == "John Doe"

    def test_init_default_metadata_is_empty_dict(self):
        """Test that default metadata is empty dict."""
        from local_deep_research.advanced_search_system.evidence.base_evidence import (
            Evidence,
            EvidenceType,
        )

        evidence = Evidence(
            claim="Test claim",
            type=EvidenceType.NEWS_REPORT,
            source="Test source",
        )

        assert evidence.metadata == {}

    def test_different_instances_have_different_metadata(self):
        """Test that different instances don't share metadata."""
        from local_deep_research.advanced_search_system.evidence.base_evidence import (
            Evidence,
            EvidenceType,
        )

        evidence1 = Evidence(
            claim="Claim 1",
            type=EvidenceType.NEWS_REPORT,
            source="Source 1",
        )
        evidence2 = Evidence(
            claim="Claim 2",
            type=EvidenceType.NEWS_REPORT,
            source="Source 2",
        )

        evidence1.metadata["key"] = "value1"

        assert "key" not in evidence2.metadata


class TestEvidencePostInit:
    """Tests for Evidence __post_init__ method."""

    def test_zero_confidence_triggers_auto_calculation(self):
        """Test that zero confidence triggers auto-calculation."""
        from local_deep_research.advanced_search_system.evidence.base_evidence import (
            Evidence,
            EvidenceType,
        )

        evidence = Evidence(
            claim="Test claim",
            type=EvidenceType.OFFICIAL_RECORD,
            source="Test source",
            confidence=0.0,
        )

        # Should be set to type's base confidence
        assert evidence.confidence == 0.90

    def test_nonzero_confidence_preserved(self):
        """Test that non-zero confidence is preserved."""
        from local_deep_research.advanced_search_system.evidence.base_evidence import (
            Evidence,
            EvidenceType,
        )

        evidence = Evidence(
            claim="Test claim",
            type=EvidenceType.OFFICIAL_RECORD,
            source="Test source",
            confidence=0.5,
        )

        # Should keep the explicit value
        assert evidence.confidence == 0.5
