"""The final slug at confirm, through the alias appliers K3 hooked (Stream K6.3; plan W18). What it
cost before: a product line kept the address its first rule key gave it whatever a person named
it, and a second rename would have left the first address two hops from the page. The SQLite
kit of the decided suite; no network."""
from __future__ import annotations

from sqlalchemy import select

from app.models import Brand, ProductLine, Redirect
from app.services import catalog_queries, merges, publish
from tests.test_decided import RIAN, db  # noqa: F401  (the SQLite kit)


def _lines(db):  # noqa: F811
    brand = Brand(slug="rabanne", name="Rabanne")
    db.add(brand)
    db.flush()
    rows = [ProductLine(brand_id=brand.id, key=k, slug=f"rabanne-{k.replace(' ', '-')}", name=k.title()) for k in ("1 million", "one million", "million")]
    db.add_all(rows)
    db.commit()
    return brand, rows


def test_a_confirmed_alias_fixes_the_slug_and_a_second_flattens_the_first(db):  # noqa: F811
    _, (a, b, c) = _lines(db)
    merges.apply_line_alias(db, b, a, decided_by=RIAN, preferred_name="1 Million Eau de Toilette")
    assert a.slug == "rabanne-1-million-eau-de-toilette"
    merges.apply_line_alias(db, c, a, decided_by=RIAN, preferred_name="One Million")
    assert a.slug == "rabanne-one-million-2", "the alias row still holds rabanne-one-million, so the page does not take it"
    rows = {r.from_slug: r.to_slug for r in db.scalars(select(Redirect).where(Redirect.kind == "product_line"))}
    assert rows == {"rabanne-1-million": a.slug, "rabanne-1-million-eau-de-toilette": a.slug}, "two hops stored as one each"
    for old in ("rabanne-1-million", "rabanne-1-million-eau-de-toilette", "rabanne-one-million", "rabanne-million"):
        assert catalog_queries.line_by_slug(db, old) == (None, a.slug), old
    assert catalog_queries.line_by_slug(db, a.slug)[0].id == a.id


def test_with_no_name_chosen_the_slug_follows_the_name_the_line_already_has(db):  # noqa: F811
    _, (a, b, _c) = _lines(db)
    merges.apply_line_alias(db, b, a, decided_by=RIAN)
    assert a.slug == "rabanne-1-million" and db.scalar(select(Redirect)) is None, "same words, nothing moved"


def test_an_indexed_line_keeps_the_address_an_engine_holds(db):  # noqa: F811
    _, (a, b, _c) = _lines(db)
    a.indexed = True
    db.commit()
    merges.apply_line_alias(db, b, a, decided_by=RIAN, preferred_name="Renamed")
    assert a.slug == "rabanne-1-million" and publish.redirect_for("rabanne-1-million", "product_line", db) is None


def test_a_brand_alias_keeps_the_brands_slug_and_forwards_the_alias(db):  # noqa: F811
    brand, _ = _lines(db)
    paco = Brand(slug="paco-rabanne", name="Paco Rabanne")
    db.add(paco)
    db.commit()
    merges.apply_brand_alias(db, paco, brand, decided_by=RIAN, preferred_name="Paco Rabanne")
    assert brand.slug == "rabanne", "the fold key ingest finds the row by"
    assert publish.redirect_for("paco-rabanne", "brand", db) == "rabanne" and catalog_queries.brand_by_slug(db, "paco-rabanne").id == brand.id
