from __future__ import annotations

import json
from pathlib import Path

from ui_builder.manager import UIBuilderManager


def test_hockey_legacy_space_ctrl_r_component_shortcuts_are_removed_and_persisted(tmp_path: Path) -> None:
    manager = UIBuilderManager(tmp_path, filename="ui.json")
    payload = {
        "version": 21,
        "project_name": "Hockey",
        "data_source": "hockey",
        "canvas": {"width": 1440, "height": 900},
        "tabs": [{"id": "main", "label": "Игра"}],
        "components": [
            {
                "id": "timer",
                "type": "hockey_game_control",
                "title": "Игра",
                "action_id": "hockey_game_timer",
                "shortcuts": [
                    {"id": "old-space", "enabled": True, "combo": "Space", "event": "timer_toggle"},
                    {"id": "old-reset", "enabled": True, "combo": "Ctrl+R", "event": "timer_reset"},
                    {"id": "keep-f8", "enabled": True, "combo": "F8", "event": "click"},
                ],
            }
        ],
        "triggers": [],
        "shortcut_sequences": [
            {
                "id": "visible-space-sequence",
                "name": "Таймеры",
                "enabled": True,
                "combo": "Space",
                "steps": [],
            }
        ],
    }
    manager.settings_file.write_text(json.dumps(payload, ensure_ascii=False), encoding="utf-8")

    loaded = manager.load()

    assert [item["combo"] for item in loaded["components"][0]["shortcuts"]] == ["F8"]
    assert loaded["shortcut_sequences"][0]["combo"] == "Space"

    persisted = json.loads(manager.settings_file.read_text(encoding="utf-8"))
    assert [item["combo"] for item in persisted["components"][0]["shortcuts"]] == ["F8"]
    assert persisted["shortcut_sequences"][0]["combo"] == "Space"


def test_hockey_templates_do_not_create_hidden_space_ctrl_r_bindings() -> None:
    app_js = Path("ui_builder/static/app.js").read_text(encoding="utf-8")

    assert 'id: "hockey-dashboard-space"' not in app_js
    assert 'id: "hockey-dashboard-reset"' not in app_js
    assert 'id: "hockey-game-space"' not in app_js
    assert 'id: "hockey-game-reset"' not in app_js
    assert "stripLegacyHockeyComponentShortcuts" in app_js
    # The visible Shortcut Sequence preset is intentionally still available.
    assert 'name: "Старт игрового времени"' in app_js
    assert 'combo: "Space"' in app_js
