from pathlib import Path


def test_team_state_settings_are_configurable_and_bilingual():
    config = Path("hockey_data/config.py").read_text(encoding="utf-8")
    router = Path("hockey_data/router.py").read_text(encoding="utf-8")
    admin = Path("hockey_data/static/admin-directories.js").read_text(encoding="utf-8")

    assert "DEFAULT_SCOREBOARD_TEAM_STATES" in config
    for key in (
        "home_delayed_penalty", "away_delayed_penalty",
        "home_empty_net", "away_empty_net",
    ):
        assert key in config
        assert key in admin
    assert "scoreboard_team_states: dict[str, Any] | None = None" in router
    assert "Русская надпись" in admin
    assert "English label" in admin
    assert "vMix Input" in admin
    assert "Overlay" in admin


def test_team_state_input_uses_operator_agent_inventory_and_stable_key():
    agent = Path("hockey_data/agent_bridge.py").read_text(encoding="utf-8")
    admin = Path("hockey_data/static/admin-directories.js").read_text(encoding="utf-8")

    assert "async def vmix_inventory_for_user" in agent
    assert '"/api/hockey/agents/vmix-inventory"' in agent
    assert 'const key = String(input.key || input.title || input.number || "").trim();' in admin
    assert 'localStorage.getItem("hockey.vmix.selected_device")' in admin
    assert 'input_title:' in admin


def test_scoreboard_sequence_syncs_active_team_state_overlays():
    js = Path("ui_builder/static/app.js").read_text(encoding="utf-8")
    manager = Path("ui_builder/manager.py").read_text(encoding="utf-8")

    assert "function hockeySyncTeamStateOverlays" in js
    assert "function hockeyTeamStateScoreboardIsLive" in js
    assert "hockeyTeamStateOverlayActive" in js
    assert "sync_hockey_team_states" in js
    assert "adding states" not in js  # keep labels localized, no accidental placeholder
    assert 'OverlayInput${setting.overlay}In' in js
    assert 'OverlayInput${previous.overlay}Out' in js
    assert '"sync_hockey_team_states": bool(sequence.get("sync_hockey_team_states", sequence.get("toggle_all_overlays_on_repeat", False)))' in manager


def test_team_state_buttons_use_configured_labels_and_persisted_match_flags():
    js = Path("ui_builder/static/app.js").read_text(encoding="utf-8")
    service = Path("hockey_data/service.py").read_text(encoding="utf-8")

    assert "const delayedSetting = hockeyTeamStateSetting(delayedKey);" in js
    assert "const emptyNetSetting = hockeyTeamStateSetting(emptyNetKey);" in js
    assert "data-hockey-team-flag" in js
    assert '"scoreboard_team_states": scoreboard_team_states' in service
    assert '"flags": self._runtime_flags_from_row(control)' in service
