from rights_filter.admin.detailed_review_presenter import detailed_review_for
from rights_filter.admin.review_handlers import applicant_summary_for
from rights_filter.domain.records import (
    AnalysisRun,
    Evidence,
    EvidenceSource,
    InMemoryRightsFilterRepository,
    ScoreResult,
)


def test_detailed_review_groups_operator_evidence_and_actions():
    repo = InMemoryRightsFilterRepository()
    run = AnalysisRun.for_submission("submission-1", "v1")
    run.add_evidence(
        Evidence(
            source=EvidenceSource.NAVER_SEARCH,
            reason="Naver result linked named work",
            confidence=0.8,
            data={"result_url": "https://example.test/page", "query": "IU album"},
        )
    )
    run.add_evidence(
        Evidence(
            source=EvidenceSource.WEB_DETECTION,
            reason="Web entity matched IU",
            confidence=0.9,
            data={"entity": "IU"},
        )
    )
    run.add_evidence(
        Evidence(
            source=EvidenceSource.LLM_SUMMARY,
            reason="Assistant summarized source-linked evidence",
            confidence=0.0,
            data={"summary": "Both sources mention IU.", "source_urls": ["url"]},
        )
    )
    run.add_evidence(
        Evidence(
            source=EvidenceSource.SEARCH_SKIPPED,
            reason="search API disabled",
            confidence=1.0,
            data={"provider": "naver"},
        )
    )
    run.score = ScoreResult(
        score=78,
        band="high",
        reasons=["Naver result linked named work", "Web entity matched IU"],
    )
    repo.save_analysis_run(run)

    review = detailed_review_for(
        repo,
        "submission-1",
        image_reference="internal://submission-1/original",
    )

    assert review["submission_id"] == "submission-1"
    assert review["image_reference"] == "internal://submission-1/original"
    assert review["score"] == 78
    assert review["band"] == "high"
    assert review["manual_actions"] == ["approved", "held", "rejected"]
    assert len(review["evidence_groups"]["naver"]) == 1
    assert len(review["evidence_groups"]["google"]) == 1
    assert len(review["evidence_groups"]["llm"]) == 1
    assert len(review["evidence_groups"]["failures"]) == 1


def test_missing_analysis_returns_unavailable_review_model():
    review = detailed_review_for(
        InMemoryRightsFilterRepository(),
        "missing",
        image_reference="internal://missing/original",
    )

    assert review["analysis_available"] is False
    assert review["evidence_groups"] == {}


def test_applicant_summary_excludes_enrichment_details():
    repo = InMemoryRightsFilterRepository()
    run = AnalysisRun.for_submission("submission-1", "v1")
    run.add_evidence(
        Evidence(
            source=EvidenceSource.LLM_SUMMARY,
            reason="Assistant summarized source-linked evidence",
            confidence=0.0,
            data={"summary": "Internal summary"},
        )
    )
    run.score = ScoreResult(score=50, band="medium", reasons=["internal"])
    repo.save_analysis_run(run)

    applicant = applicant_summary_for(repo, "submission-1")

    assert applicant == {"submission_id": "submission-1"}
