from modules.core.offer_analytics import (
    avarten_offer_key,
    cyprus_project_key,
    investment_offer_key,
    tripinvest_offer_key,
)


def test_domestic_key_uses_only_positive_numeric_id():
    assert investment_offer_key(443) == "offer:v1:investment:443"
    assert investment_offer_key("7") == "offer:v1:investment:7"
    assert investment_offer_key(0) is None
    assert investment_offer_key("bad") is None


def test_tripinvest_key_is_stable_across_slug_and_query_changes():
    old = "https://tripinvest.pl/oferta/old-apartment-75156?campaign=x"
    new = "https://www.tripinvest.pl/oferta/new-apartment-75156/"
    assert tripinvest_offer_key(old) == "offer:v1:tripinvest:75156"
    assert tripinvest_offer_key(new) == "offer:v1:tripinvest:75156"
    assert tripinvest_offer_key("https://tripinvest.pl/oferta/no-reference") is None


def test_avarten_key_uses_canonical_post_id_not_title_or_url():
    offer = {
        "identity": {"offer_id": "26161", "slug": "changing-slug"},
        "source": {"external_url": "https://example.invalid/property/changing-slug/"},
    }
    assert avarten_offer_key(offer) == "offer:v1:avarten:26161"
    assert avarten_offer_key({"identity": {"offer_id": "https://example.invalid/property/x"}}) is None


def test_cyprus_project_key_matches_normalized_source_project_slug():
    assert cyprus_project_key("https://cyprus.example/en/Aloha_Homes/") == "project:v1:cyprus:aloha_homes"
    assert cyprus_project_key("https://cyprus.example/") is None
