from rights_filter.analysis.llm_assistance import FakeInternalLlmClient, InternalLlmAssistant
from rights_filter.domain.records import Evidence, EvidenceSource


def test_llm_summary_cites_source_urls_and_evidence_ids():
    source_evidence = [
        Evidence(
            source=EvidenceSource.NAVER_SEARCH,
            reason="Naver search result found",
            confidence=0.8,
            data={
                "evidence_id": "evidence-1",
                "result_url": "https://example.test/page",
                "title": "IU official album cover",
            },
        )
    ]
    assistant = InternalLlmAssistant(
        FakeInternalLlmClient(summary="Search evidence cites the named work.")
    )

    evidence = assistant.summarize("submission-1", source_evidence)

    assert evidence.source == EvidenceSource.LLM_SUMMARY
    assert evidence.confidence == 0.0
    assert evidence.data["summary"] == "Search evidence cites the named work."
    assert evidence.data["source_urls"] == ["https://example.test/page"]
    assert evidence.data["source_evidence_ids"] == ["evidence-1"]


def test_llm_summary_without_sources_is_marked_unverified():
    assistant = InternalLlmAssistant(FakeInternalLlmClient(summary="Looks famous."))

    evidence = assistant.summarize("submission-1", [])

    assert evidence.source == EvidenceSource.LLM_SUMMARY
    assert evidence.reason == "LLM summary has no source evidence"
    assert evidence.data["verified"] is False


def test_llm_failure_becomes_enrichment_failure_evidence():
    assistant = InternalLlmAssistant(FakeInternalLlmClient(error=RuntimeError("down")))

    evidence = assistant.summarize("submission-1", [])

    assert evidence.source == EvidenceSource.ENRICHMENT_FAILURE
    assert "LLM assistance failed" in evidence.reason
