from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
RUNTIME = (ROOT / "ui_builder" / "static" / "runtime.html").read_text(encoding="utf-8")
EDITOR = (ROOT / "ui_builder" / "static" / "index.html").read_text(encoding="utf-8")
INTEGRATION = (ROOT / "ui_builder" / "integration.py").read_text(encoding="utf-8")
APP_JS = (ROOT / "ui_builder" / "static" / "app.js").read_text(encoding="utf-8")
TOURNAMENT_JS = (ROOT / "hockey_data" / "static" / "tournament-menu.js").read_text(encoding="utf-8")
APP_PY = (ROOT / "app.py").read_text(encoding="utf-8")


def test_runtime_has_no_builder_dom():
    assert 'id="editor"' not in RUNTIME
    assert 'id="componentLibrary"' not in RUNTIME
    assert 'id="canvasStage"' not in RUNTIME
    assert 'id="inspector"' not in RUNTIME
    assert 'class="topbar editor-only"' not in RUNTIME
    assert 'id="runtimeView"' in RUNTIME
    assert 'runtime-view runtime-only hidden' not in RUNTIME


def test_editor_keeps_full_builder():
    assert 'id="editor"' in EDITOR
    assert 'id="componentLibrary"' in EDITOR
    assert 'id="canvasStage"' in EDITOR
    assert 'id="inspector"' in EDITOR


def test_integration_uses_separate_runtime_template():
    assert 'runtime_template = (assets_dir / "runtime.html")' in INTEGRATION
    assert 'template = editor_template if mode == "editor" else runtime_template' in INTEGRATION


def test_editor_button_is_admin_only_and_hotkey_respects_it():
    assert 'id="runtimeEditorBtn" class="icon-btn hidden"' in RUNTIME
    assert 'editorButton?.classList.toggle("hidden", !state.isAdmin);' in TOURNAMENT_JS
    assert 'document.body.dataset.hockeyAdmin = state.isAdmin ? "1" : "0";' in TOURNAMENT_JS
    assert 'el.runtimeEditorBtn?.classList.contains("hidden")' in APP_JS


def test_editor_server_paths_are_admin_protected():
    assert 'path == "/editor"' in APP_PY
    assert 'path.startswith("/api/ui-builder/editor/")' in APP_PY
    assert 'path.startswith("/api/ui-builder/auth/")' in APP_PY
    assert '(khl_admin_path or editor_path) and not user.is_admin' in APP_PY
