-- 2026-05-04: Lehrer Jakob + 3 Schüler in Klasse "Jakob 1" + je 1 Lizenz
-- Idempotent: kann mehrfach ausgeführt werden, ohne Daten zu duplizieren.
-- Passwörter als bcrypt-Hash (PHP_DEFAULT, $2y$10$).

START TRANSACTION;

-- 1) Lehrer Jakob (jakob@geograsim.at / hoppala3355!)
INSERT INTO teachers (username, email, password, display_name, email_verified)
VALUES ('jakob', 'jakob@geograsim.at',
        '$2y$10$ofuRFIeNBm/UVZcATyI37eFmEph1vfC81SeP7lalIRNynxoLb77C6',
        'Jakob', 1)
ON DUPLICATE KEY UPDATE
  email          = VALUES(email),
  password       = VALUES(password),
  display_name   = VALUES(display_name),
  email_verified = VALUES(email_verified);

SET @teacher_id := (SELECT id FROM teachers WHERE username='jakob' LIMIT 1);

-- 2) Klasse "Jakob 1" mit Join-Code JAKOB1
INSERT INTO classes (teacher_id, name, school_year, join_code)
VALUES (@teacher_id, 'Jakob 1', '25/26', 'JAKOB1')
ON DUPLICATE KEY UPDATE
  teacher_id  = VALUES(teacher_id),
  name        = VALUES(name),
  school_year = VALUES(school_year);

SET @class_id := (SELECT id FROM classes WHERE join_code='JAKOB1' LIMIT 1);

-- 3) Schüler:innen (Pseudonyme, anonym=1)
INSERT INTO students (class_id, username, password, password_plaintext, display_name, is_anonymous, emoji_avatar)
VALUES
  (@class_id, 'Alexom',   '$2y$10$FvNMeFn/ozKIZY0l2zbQEOx/UZefPQZIf2bbDcOA/igjNBWxj5H0a', '3322', 'Alexom',   1, '🧑‍🎓'),
  (@class_id, 'Berturam', '$2y$10$uj6YrgOqTeI9nv9bV.5/XOEFYRLrlt5MdXBzxRX3dFREcw2Th9X1C', '7788', 'Berturam', 1, '🧑‍🎓'),
  (@class_id, 'Sussama',  '$2y$10$nOS5jXAY.kuMQ1mhr/6Zwug/fHglF94K3D5gSg.1RWrSoq7GIr7y6', '9991', 'Sussama',  1, '🧑‍🎓')
ON DUPLICATE KEY UPDATE
  password           = VALUES(password),
  password_plaintext = VALUES(password_plaintext),
  display_name       = VALUES(display_name),
  is_anonymous       = VALUES(is_anonymous),
  emoji_avatar       = VALUES(emoji_avatar);

SET @stud_alex := (SELECT id FROM students WHERE class_id=@class_id AND username='Alexom'   LIMIT 1);
SET @stud_bert := (SELECT id FROM students WHERE class_id=@class_id AND username='Berturam' LIMIT 1);
SET @stud_suss := (SELECT id FROM students WHERE class_id=@class_id AND username='Sussama'  LIMIT 1);

-- 4) Lizenzen — pro Schüler eine Lizenz aus dem freien Pool nehmen, an Jakob redeemen, an Schüler zuweisen.
--    Wenn der Schüler schon eine Lizenz hat → die behalten (idempotent).
SET @lic_alex := IFNULL(
  (SELECT id FROM licenses WHERE student_id=@stud_alex LIMIT 1),
  (SELECT id FROM licenses WHERE student_id IS NULL AND teacher_id IS NULL ORDER BY id LIMIT 1)
);
UPDATE licenses
   SET teacher_id  = @teacher_id,
       redeemed_at = COALESCE(redeemed_at, NOW()),
       student_id  = @stud_alex
 WHERE id = @lic_alex;

SET @lic_bert := IFNULL(
  (SELECT id FROM licenses WHERE student_id=@stud_bert LIMIT 1),
  (SELECT id FROM licenses WHERE student_id IS NULL AND teacher_id IS NULL ORDER BY id LIMIT 1)
);
UPDATE licenses
   SET teacher_id  = @teacher_id,
       redeemed_at = COALESCE(redeemed_at, NOW()),
       student_id  = @stud_bert
 WHERE id = @lic_bert;

SET @lic_suss := IFNULL(
  (SELECT id FROM licenses WHERE student_id=@stud_suss LIMIT 1),
  (SELECT id FROM licenses WHERE student_id IS NULL AND teacher_id IS NULL ORDER BY id LIMIT 1)
);
UPDATE licenses
   SET teacher_id  = @teacher_id,
       redeemed_at = COALESCE(redeemed_at, NOW()),
       student_id  = @stud_suss
 WHERE id = @lic_suss;

COMMIT;

-- Bestätigung
SELECT t.id AS teacher_id, t.username, t.email
  FROM teachers t WHERE t.username='jakob';
SELECT c.id AS class_id, c.name, c.school_year, c.join_code
  FROM classes c WHERE c.teacher_id=(SELECT id FROM teachers WHERE username='jakob');
SELECT s.id, s.username, s.display_name, s.password_plaintext, l.code AS license_code
  FROM students s
  LEFT JOIN licenses l ON l.student_id = s.id
 WHERE s.class_id=(SELECT id FROM classes WHERE join_code='JAKOB1')
 ORDER BY s.id;
