-- 2026-05-04: Tempo-Kontrolle + Live View
-- Idempotent.

-- 1) Pause-Schalter pro Klasse+Modul (Lehrer steuert das Tempo)
SET @col_exists := (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'class_modules' AND COLUMN_NAME = 'paused'
);
SET @sql := IF(@col_exists = 0,
  'ALTER TABLE class_modules ADD COLUMN paused TINYINT(1) NOT NULL DEFAULT 0 AFTER current_level',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- 2) Live-Heartbeat-Tabelle für Live View
CREATE TABLE IF NOT EXISTS live_sessions (
  student_id  INT UNSIGNED PRIMARY KEY,
  class_id    INT UNSIGNED NOT NULL,
  module_id   VARCHAR(16) NOT NULL,
  state_json  LONGTEXT,
  started_at  TIMESTAMP NULL,
  last_seen   TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  INDEX idx_class (class_id, last_seen),
  INDEX idx_module (module_id, last_seen)
) ENGINE=InnoDB;
