"""A decided attribute carries its own kind (K8, from K4's find on the CHANEL rehearsal).

`product_key` read the decided row for the attribute's VALUE but took its KIND from the rule.
Where the rule reads a shade the shop wrote without naming a kind (CHANEL's
"ROUGE ALLURE 3.5gr / 196 A DEMI-MOT" at Panama), the batch tail's rekey then wrote the variant's
attributes with no `attribute_kind`, the column the page reads went empty, and `decisions verify`
reported drift on every approved shade: one individual approval, 1 checked, 1 drift. The kind is
the decided row's own field suffix now. The SQLite kit; no network.
"""

from __future__ import annotations

from app.models import Brand, ProductVariant
from app.services import keying
from app.services.decisions import writer
from tests.test_decided import RIAN, db  # noqa: F401  (the SQLite kit)


class TestADecidedAttributeKeepsItsKind:
    def test_the_kind_comes_from_the_decided_field_not_the_rule(self, db):  # noqa: F811
        chanel = Brand(slug="chanel", name="CHANEL")
        db.add(chanel)
        db.flush()
        # The rule reads this name's tail as an attribute but names no kind for it.
        variant = ProductVariant(name="ROUGE ALLURE 3.5gr / 196 A DEMI-MOT", brand="CHANEL", brand_id=chanel.id,
                                 vertical="beauty", match_key="pending", attributes={},
                                 quantity_value=3.5, quantity_unit="g", quantity_state="stated", form="single")
        db.add(variant)
        db.commit()
        keying.invalidate()
        rule_kind = keying.load_maps(db).attribute_kind(variant.name, variant.vertical, category=variant.category)
        assert not rule_kind, "the rule names no kind here; that is what made the drift"

        with writer.batch(db, "route", "individual", RIAN, commit=False, tail=False) as b:
            writer.record(b, "product_variant", variant, "attribute:color", "196 A Demi-Mot",
                          reason="the shop's shade, read by a person")
        db.commit()
        keying.invalidate()

        _, attributes = keying.product_key(variant, keying.load_maps(db))
        assert attributes["attribute"].lower() == "196 a demi-mot", "the decided wording is what a page reads"
        assert attributes["attribute_kind"] == "color", "the decided row's field suffix is the kind"

    def test_the_rules_kind_still_answers_when_nothing_was_decided(self, db):  # noqa: F811
        """The rule keeps naming the kind for every variant a person has not touched."""
        chanel = Brand(slug="chanel", name="CHANEL")
        db.add(chanel)
        db.flush()
        variant = ProductVariant(name="ROUGE ALLURE 3.5gr / 196 A DEMI-MOT", brand="CHANEL", brand_id=chanel.id,
                                 vertical="beauty", match_key="pending", attributes={},
                                 quantity_value=3.5, quantity_unit="g", quantity_state="stated", form="single")
        db.add(variant)
        db.commit()
        keying.invalidate()
        _, attributes = keying.product_key(variant, keying.load_maps(db))
        assert "attribute_kind" not in attributes or attributes["attribute_kind"], "no kind invented"
