from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
JS = (ROOT / "ui_builder/static/app.js").read_text(encoding="utf-8")
MAPPING = (ROOT / "hockey_data/mapping_context.py").read_text(encoding="utf-8")
SERVICE = (ROOT / "hockey_data/service.py").read_text(encoding="utf-8")
CSS = (ROOT / "ui_builder/static/styles.css").read_text(encoding="utf-8")


def test_player_payload_exposes_database_id_without_replacing_external_id():
    assert '"id": player.external_id' in SERVICE
    assert '"external_id": player.external_id' in SERVICE
    assert '"db_id": player.id' in SERVICE


def test_mapping_has_independent_home_and_away_penalty_identifiers():
    for key in (
        "selected_home_penalty_id",
        "selected_home_penalty_player_id",
        "selected_home_penalty_player_db_id",
        "selected_home_penalty_team_penalty",
        "selected_away_penalty_id",
        "selected_away_penalty_player_id",
        "selected_away_penalty_player_db_id",
        "selected_away_penalty_team_penalty",
        "selected_penalty_player_db_id",
        "selected_penalty_team_penalty",
    ):
        assert f'"key": "{key}"' in MAPPING


def test_penalty_preview_writes_side_specific_context_and_team_flag():
    for key in (
        "selected_home_penalty_id",
        "selected_home_penalty_player_db_id",
        "selected_home_penalty_team_penalty",
        "selected_away_penalty_id",
        "selected_away_penalty_player_db_id",
        "selected_away_penalty_team_penalty",
    ):
        assert f"{key}:" in JS
    assert 'selected_penalty_team_penalty: detail.team_penalty ? "1" : "0"' in JS
    assert 'selected_penalty_player_db_id: detail.player_db_id' in JS


def test_preview_selection_is_kept_per_side_and_visible():
    assert 'selectedPreviewEventIds: { home: "", away: "" }' in JS
    assert 'board.selectedPreviewEventIds[side] = event.id' in JS
    assert 'hockeyPenaltyIsPreviewSelected(board, event)' in JS
    assert 'selected_home_penalty_player_db_id' in JS
    assert 'selected_away_penalty_player_db_id' in JS
    assert 'selected_home_penalty_team_penalty' in JS
    assert 'selected_away_penalty_team_penalty' in JS
    assert '.hpd-team-card-identifiers' in CSS
