"""
Pure-logic tests for EvidenceEvaluator parsing and scoring helpers.

Tests _parse_evidence_response, _parse_evidence_type, and
_assess_match_quality — no LLM or search calls.
"""

from unittest.mock import Mock

from local_deep_research.advanced_search_system.constraints.base_constraint import (
    Constraint,
    ConstraintType,
)
from local_deep_research.advanced_search_system.evidence.base_evidence import (
    Evidence,
    EvidenceType,
)
from local_deep_research.advanced_search_system.evidence.evaluator import (
    EvidenceEvaluator,
)


def _evaluator():
    return EvidenceEvaluator(model=Mock())


def _constraint(value="test value"):
    return Constraint(
        id="c1",
        type=ConstraintType.PROPERTY,
        description=value,
        value=value,
        weight=1.0,
    )


def _evidence(claim="test claim"):
    return Evidence(
        claim=claim,
        type=EvidenceType.DIRECT_STATEMENT,
        source="src",
        confidence=0.8,
    )


# ---------------------------------------------------------------------------
# _parse_evidence_response
# ---------------------------------------------------------------------------


class TestParseEvidenceResponse:
    """Verify LLM response parsing into evidence components."""

    def test_no_colon_lines_skipped(self):
        """Lines without colons are skipped."""
        ev = _evaluator()
        content = "CLAIM: test\nThis line has no colon\nTYPE: inference"
        result = ev._parse_evidence_response(content)
        assert result["claim"] == "test"
        assert result["type"] == "inference"


# ---------------------------------------------------------------------------
# _parse_evidence_type
# ---------------------------------------------------------------------------


# ---------------------------------------------------------------------------
# _assess_match_quality
# ---------------------------------------------------------------------------


class TestAssessMatchQuality:
    """Verify evidence-constraint match quality scoring."""

    def test_exact_match_returns_1(self):
        """Constraint value fully in claim returns 1.0."""
        ev = _evaluator()
        constraint = _constraint("Mount Rainier")
        evidence = _evidence("Mount Rainier is a volcano in Washington")
        assert ev._assess_match_quality(evidence, constraint) == 1.0

    def test_partial_word_match_returns_08(self):
        """Some constraint words in claim returns 0.8."""
        ev = _evaluator()
        constraint = _constraint("Mount Rainier elevation")
        evidence = _evidence("The elevation of the peak is 14411 feet")
        result = ev._assess_match_quality(evidence, constraint)
        assert result == 0.8

    def test_no_match_returns_06(self):
        """No constraint words in claim returns 0.6."""
        ev = _evaluator()
        constraint = _constraint("population density")
        evidence = _evidence("The weather is sunny today")
        result = ev._assess_match_quality(evidence, constraint)
        assert result == 0.6

    def test_case_insensitive_matching(self):
        """Matching is case-insensitive."""
        ev = _evaluator()
        constraint = _constraint("MOUNT RAINIER")
        evidence = _evidence("mount rainier is beautiful")
        assert ev._assess_match_quality(evidence, constraint) == 1.0

    def test_single_word_match(self):
        """Single word from multi-word constraint matches at 0.8."""
        ev = _evaluator()
        constraint = _constraint("tall mountain peak")
        evidence = _evidence("The mountain was first climbed in 1899")
        result = ev._assess_match_quality(evidence, constraint)
        assert result == 0.8
