"""The slug hook at confirm (Stream K3.6, moved by K8; plan W18): the moment an alias is recorded
is the moment the page's final slug is fixed from the chosen name and the old address forwards.

The hook first sat in `merges.apply_line_alias`, which only tests call: the desk's Confirm same,
the sheet's absorb and replay all record `alias_of` through `writer.record`, so on production no
confirmed alias would have fixed a slug (K6 found it; the running list's
`issue-the-slug-hook-is-on-a-path-nothing-calls`). It lives in the `(product_line, alias_of)`
applier now, which every one of those paths shares. The SQLite kit; no network.
"""

from __future__ import annotations

from sqlalchemy import select

from app.models import Brand, ProductLine, Suggestion
from app.models.decisions import Redirect
from app.services import merge_session
from tests.test_decided import RIAN, db  # noqa: F401  (the SQLite kit)


class TestTheSlugHook:
    def test_the_desks_confirm_same_on_two_lines_writes_a_redirect(self, db):  # noqa: F811
        """The path a person actually uses: Confirm same on a line pair fixes the surviving
        line's slug from its name and leaves its old address forwarding."""
        rabanne = Brand(slug="rabanne", name="Rabanne")
        db.add(rabanne)
        db.flush()
        # The survivor's address is stale: its name says 1 Million, its slug still says one-million.
        survivor = ProductLine(brand_id=rabanne.id, key="1 million", slug="rabanne-one-million", name="1 Million")
        alias = ProductLine(brand_id=rabanne.id, key="one million", slug="rabanne-one-million-2", name="One Million")
        db.add_all([survivor, alias])
        db.flush()
        pair = Suggestion(level="line", left_id=min(survivor.id, alias.id), right_id=max(survivor.id, alias.id),
                          reason="name_within", score=0.9)
        db.add(pair)
        db.flush()

        merge_session.confirm(db, pair.id, decided_by=RIAN, keep=str(survivor.id), commit=False)

        assert survivor.slug == "rabanne-1-million", "the survivor's address follows its name"
        row = db.scalar(select(Redirect).where(Redirect.from_slug == "rabanne-one-million"))
        assert row is not None and row.kind == "product_line" and row.to_slug == "rabanne-1-million"
        assert row.decision_id is not None, "the redirect carries the decision that caused it"

    def test_a_typed_name_at_confirm_is_the_address(self, db):  # noqa: F811
        """`detail.preferred_name` beats the row's current name, so the address is the one the
        person chose in the same act."""
        brand = Brand(slug="macallan", name="Macallan")
        db.add(brand)
        db.flush()
        survivor = ProductLine(brand_id=brand.id, key="12", slug="macallan-12", name="12")
        alias = ProductLine(brand_id=brand.id, key="12 years old", slug="macallan-12-years-old", name="12 Years Old")
        db.add_all([survivor, alias])
        db.flush()
        pair = Suggestion(level="line", left_id=min(survivor.id, alias.id), right_id=max(survivor.id, alias.id),
                          reason="name_within", score=0.9)
        db.add(pair)
        db.flush()

        merge_session.confirm(db, pair.id, decided_by=RIAN, keep=str(survivor.id),
                              preferred_name="Double Cask 12", commit=False)

        assert survivor.slug == "macallan-double-cask-12"
        assert db.scalar(select(Redirect).where(Redirect.from_slug == "macallan-12")) is not None
