"""Pinning the Heinemann platform's `contentUnit` code table.

Both `heinemann.py` (the global catalogue) and `heinemann_platform.py` (the shared-platform
shops: Iceland, Sydney, BorderShop) read the same `contentUnit.unit.code` shape, but the
only code either module's own fixtures attest is "L" -- staging holds zero Heinemann
fragments to check the rest against. The other codes ("MLT", "CLT", "GRM", "KGM", "PCE" and
their siblings) come from the platform's own published code list rather than a captured
page, so this file is what pins the table: a change to it should be a deliberate edit here,
not a silent drift.
"""

import pytest

from app.services.collectors import heinemann, heinemann_platform

MODULES = (heinemann, heinemann_platform)

# (code, quantity, expected unit, expected canonical value)
CASES = [
    ("MLT", 500, "ml", 500),
    ("ML", 500, "ml", 500),
    ("CLT", 70, "ml", 700),
    ("LTR", 0.7, "ml", 700),
    ("L", 1, "ml", 1000),
    ("GRM", 250, "g", 250),
    ("G", 250, "g", 250),
    ("KGM", 1.5, "g", 1500),
    ("KG", 2, "g", 2000),
    ("PCE", 3, "pcs", 3),
    ("PC", 4, "pcs", 4),
    ("ST", 5, "pcs", 5),
    ("EA", 6, "pcs", 6),
]


@pytest.mark.parametrize("module", MODULES)
class TestContentUnitCodeTable:
    @pytest.mark.parametrize("code,quantity,unit,value", CASES)
    def test_a_known_code_converts_to_its_canonical_unit(self, module, code, quantity, unit, value):
        item = {"contentUnit": {"quantity": quantity, "unit": {"code": code}}}
        q, text = module._quantity_from_content_unit(item)
        assert q.state == "stated"
        assert q.unit == unit
        assert q.value == value
        assert text == f"{quantity:g} {code}"

    def test_an_unknown_code_is_reported_unparsed_not_dropped(self, module):
        item = {"contentUnit": {"quantity": 5, "unit": {"code": "XYZ"}}}
        q, text = module._quantity_from_content_unit(item)
        assert q.state == "unparsed"
        assert q.detail == "contentUnit:XYZ"
        assert text == "5 XYZ"

    def test_a_missing_content_unit_is_no_quantity_and_no_text(self, module):
        assert module._quantity_from_content_unit({}) == (None, None)
        assert module._quantity_from_content_unit({"contentUnit": {}}) == (None, None)
        assert module._quantity_from_content_unit(
            {"contentUnit": {"quantity": 0, "unit": {"code": "LTR"}}}
        ) == (None, None)

    def test_size_from_content_unit_is_unchanged(self, module):
        """`_size_from_content_unit` keeps computing the derived millilitre figure on its
        own volume-only table; a mass code is simply not a size."""
        assert module._size_from_content_unit({"contentUnit": {"quantity": 0.7, "unit": {"code": "LTR"}}}) == 700
        assert module._size_from_content_unit({"contentUnit": {"quantity": 500, "unit": {"code": "GRM"}}}) is None
