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

Note (documented, not fixed): standard_pagination_from_meta()'s
`total_pages` fallback defaults to `fallback_page` (the *current* page
number) rather than to the actual meta total, e.g. `total_pages` or `1`.
This is almost certainly an existing bug/inconsistency; see the recon
notes handed back to the user. The test below documents the current
(possibly unintended) behaviour rather than the "correct" one.
"""
import pytest

from InvestmentWebsite.offers.response_contracts import (
    CONTRACT_CANONICAL_V1,
    adapt_catalog_items_for_contract,
    adapt_offer_for_contract,
    build_catalog_payload_for_contract,
    standard_pagination_from_meta,
)


def test_contract_constant_value():
    assert CONTRACT_CANONICAL_V1 == "canonical_v1"


def test_adapt_catalog_items_for_contract_passes_through_unchanged():
    items = [{"a": 1}, {"b": 2}]
    result = adapt_catalog_items_for_contract(items, CONTRACT_CANONICAL_V1)
    assert result is items


def test_adapt_catalog_items_for_contract_rejects_unknown_contract():
    with pytest.raises(ValueError, match="Unsupported response contract: v2"):
        adapt_catalog_items_for_contract([], "v2")


def test_adapt_offer_for_contract_passes_through_unchanged():
    offer = {"id": "42"}
    result = adapt_offer_for_contract(offer, CONTRACT_CANONICAL_V1)
    assert result is offer


def test_adapt_offer_for_contract_rejects_unknown_contract():
    with pytest.raises(ValueError, match="Unsupported response contract: legacy"):
        adapt_offer_for_contract({}, "legacy")


def test_build_catalog_payload_minimal_without_pagination_or_extra():
    payload = build_catalog_payload_for_contract(
        items=[{"id": 1}],
        meta={},
        page=1,
        contract=CONTRACT_CANONICAL_V1,
    )
    assert payload == {"offers": [{"id": 1}]}


def test_build_catalog_payload_with_pagination_builder():
    payload = build_catalog_payload_for_contract(
        items=[{"id": 1}],
        meta={"current_page": 2, "total_pages": 5},
        page=2,
        contract=CONTRACT_CANONICAL_V1,
        pagination_builder=standard_pagination_from_meta,
    )
    assert payload == {
        "offers": [{"id": 1}],
        "pagination": {"current_page": 2, "total_pages": 5},
    }


def test_build_catalog_payload_with_extra_payload_merged_in():
    payload = build_catalog_payload_for_contract(
        items=[],
        meta={},
        page=1,
        contract=CONTRACT_CANONICAL_V1,
        extra_payload={"filters_applied": {"kraj": "Polska"}},
    )
    assert payload == {"offers": [], "filters_applied": {"kraj": "Polska"}}


def test_build_catalog_payload_rejects_unknown_contract():
    with pytest.raises(ValueError, match="Unsupported response contract"):
        build_catalog_payload_for_contract(items=[], meta={}, page=1, contract="bogus")


def test_standard_pagination_from_meta_uses_meta_values_when_present():
    pagination = standard_pagination_from_meta({"current_page": 3, "total_pages": 9}, fallback_page=1)
    assert pagination == {"current_page": 3, "total_pages": 9}


def test_standard_pagination_from_meta_falls_back_to_fallback_page_for_both_fields():
    # Documents current behaviour: when meta is empty, total_pages falls
    # back to fallback_page (the current page), not to a "real" total.
    pagination = standard_pagination_from_meta({}, fallback_page=4)
    assert pagination == {"current_page": 4, "total_pages": 4}
