"""The cards say what the answer does (Stream K12.6; the 18 Sep simulation's findings).

What it cost: the brand fold card never said what its DIRECTION does (which way round keeps how
many product line addresses), presented "which row survives" and "what it is called" as one
choice, and the product variant merge card, the most valuable change on the page, said only
"1 product variants move". And the brand index had no way to put a brand with a new kind of
question first. SQLite kit, no network."""
from __future__ import annotations

from sqlalchemy import select

from app.models import Brand, ProductLine, ProductVariant, Proposal
from app.services import proposals, review_detail
from tests.test_precedents import _v6
from tests.test_proposals_load import _file, _key, db  # noqa: F401  (the fixture)


def test_a_brand_fold_card_counts_both_directions_and_says_the_name_is_a_second_decision(db):
    paris = Brand(id=2, slug="chanel-paris", name="CHANEL Paris")
    db.add(paris); db.flush()
    db.add(ProductLine(id=9, brand_id=2, key="rouge allure", slug="chanel-paris-rouge-allure", name="Rouge Allure"))
    db.add(ProductLine(id=10, brand_id=2, key="coco", slug="chanel-paris-coco", name="Coco"))
    db.flush()
    db.add(ProductVariant(id=9, name="Coco 50ml", brand="CHANEL Paris", brand_id=2, vertical="beauty", match_key="k9", product_line_id=10,
                          quantity_value=50, quantity_unit="ml", quantity_state="stated", form="single", attributes={}))
    db.commit()
    data = _file(db)
    data["proposals"] = [{"entity": {"type": "suggestion", "key": "pair:brand:brand:chanel-paris||brand:chanel", "detail": {"level": "brand"}},
                          "field": "decision", "value": {"decision": "same", "survivor": "brand:chanel", "name": "CHANEL", "note": None},
                          "confidence": 0.9, "reason": "one house, two spellings",
                          "evidence": [{"listing": _key(db, __import__("app.models", fromlist=["Listing"]).Listing, 1), "source": "listed_name", "span": [0, 12], "text": "Rouge Allure"}]}]
    proposals.load(db, data)
    row = db.scalar(select(Proposal).where(Proposal.entity_type == "suggestion"))
    out = review_detail.read(db, str(row.uid))
    directions = out["consequences"]["directions"]
    assert len(directions) == 2
    chosen = next(d for d in directions if d["chosen"])
    other = next(d for d in directions if not d["chosen"])
    assert chosen["survivor"] == "brand:chanel" and chosen["variants_move"] == 1 and chosen["lines_fold"] == 1 and chosen["lines_arrive"] == 1
    assert chosen["addresses_kept"] == 3 and other["addresses_kept"] == 2 and other["variants_move"] == 4
    assert "second decision" in out["consequences"]["name_note"] and "/brands/chanel/" in out["consequences"]["name_note"]


def test_a_product_variant_merge_card_says_which_retailers_it_puts_on_one_page(db):
    data = _file(db)
    proposals.load(db, data)
    pair = db.scalar(select(Proposal).where(Proposal.entity_type == "suggestion"))
    out = review_detail.read(db, str(pair.uid))
    assert out["consequences"]["joins_retailers"] == ["attenza", "avolta"]
    assert "attenza, avolta" in out["consequences"]["if_confirmed"][0]["what"] and "did not exist" in out["consequences"]["if_confirmed"][0]["what"]


def test_the_brand_index_counts_new_kinds_and_puts_them_first(db):
    db.add(Brand(id=2, slug="zz-quiet", name="Quiet")); db.commit()
    data = _v6(db)
    proposals.load(db, data)
    index = proposals.brand_index(db, has_suggestions=False)
    assert index["rows"][0]["brand_slug"] == "chanel" and index["rows"][0]["new_kinds"] == 4
    assert next(r for r in index["rows"] if r["brand_slug"] == "zz-quiet")["new_kinds"] == 0
