from pathlib import Path

from hockey_data.service import HockeyDataService
from tests.support import LocalTestDatabase


JS = Path("ui_builder/static/app.js").read_text(encoding="utf-8")
HTML = Path("ui_builder/static/index.html").read_text(encoding="utf-8")
AGENT_JS = Path("hockey_data/static/agent-devices.js").read_text(encoding="utf-8")
AGENT_CSS = Path("hockey_data/static/agent-devices.css").read_text(encoding="utf-8")
ROUTER = Path("hockey_data/router.py").read_text(encoding="utf-8")
DATABASE = Path("hockey_data/database.py").read_text(encoding="utf-8")


def test_vmix_agent_is_small_topbar_indicator():
    assert 'document.querySelector(".runtime-topbar .runtime-actions")' in AGENT_JS
    assert 'toolbar.prepend(root)' in AGENT_JS
    assert 'hockey-agent-chip-copy' not in AGENT_JS
    assert 'width: 35px;' in AGENT_CSS
    assert 'height: 35px;' in AGENT_CSS
    assert 'top: calc(100% + 9px);' in AGENT_CSS
    assert 'bottom: auto;' in AGENT_CSS
    assert 'position: fixed;' not in AGENT_CSS.split('.hockey-agent-panel > header', 1)[0]


def test_account_switch_now_controls_runtime_toasts_without_disabling_triggers():
    assert 'id="runtimeEventsToggleBtn"' in HTML
    assert 'operatorToastsEnabled: true' in JS
    assert 'toggleRuntimeEventsPreference' in JS
    assert '"/preferences/toasts"' in JS
    assert 'Toast: ВКЛ' in JS
    assert '@router.put("/preferences/toasts")' in ROUTER
    assert '@router.get("/preferences")' in ROUTER
    process_start = JS.index('async function processTriggers(context)')
    shortcut_start = JS.index('async function runShortcutSequence')
    assert 'if (!state.triggersEnabled) return;' not in JS[process_start:process_start + 900]
    assert 'if (!state.triggersEnabled) return;' not in JS[shortcut_start:shortcut_start + 1500]
    assert 'state.operatorToastsEnabled === false' in JS


def test_user_preference_defaults_toasts_on_and_persists_per_account(tmp_path):
    db = LocalTestDatabase(tmp_path / "prefs.sqlite")
    db.create_all()
    service = HockeyDataService.__new__(HockeyDataService)
    service.database = db

    first = service.user_preference(wfl_user_id="operator-a")
    second = service.user_preference(wfl_user_id="operator-b")
    assert first["triggers_enabled"] is True
    assert first["toasts_enabled"] is True
    assert second["triggers_enabled"] is True
    assert second["toasts_enabled"] is True

    saved = service.save_toasts_preference(wfl_user_id="operator-a", enabled=False)
    assert saved["triggers_enabled"] is True
    assert saved["toasts_enabled"] is False
    assert service.user_preference(wfl_user_id="operator-a")["toasts_enabled"] is False
    assert service.user_preference(wfl_user_id="operator-b")["triggers_enabled"] is True


def test_additive_schema_repair_includes_user_preferences():
    assert 'UserPreference,' in DATABASE
    repair_block = DATABASE[DATABASE.index('for model in ('):DATABASE.index('self._validate_game_table()')]
    assert 'UserPreference,' in repair_block
