"""REVIEW-PROCESS.md section 2 records consensus; it does not prescribe blanket defaults (K11.1).

The real cost this pins. Version 3 wrote section 2 as a per-vertical default table decided in
advance, and paired option B (certain-only keys) with option D (the word lists kept as proposals)
when W14 offered them as alternatives. The result was 938 open proposals, every one of them from a
`rule:*:6` pass, sitting in rian's queue before the AI pass had ever run -- he spent two days
answering the output of the stage his own ruling says he never sees. Measured on a copy of
`dfp-2026-09-17-post-chain.dump`: 938 open proposals, 13 passes, all `kind=rule, generator=rule`.

So the document's shape is a gate now: section 2 says what was decided and from which case, the
set/coffret question is raised per case rather than answered in advance, and the two sections rian
asked for -- brands, and the display-name policy -- exist.
"""

from __future__ import annotations

import re
from pathlib import Path

from app.services import proposal_rules

DOC = (Path(__file__).resolve().parents[1] / "docs" / "REVIEW-PROCESS.md").read_text()
SECTION_2 = DOC[DOC.index("## 2. Grouping defaults"): DOC.index("## 3. What a proposal must carry")]


def flat(text: str) -> str:
    """One space between words, so a sentence that rewraps across lines still reads the same."""
    return " ".join(text.split())


FLAT_2 = flat(SECTION_2)


def test_the_document_version_and_the_generators_agree():
    assert re.search(r"\*\*Version (\d+),", DOC).group(1) == "6" == proposal_rules.PROCESS_VERSION


def test_section_two_says_what_was_decided_and_from_which_case():
    header = SECTION_2[SECTION_2.index("| Vertical and case"):].splitlines()[0]
    for column in ("What was decided", "From which case", "Still open"):
        assert column in header, header
    assert "a record, not a rule set" in SECTION_2


def test_the_set_and_coffret_question_is_raised_per_case_not_answered_in_advance():
    """Version 3's blanket default ('never a member of the bottle's line') is withdrawn."""
    row = next(line for line in SECTION_2.splitlines() if line.startswith("| **A set, coffret"))
    decided, from_which_case = row.split("|")[2], row.split("|")[3]
    assert "nothing is decided" in decided and "raises it per case" in decided
    # The withdrawn default survives only as provenance, never as the decision.
    assert "never a member of the bottle's line" not in decided
    assert "withdrew it as a blanket default" in from_which_case
    # Rian's two distinctions are written out as what the pass must weigh.
    assert "A refill serving several product lines is not the same case" in FLAT_2
    assert "A gift set spanning several product lines is not the same case" in FLAT_2


def test_the_format_words_rian_ruled_on_stay_on_the_list():
    """Recognised as a possible attribute, never folded into the bottle's line by a rule."""
    assert {"set", "gift", "duo", "refill"} <= proposal_rules.FORMAT_WORDS
    assert "**`FORMAT_WORDS` keeps set, gift, duo and refill**" in FLAT_2


def test_the_brand_section_and_the_display_name_policy_exist():
    assert "### 2.2 Brands: when two brand rows are the same house" in SECTION_2
    assert "### 2.3 The display name of a brand" in SECTION_2
    # The rename no rule can find, and the overlap that is only ever a candidate.
    assert "Paco Rabanne became Rabanne" in FLAT_2
    assert "containment overlap is a candidate, not a conclusion" in FLAT_2
    # Never the most common spelling; never overwriting a person.
    assert "is what the brand itself would use" in FLAT_2
    assert "against_decision_id" in FLAT_2


def test_the_word_lists_are_input_to_a_pass_not_rows_in_a_persons_queue():
    section_1 = flat(DOC[DOC.index("## 1. What a rule may do"): DOC.index("## 2. Grouping defaults")])
    assert "never a row in rian's queue" in section_1
    assert "938" in section_1, "the cost of pairing option B with option D is recorded"
