"""The brand fold key, ahead of migration #3's brands table.

Measured 2026-09-03: 52 launch-scale duplicate product groups, most of them
one brand spelled several ways across shops ("Moët & Chandon" at one airport,
"MOET CHANDON" at another). The fold must join those and nothing else.
"""

import pytest

from app.services.normalize import brand_key


class TestFolds:
    @pytest.mark.parametrize("spelling", ["Moët & Chandon", "MOET ET CHANDON", "moet-chandon", "Moet and Chandon", "Moët&Chandon"])
    def test_every_spelling_of_one_house_agrees(self, spelling):
        assert brand_key(spelling) == "moet chandon"

    @pytest.mark.parametrize(("spelling", "key"), [
        ("The Macallan", "macallan"),
        ("Hendrick's", "hendricks"),
    ])
    def test_a_leading_article_and_punctuation_still_fold(self, spelling, key):
        assert brand_key(spelling) == key

    @pytest.mark.parametrize(("spelling", "key"), [
        ("CÎROC™", "ciroc"),
        ("Crown Royal™", "crown royal"),
        ("Appleton Estate®", "appleton estate"),
        ("Roe & Co™", "roe co"),
        ("The Singleton™", "singleton"),
    ])
    def test_a_trademark_glyph_is_not_part_of_the_brand(self, spelling, key):
        """NFKD decomposes U+2122 to the LETTERS "TM", which survived the ASCII step and sat in
        the key, so `CIROC(tm)` was the brand row `ciroctm` and `Ciroc` was `ciroc`. Seven pairs
        of live brand rows in one dump were split that way (K11.3)."""
        assert brand_key(spelling) == key


class TestTheKeyNeverJoins:
    """Rian's ruling, 17 Sep: the programmatic stage may act only toward SEPARATE brands, and
    every join is the AI pass's to propose and a person's to make. The key used to pop trailing
    words off while they were in `_BRAND_TRAILERS`, which joined two brand rows with no decision
    behind it -- and, because the list holds ordinary words, mangled real names as well."""

    @pytest.mark.parametrize(("spelling", "key"), [
        ("Tanqueray Gin", "tanqueray gin"),
        ("Tanqueray Distillery", "tanqueray distillery"),
        ("Glenfiddich Whisky", "glenfiddich whisky"),
        ("Yves Saint Laurent Beauty", "yves saint laurent beauty"),
        ("Dior Parfums", "dior parfums"),
    ])
    def test_a_trailing_category_or_corporate_word_stays_in_the_key(self, spelling, key):
        assert brand_key(spelling) == key

    def test_the_two_spellings_are_separate_brand_rows_until_a_person_joins_them(self):
        assert brand_key("Tanqueray Gin") != brand_key("Tanqueray")

    @pytest.mark.parametrize(("spelling", "key"), [
        ("L'Oréal Paris", "loreal paris"),
        ("Souvenir de Paris", "souvenir de paris"),
        ("Au Vodka", "au vodka"),
        ("Cuba Vodka", "cuba vodka"),
        ("Kylie Cosmetics", "kylie cosmetics"),
    ])
    def test_the_fold_no_longer_eats_words_that_are_part_of_the_name(self, spelling, key):
        """Live brand rows in the 17 Sep dump, keyed by the old fold as `loreal`,
        `souvenir-de`, `au`, `cuba` and `kylie`: the list holds "paris", "vodka" and
        "cosmetics", which are ordinary words inside real brand names."""
        assert brand_key(spelling) == key


class TestDoesNotOverFold:
    def test_a_brand_that_is_a_category_word_survives(self):
        """"Gin Mare" is a brand; stripping from the end never touches its head."""
        assert brand_key("Gin Mare") == "gin mare"
        assert brand_key("Gin") == "gin"

    def test_spaces_still_separate_different_houses(self):
        assert brand_key("Glen Moray") != brand_key("Glenmorangie")
        assert brand_key("Glen Grant") != brand_key("Glengrant")

    def test_no_brand_is_empty_not_none(self):
        assert brand_key(None) == "" and brand_key("") == ""
