"""The opening says WHAT KIND of competition this is (`commentary_banks.race_format_phrase`).

`meta.raceType` reached the feed from the event definition and was read by nothing at all — not the
picture, not the commentary. The only statement of the format lived in the venue-LESS welcome ("a
two-on-two team race, {a} against {b}"), and a race whose position resolves takes `welcome_city`
instead, which says "the team racing". Since a position resolves for every real race, the one line a
viewer joining cold most needs was the one nobody ever heard.

The format is DATA, like every other event fact: the renderer and the commentary name what the event
declared, and an undeclared or unknown format is not guessed.
"""
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "scoring"))

from commentary_banks import (  # noqa: E402
    INTRO,
    LANGS,
    RACE_FORMAT,
    RACE_FORMAT_UNKNOWN,
    race_format_phrase,
)


def test_the_declared_format_is_named():
    got = race_format_phrase("team_race_2")
    assert got["en"] == "two-on-two team racing"
    assert got["sv"] == "två-mot-två lagkappsegling"


def test_an_undeclared_format_is_not_invented():
    """A format nobody declared is one the broadcast must not name — the same rule that keeps a team
    with no `fullName` on its bare code."""
    assert race_format_phrase(None) == RACE_FORMAT_UNKNOWN
    assert race_format_phrase("") == RACE_FORMAT_UNKNOWN
    assert race_format_phrase("kite_slalom") == RACE_FORMAT_UNKNOWN
    assert RACE_FORMAT_UNKNOWN["en"] == "the racing"


def test_every_known_format_is_translated_into_every_language():
    """A missing language would render the opening in English inside a Swedish broadcast."""
    for key, phrases in RACE_FORMAT.items():
        for lng in LANGS:
            assert phrases.get(lng), f"{key} has no {lng} phrase"
    for lng in LANGS:
        assert RACE_FORMAT_UNKNOWN.get(lng)


def test_the_welcome_reads_naturally_in_every_language():
    """The article lives in the PHRASE, not the template, so "two-on-two team racing" and "the racing"
    both follow "Welcome to" without one gaining a stray "the"."""
    for lng in LANGS:
        line = INTRO[lng]["welcome_city_format"]
        assert "{format}" in line and "{place}" in line
        for key in (*RACE_FORMAT, None):
            said = line.format(format=race_format_phrase(key)[lng], place="X")
            assert "  " not in said, f"double space in {said!r}"
            if lng == "en":
                assert " the the " not in said and not said.startswith("Welcome to the the")


def test_no_event_or_team_name_is_baked_into_the_format_bank():
    """The no-hardcoded-names rule. A format is a property of the COMPETITION FORM, not of whoever is
    running it — TRWC and a club Saturday sailing the same format get the same words."""
    blob = " ".join(f"{k} {v}" for k, v in RACE_FORMAT.items()) + str(RACE_FORMAT_UNKNOWN)
    for banned in ("TRWC", "Team Racing World", "GSYS", "Stockholm", "Riddarfj"):
        assert banned.lower() not in blob.lower()
