from rights_filter.admin.review_handlers import (
    applicant_summary_for,
    operator_summary_for,
    record_operator_decision,
)
from rights_filter.domain.records import (
    AnalysisRun,
    Evidence,
    EvidenceSource,
    InMemoryRightsFilterRepository,
    ReviewStatus,
    ScoreResult,
)


def test_high_risk_score_is_operator_visible_but_not_applicant_visible():
    repo = InMemoryRightsFilterRepository()
    run = AnalysisRun.for_submission("submission-1", analysis_version="v1")
    run.add_evidence(
        Evidence(
            source=EvidenceSource.WEB_DETECTION,
            reason="Web entity matched 아이유",
            confidence=0.93,
            data={"entity": "아이유"},
        )
    )
    run.score = ScoreResult(score=88, band="high", reasons=["Web entity matched 아이유"])
    repo.save_analysis_run(run)

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

    assert operator["score"] == 88
    assert operator["evidence"][0]["reason"] == "Web entity matched 아이유"
    assert "score" not in applicant
    assert "evidence" not in applicant


def test_rejection_creates_automatic_knowledge_entry():
    repo = InMemoryRightsFilterRepository()
    decision = record_operator_decision(
        repo,
        submission_id="submission-2",
        status=ReviewStatus.REJECTED,
        memo="캐릭터 권리 위험",
        fingerprints=["phash:abc"],
    )

    entries = repo.active_knowledge_entries()
    assert decision.status == ReviewStatus.REJECTED
    assert len(entries) == 1
    assert entries[0].source_decision_id == decision.id
