"""
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 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
