-- Miage Temara (jusqu'en 2025-2026) : chaque épreuve prend l'enseignant de son élément
-- Seulement les épreuves dont l'enseignant n'est ni l'enseignant de cours / TD / TP de l'élément, ni un enseignant affecté à l'élément
-- (246 épreuves, 65 éléments, différences déjà présentes dans l'ancienne base).
-- Enseignant retenu : enseignant de cours de l'élément, sinon l'enseignant affecté « cours », sinon le premier enseignant affecté.
-- Sauvegarde dans zz_sauv_epreuves_enseignant (exam_id + ancien enseignant). Relançable. phpMyAdmin : base Groupe Miage, onglet SQL, Exécuter.

SET @ct := (SELECT ct_id FROM n_centres WHERE ct_nom = 'Miage Temara' LIMIT 1);

DROP TABLE IF EXISTS zz_ep_prof;
CREATE TABLE zz_ep_prof (exam_id BIGINT UNSIGNED PRIMARY KEY, ancien_prof BIGINT UNSIGNED NULL, nouveau_prof BIGINT UNSIGNED NULL);
INSERT INTO zz_ep_prof (exam_id, ancien_prof, nouveau_prof)
SELECT x.exam_id, x.exam_prof_id,
       COALESCE(e.emo_prof_cours_id,
                (SELECT af.aff_prof_id FROM n_element_affectations af WHERE af.aff_emo_id = e.emo_id AND af.aff_role = 'cours' ORDER BY af.aff_id LIMIT 1),
                (SELECT af.aff_prof_id FROM n_element_affectations af WHERE af.aff_emo_id = e.emo_id ORDER BY af.aff_id LIMIT 1))
FROM n_examens x
JOIN n_elements_modules e ON e.emo_id = x.exam_emo_id
JOIN n_unites_formation u ON u.uf_id = e.emo_uf_id
JOIN n_annees_scolaires a ON a.as_id = u.uf_annee_id
JOIN n_classes c ON c.classe_id = e.emo_classe_id
WHERE c.classe_centre_id = @ct AND a.as_nom <= '2025-2026' AND x.deleted_at IS NULL
  AND NOT (x.exam_prof_id <=> e.emo_prof_cours_id) AND NOT (x.exam_prof_id <=> e.emo_prof_td_id) AND NOT (x.exam_prof_id <=> e.emo_prof_p_id)
  AND NOT EXISTS (SELECT 1 FROM n_element_affectations af WHERE af.aff_emo_id = e.emo_id AND af.aff_prof_id = x.exam_prof_id);
DELETE FROM zz_ep_prof WHERE nouveau_prof IS NULL;          -- élément sans enseignant : on ne touche pas

-- Avant : ce qui va changer
SELECT c.classe_abbr AS classe, a.as_nom AS annee, m.mat_nom AS element, COUNT(*) AS epreuves,
       CONCAT(pa.prof_nom, ' ', pa.prof_prenom) AS enseignant_actuel, CONCAT(pn.prof_nom, ' ', pn.prof_prenom) AS nouvel_enseignant
FROM zz_ep_prof z JOIN n_examens x ON x.exam_id = z.exam_id JOIN n_elements_modules e ON e.emo_id = x.exam_emo_id
JOIN n_unites_formation u ON u.uf_id = e.emo_uf_id JOIN n_annees_scolaires a ON a.as_id = u.uf_annee_id
JOIN n_classes c ON c.classe_id = e.emo_classe_id JOIN n_matieres m ON m.mat_id = e.emo_mat_id
LEFT JOIN n_enseignants pa ON pa.prof_id = z.ancien_prof LEFT JOIN n_enseignants pn ON pn.prof_id = z.nouveau_prof
GROUP BY c.classe_abbr, a.as_nom, m.mat_nom, z.ancien_prof, z.nouveau_prof ORDER BY annee, classe, element;

CREATE TABLE IF NOT EXISTS zz_sauv_epreuves_enseignant (exam_id BIGINT UNSIGNED PRIMARY KEY, ancien_prof BIGINT UNSIGNED NULL, sauve_le DATETIME);
INSERT IGNORE INTO zz_sauv_epreuves_enseignant SELECT exam_id, ancien_prof, NOW() FROM zz_ep_prof;

UPDATE n_examens x JOIN zz_ep_prof z ON z.exam_id = x.exam_id SET x.exam_prof_id = z.nouveau_prof;

DROP TABLE IF EXISTS zz_ep_prof;

-- Vérification : épreuves encore avec un autre enseignant (doit être 0, sauf élément sans enseignant)
SELECT COUNT(*) AS epreuves_encore_differentes
FROM n_examens x JOIN n_elements_modules e ON e.emo_id = x.exam_emo_id JOIN n_unites_formation u ON u.uf_id = e.emo_uf_id
JOIN n_annees_scolaires a ON a.as_id = u.uf_annee_id JOIN n_classes c ON c.classe_id = e.emo_classe_id
WHERE c.classe_centre_id = @ct AND a.as_nom <= '2025-2026' AND x.deleted_at IS NULL
  AND NOT (x.exam_prof_id <=> e.emo_prof_cours_id) AND NOT (x.exam_prof_id <=> e.emo_prof_td_id) AND NOT (x.exam_prof_id <=> e.emo_prof_p_id)
  AND NOT EXISTS (SELECT 1 FROM n_element_affectations af WHERE af.aff_emo_id = e.emo_id AND af.aff_prof_id = x.exam_prof_id);

-- Retour en arrière si besoin :
-- UPDATE n_examens x JOIN zz_sauv_epreuves_enseignant s ON s.exam_id = x.exam_id SET x.exam_prof_id = s.ancien_prof;
