from hockey_data.service import HockeyDataService


def penalty(side: str):
    return {
        "side": side,
        "infraction": {"id": "minor"},
        "preset": "2m",
        "durationMs": 120000,
        "remainingMs": 120000,
        "finished": False,
    }


def strength(settings, *, home=0, away=0, language="ru"):
    timer_state = {
        "penalty_board": {
            "penalties": [penalty("home") for _ in range(home)] + [penalty("away") for _ in range(away)]
        }
    }
    base = {
        "strength_regulation_skaters": 5,
        "strength_regular_overtime_skaters": 3,
        "strength_playoff_overtime_skaters": 5,
        "strength_min_skaters": 3,
    }
    base.update(settings)
    return HockeyDataService._strength_payload(
        base, stage="regular", current_period="1", timer_state=timer_state, language=language
    )


def test_home_and_away_labels_use_exact_configured_strength_caption():
    result = strength({
        "strength_state_labels": {
            "regulation": {"5x4": {"ru": "МОЯ ПОДПИСЬ 5x4", "en": "MY 5x4 CAPTION"}}
        }
    }, home=1)
    assert result["state_key"] == "5x4"
    assert result["state_label"] == "МОЯ ПОДПИСЬ 5x4"
    assert result["home_label"] == "МОЯ ПОДПИСЬ 5x4"
    assert result["away_label"] == "МОЯ ПОДПИСЬ 5x4"


def test_complex_strength_uses_configured_caption_without_numeric_regeneration():
    result = strength({
        "strength_state_labels": {
            "regulation": {"4x3": {"ru": "СТУДИЙНОЕ 4x3", "en": "STUDIO 4x3"}}
        }
    }, home=2, away=1)
    assert result["advantage_side"] == "away"
    assert result["state_label"] == "СТУДИЙНОЕ 4x3"
    assert result["home_label"] == "СТУДИЙНОЕ 4x3"
    assert result["away_label"] == "СТУДИЙНОЕ 4x3"


def test_explicit_empty_strength_caption_remains_empty_everywhere():
    result = strength({
        "strength_state_labels": {
            "regulation": {"5x4": {"ru": "", "en": ""}}
        }
    }, home=1)
    assert result["state_label"] == ""
    assert result["home_label"] == ""
    assert result["away_label"] == ""


def test_missing_raw_matrix_cell_uses_same_settings_default_matrix():
    result = strength({"strength_state_labels": {}}, home=1)
    assert result["state_key"] == "5x4"
    assert result["state_label"] == "PP"
    assert result["home_label"] == "PP"
    assert result["away_label"] == "PP"


def test_english_uses_english_caption_from_same_settings_cell():
    result = strength({
        "strength_state_labels": {
            "regulation": {"5x3": {"ru": "ПЯТЬ НА ТРИ", "en": "FIVE ON THREE"}}
        }
    }, home=2, language="en")
    assert result["state_label"] == "FIVE ON THREE"
    assert result["home_label"] == "FIVE ON THREE"
    assert result["away_label"] == "FIVE ON THREE"
