from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
APP_JS = (ROOT / "ui_builder/static/app.js").read_text(encoding="utf-8")
APP = (ROOT / "app.py").read_text(encoding="utf-8")


def _block(start: str, end: str) -> str:
    return APP_JS.split(start, 1)[1].split(end, 1)[0]


def test_prepared_penalty_does_not_force_vmix_rebalance():
    block = _block("function emitHockeyEventUpdated", "function createHockeyPenaltyDraft")
    assert "rebalanceVmixPenaltyTargets" not in block
    draft = _block("function createHockeyPenaltyDraft", "function findHockeyPenalty")
    assert "startedOnce: false" in draft
    control = _block("function controlHockeyPenalty", "function formatHockeyPenaltyTime")
    start_branch = control.split('if (command === "start")', 1)[1].split('} else if (command === "pause")', 1)[0]
    assert "event.startedOnce = true" in start_branch


def test_penalty_vmix_display_uses_only_started_or_paused_active_penalties():
    assert "function activeHockeyPenaltyEntries()" in APP_JS
    sorted_block = _block("function sortedPenaltyEntries", "function penaltyLocalAdvantageSide")
    assert "activeHockeyPenaltyEntries()" in sorted_block


def test_main_countdown_start_pause_stop_all_hard_sync_web_value():
    block = _block("async function syncActiveVmixGameCountdown", "function penaltyMirrorKey")
    assert 'vmixCountdownSyncCommands(input, selectedName, valueProvider, "start")' in block
    assert 'vmixCountdownSyncCommands(input, selectedName, valueProvider, "pause")' in block
    assert 'vmixCountdownSyncCommands(input, selectedName, valueProvider, "stop")' in block
    helper = _block("function vmixCountdownSyncCommands", "function buildShortcutRuntimeContext")
    start_branch = helper.rsplit("return [", 1)[1]
    assert 'Function: "SuspendCountdown"' not in start_branch
    assert start_branch.index('Function: "SetCountdown"') < start_branch.index('Function: "StartCountdown"')


def test_penalty_pause_freezes_before_reseed_and_start_reseeds_before_run():
    block = _block("async function rebalanceVmixPenaltyTargets", "function finishActionMatchesSource")
    assert 'stopCommands.push({ Function: "StopCountdown"' in block
    assert 'setCommands.push({ Function: "SetCountdown"' in block
    assert 'runCommands.push({ Function: "StartCountdown"' in block
    assert 'sendRuntimeVmixTimerSequence(commands)' in block
    assert "prepared/paused penalty" in block


def test_combined_shortcut_changes_web_first_then_synchronizes_vmix():
    block = APP_JS.split('case "hockey_vmix_timers_start":', 1)[1].split('case "delay":', 1)[0]
    web_game = block.index("if (step.start_web_game)")
    vmix_game = block.index("if (step.sync_vmix_game && step.game_vmix_input)")
    assert web_game < vmix_game
    assert 'vmixCountdownSyncCommands(' in block
    assert '() => gameTimerState.currentMs' in block
    assert '() => event.remainingMs' in block


def test_build95_runtime_version():
    assert 'BUILD_VERSION = "2026.08.20.16"' in APP
