-- GeoGraSim — Schema V2: Vollstaendiges User Management
-- Fuehre NACH schema.sql aus: mysql -u root geograsim < schema-v2.sql

USE geograsim;

-- Lehrkraefte erweitern um E-Mail (ignoriere Fehler wenn Spalte schon existiert)
ALTER TABLE teachers ADD COLUMN email VARCHAR(255) UNIQUE AFTER username;
ALTER TABLE teachers ADD COLUMN email_verified BOOLEAN DEFAULT FALSE AFTER email;
ALTER TABLE teachers ADD COLUMN school_name VARCHAR(255) AFTER display_name;
ALTER TABLE teachers ADD COLUMN reset_token VARCHAR(64) AFTER email_verified;
ALTER TABLE teachers ADD COLUMN reset_expires TIMESTAMP NULL AFTER reset_token;

-- Schueler als echte Accounts (nicht nur Sessions)
CREATE TABLE IF NOT EXISTS students (
  id           INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  class_id     INT UNSIGNED NOT NULL,
  username     VARCHAR(64) NOT NULL,
  password     VARCHAR(255) NOT NULL,
  display_name VARCHAR(128),
  first_name   VARCHAR(64),
  last_name    VARCHAR(64),
  email        VARCHAR(255),
  emoji_avatar VARCHAR(8) DEFAULT '🧑‍🎓',
  is_anonymous BOOLEAN DEFAULT TRUE,   -- true = Pseudonym, false = echter Name
  created_at   TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  last_login   TIMESTAMP NULL,
  UNIQUE KEY uq_class_user (class_id, username),
  FOREIGN KEY (class_id) REFERENCES classes(id) ON DELETE CASCADE
) ENGINE=InnoDB;

-- Lizenzen
CREATE TABLE IF NOT EXISTS licenses (
  id           INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  code         VARCHAR(32) NOT NULL UNIQUE,
  teacher_id   INT UNSIGNED,           -- NULL = noch nicht eingeloest
  max_students INT UNSIGNED DEFAULT 30,
  valid_from   DATE NOT NULL,
  valid_until  DATE NOT NULL,
  created_at   TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  redeemed_at  TIMESTAMP NULL,
  FOREIGN KEY (teacher_id) REFERENCES teachers(id) ON DELETE SET NULL
) ENGINE=InnoDB;

-- Modulfreigabe erweitern
ALTER TABLE class_modules ADD COLUMN mode ENUM('locked','free','teacher_started') DEFAULT 'locked' AFTER enabled;
ALTER TABLE class_modules ADD COLUMN started_at TIMESTAMP NULL AFTER mode;
ALTER TABLE class_modules ADD COLUMN due_date TIMESTAMP NULL AFTER started_at;

-- Schueler-Ergebnisse (verknuepft mit Schueler-Account, nicht nur Session)
CREATE TABLE IF NOT EXISTS student_results (
  id           INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  student_id   INT UNSIGNED NOT NULL,
  class_id     INT UNSIGNED NOT NULL,
  module_id    VARCHAR(16) NOT NULL,
  score        DECIMAL(5,2),           -- 0-100
  balance_idx  DECIMAL(5,2),           -- 0-100
  duration_sec INT UNSIGNED,
  detail_json  JSON,                   -- Modulspezifische Details
  completed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_student (student_id),
  INDEX idx_class_module (class_id, module_id),
  FOREIGN KEY (student_id) REFERENCES students(id) ON DELETE CASCADE,
  FOREIGN KEY (class_id) REFERENCES classes(id) ON DELETE CASCADE
) ENGINE=InnoDB;

-- Aktivitaetslog (wer hat wann was gemacht)
CREATE TABLE IF NOT EXISTS activity_log (
  id           INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  user_type    ENUM('teacher','student') NOT NULL,
  user_id      INT UNSIGNED NOT NULL,
  action       VARCHAR(64) NOT NULL,   -- 'login','start_module','complete_module','view_results'
  detail       VARCHAR(255),
  created_at   TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_user (user_type, user_id),
  INDEX idx_time (created_at)
) ENGINE=InnoDB;
