"""Characterisation tests for modules/offers/validator.py.

These tests document the current behaviour of validate_canonical_offer()
as observed in the code, not necessarily the "ideal" behaviour. See the
recon notes handed back to the user for a discussion of borderline cases.
"""
from InvestmentWebsite.offers.validator import (
    ALLOWED_CURRENCIES,
    ALLOWED_SOURCES,
    validate_canonical_offer,
)


def _valid_offer(**overrides):
    offer = {
        "source": {"platform": "investment"},
        "identity": {"offer_id": "123"},
        "listing": {"title": "Mieszkanie w centrum"},
        "pricing": {"currency": "PLN"},
        "media": {"gallery": []},
    }
    offer.update(overrides)
    return offer


def test_allowed_sources_matches_registered_sources():
    assert ALLOWED_SOURCES == {"investment", "tripinvest", "avarten", "lanzarote"}


def test_allowed_currencies():
    assert ALLOWED_CURRENCIES == {"PLN", "EUR", "AED", "unknown"}


def test_valid_offer_passes_with_no_errors():
    ok, errors = validate_canonical_offer(_valid_offer())
    assert ok is True
    assert errors == []


def test_empty_gallery_list_is_valid():
    ok, errors = validate_canonical_offer(_valid_offer(media={"gallery": []}))
    assert ok is True
    assert errors == []


def test_unknown_currency_is_allowed():
    ok, errors = validate_canonical_offer(_valid_offer(pricing={"currency": "unknown"}))
    assert ok is True
    assert errors == []


def test_completely_empty_offer_reports_all_five_errors():
    ok, errors = validate_canonical_offer({})
    assert ok is False
    assert set(errors) == {
        "source.platform",
        "identity.offer_id",
        "listing.title",
        "pricing.currency",
        "media.gallery",
    }


def test_unknown_source_platform_is_rejected():
    ok, errors = validate_canonical_offer(_valid_offer(source={"platform": "not_a_real_source"}))
    assert ok is False
    assert errors == ["source.platform"]


def test_blank_offer_id_is_rejected():
    ok, errors = validate_canonical_offer(_valid_offer(identity={"offer_id": "   "}))
    assert ok is False
    assert errors == ["identity.offer_id"]


def test_blank_title_is_rejected():
    ok, errors = validate_canonical_offer(_valid_offer(listing={"title": "   "}))
    assert ok is False
    assert errors == ["listing.title"]


def test_unsupported_currency_is_rejected():
    ok, errors = validate_canonical_offer(_valid_offer(pricing={"currency": "USD"}))
    assert ok is False
    assert errors == ["pricing.currency"]


def test_gallery_as_none_is_rejected():
    ok, errors = validate_canonical_offer(_valid_offer(media={"gallery": None}))
    assert ok is False
    assert errors == ["media.gallery"]


def test_gallery_as_non_list_is_rejected():
    ok, errors = validate_canonical_offer(_valid_offer(media={"gallery": "not-a-list"}))
    assert ok is False
    assert errors == ["media.gallery"]


def test_missing_nested_sections_are_treated_as_missing_values():
    # No KeyError even when whole sub-dicts are absent: the implementation
    # uses `.get(...) or {}` fallbacks throughout.
    ok, errors = validate_canonical_offer({"source": {"platform": "investment"}})
    assert ok is False
    assert set(errors) == {"identity.offer_id", "listing.title", "pricing.currency", "media.gallery"}
