"""Topic bank integrity — the verbatim facilitation record.

The bank lives in the notebook's ``topic-bank`` cell (content is edited in
Jupyter, never in ``.py``); the ``topics`` fixture execs that cell, so
these pins guard the exact content the deliverable ships. Keys are stable
identities that captured notes and the JSON export key off, so renaming or
reordering a topic must be a deliberate, test-breaking act — never a
silent drift.
"""

from discoverylib import Topic, agenda_minutes


# ── Hand-checked shape (recount if you add/remove content) ───────────
def test_topic_count_and_order(topics):
    assert len(topics) == 8
    assert tuple(t.key for t in topics) == (
        "background",
        "cx_strategy",
        "channels",
        "agent_environment",
        "routing_automation",
        "workforce_engagement",
        "training",
        "reporting_insights",
    )


def test_subtopic_and_prompt_totals(topics):
    assert sum(len(t.subtopics) for t in topics) == 26
    assert sum(t.prompt_count for t in topics) == 95


def test_agenda_minutes_sum(topics):
    assert agenda_minutes(topics) == 110
    assert agenda_minutes(topics) == sum(t.minutes for t in topics)


def test_bank_uses_library_schema(topics):
    # The cell must build on discoverylib's dataclasses, not ad-hoc types,
    # or the engine's TopicState/export contracts silently stop applying.
    assert all(isinstance(t, Topic) for t in topics)


# ── Structural invariants — hold for every topic ─────────────────────
def test_keys_unique_and_wellformed(topics):
    keys = [t.key for t in topics]
    assert len(keys) == len(set(keys))                       # unique
    for t in topics:
        assert t.key.replace("_", "").isalnum()              # snake_case slug
        assert t.title and t.scope                           # client-facing text present
        assert t.minutes > 0
        assert t.subtopics                                   # no empty topic
        sub_keys = [st.key for st in t.subtopics]
        assert len(sub_keys) == len(set(sub_keys))           # unique within topic
        for st in t.subtopics:
            assert st.prompts                                # no empty sub-topic


def test_lookup(topics, topic_by_key):
    assert topic_by_key["channels"].title == "Channels"
    assert topic_by_key["training"].minutes == 5
    assert topics[0].key == "background"


def test_no_double_dollar_or_raw_markup_in_scope(topics):
    # scope lines render on stage HTML — keep them plain text.
    for t in topics:
        assert "<" not in t.scope and ">" not in t.scope
