from rights_filter.domain.records import (
    InMemoryRightsFilterRepository,
    KnowledgeEntryType,
    KnowledgeProvenance,
)
from rights_filter.domain.knowledge_base import create_manual_knowledge_entry


def test_create_manual_knowledge_entry_stores_aliases_and_policy_memo():
    entry = create_manual_knowledge_entry(
        entry_type=KnowledgeEntryType.CHARACTER,
        name="Sample character",
        aliases=["Sample hero"],
        related_keywords=["webtoon"],
        policy_memo="Reject unless licensed.",
        exception_conditions="licensed collaboration",
        sample_fingerprints=["phash:sample"],
    )

    assert entry.provenance == KnowledgeProvenance.MANUAL
    assert entry.aliases == ["Sample hero"]
    assert entry.related_keywords == ["webtoon"]
    assert entry.policy_memo == "Reject unless licensed."
    assert entry.exception_conditions == "licensed collaboration"


def test_deactivating_derived_entries_preserves_manual_entries():
    repo = InMemoryRightsFilterRepository()
    automatic = repo.create_rejected_image_entry(
        decision_id="decision-1",
        submission_id="submission-1",
        fingerprints=["phash:auto"],
    )
    manual = create_manual_knowledge_entry(
        entry_type=KnowledgeEntryType.CELEBRITY,
        name="IU",
        sample_fingerprints=["phash:manual"],
    )
    repo.save_knowledge_entry(manual)

    repo.deactivate_entries_for_source_decision("decision-1", "corrected")

    assert repo.knowledge_entry(automatic.id).active is False
    assert repo.knowledge_entry(manual.id).active is True
