from services.rpg_facts import (
    merge_facts,
    parse_facts_list,
    facts_to_prompt,
    facts_list_to_json,
    dedupe_facts_fuzzy,
    facts_are_similar,
)


def test_legacy_string_facts():
    raw = '["Old fact", "Another"]'
    facts = parse_facts_list(raw)
    assert len(facts) == 2
    assert facts[0]["text"] == "Old fact"


def test_fuzzy_similar_near_duplicate():
    assert facts_are_similar(
        "Rin and Grigo found a magical glade with glowing flowers",
        "Rin and Grigo found magical glade glowing flowers",
    )


def test_dedupe_collapses_duplicates():
    raw = facts_list_to_json(
        [
            {"text": "Rin found a magical glade with glowing flowers", "rp_day": "day 1"},
            {"text": "Rin found magical glade glowing flowers", "rp_day": "day 1"},
            {"text": "Player name is Grigo", "rp_day": "day 1"},
        ]
    )
    out = dedupe_facts_fuzzy(parse_facts_list(raw))
    assert len(out) == 2


def test_merge_with_rp_day():
    existing = facts_list_to_json([{"text": "A", "rp_day": "день 1"}])
    merged = parse_facts_list(
        merge_facts(existing, [{"text": "B", "rp_day": ""}], rp_day_default="день 2")
    )
    assert merged[1]["rp_day"] == "день 2"
