// Show tab — slim entry. Per KL-109 most of the heavy logic moved to:
//   director_panel.js — Lumi cartoon, mood word, cycle bar, beat pulse, etc.
//   lumi_console.js   — KL-63 read-only fader animations
//   hud_systems.js    — KL-67 DMX + Govee status badges
//
// What lives here: floating quick-actions wiring, engine-state reflection,
// and the small stats card. main.js imports from this file plus the three
// modules above directly.

export function updateStats(data) {
    const lullEl = document.getElementById('stat-lull');
    const buildupEl = document.getElementById('stat-buildup');
    const convergenceEl = document.getElementById('stat-convergence');
    const uptimeEl = document.getElementById('stat-uptime');

    if (lullEl) lullEl.textContent = `${Math.round((data.lull_factor || 0) * 100)}%`;
    if (buildupEl) buildupEl.textContent = (data.buildup_factor || 0).toFixed(2);
    if (convergenceEl) convergenceEl.textContent = (data.convergence_factor || 0).toFixed(2);
    if (uptimeEl) {
        const secs = Math.floor(data.uptime || 0);
        const mins = Math.floor(secs / 60);
        const s = secs % 60;
        uptimeEl.textContent = `${mins}:${s.toString().padStart(2, '0')}`;
    }
}

export function initQuickActions() {
    const playPauseBtn = document.getElementById('btn-engine-playpause');
    const stopBtn = document.getElementById('btn-engine-stop');

    // Play/Pause toggle. The button's data-state attribute reflects the
    // current engine state; on click we POST to whichever endpoint
    // corresponds to the *next* action.
    playPauseBtn?.addEventListener('click', async () => {
        const isPlaying = playPauseBtn.dataset.state === 'playing';
        const url = isPlaying ? '/api/engine/pause' : '/api/engine/play';
        try {
            const r = await fetch(url, {method: 'POST'});
            const data = await r.json();
            reflectEngineState(!!data.running);
        } catch (e) { /* non-critical */ }
    });

    // Emergency stop. Always available, always the same call regardless
    // of current state — re-clicking on a stopped engine re-asserts the
    // blackout, which is intentional.
    stopBtn?.addEventListener('click', async () => {
        try {
            const r = await fetch('/api/engine/stop', {method: 'POST'});
            const data = await r.json();
            reflectEngineState(!!data.running);
        } catch (e) { /* non-critical */ }
    });

    document.getElementById('btn-blackout')?.addEventListener('click', () => {
        fetch('/api/action/blackout', {method: 'POST'});
    });
    document.getElementById('btn-flash')?.addEventListener('click', () => {
        fetch('/api/action/flash', {method: 'POST'});
    });
    // KL-106: btn-scene-next removed alongside the show-preset feature.

    // Initial render from state endpoint; 10Hz state push keeps it live.
    fetch('/api/engine/state').then(r => r.json()).then(d => {
        reflectEngineState(!!d.running);
    }).catch(() => {});
}

// Switch the Play/Pause button's label + data-state to mirror the live
// engine state. Streaming-service paradigm: when playing, button shows
// PAUSE (the action you'd take); when stopped, button shows PLAY.
export function reflectEngineState(running) {
    const btn = document.getElementById('btn-engine-playpause');
    if (!btn) return;
    if (running) {
        btn.dataset.state = 'playing';
        btn.textContent = '⏸ PAUSE';
        btn.title = 'Pause: freeze the show; fixtures hold their current state';
    } else {
        btn.dataset.state = 'stopped';
        btn.textContent = '▶ PLAY';
        btn.title = 'Play: start (or resume) the engine';
    }
}

// Re-exports from the split modules. Director panel functions used to
// re-export from here too; main.js now imports them directly from
// features/director_panel.js.
export {updateSystems} from './hud_systems.js';
export {updateLumiSliders, initSliders} from './lumi_console.js';

// KL-106 cleanup left initPresets dead. Removed in that commit.
