-- Miage Temara (jusqu'en 2025-2026) : l'élément prend l'ENSEIGNANT DE SES ÉPREUVES
-- (inverse de miage_temara_aligner_enseignant_epreuves.sql : n'exécuter que l'un des deux)
-- Concerne les 65 éléments dont toutes les épreuves sont au nom d'un enseignant qui n'est ni l'enseignant de cours / TD / TP
-- de l'élément, ni un enseignant affecté. Chacun de ces éléments n'a qu'un seul enseignant d'épreuve.
-- Change : enseignant de cours de l'élément, ses TD / TP s'ils étaient au même enseignant, affectations « cours »,
-- liens enseignant-élément. Sauvegarde dans zz_sauv_affect_epreuve_*. Relançable. phpMyAdmin : base Groupe Miage, onglet SQL.

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

DROP TABLE IF EXISTS zz_aff_ep;
CREATE TABLE zz_aff_ep (emo_id BIGINT UNSIGNED PRIMARY KEY, ancien_prof BIGINT UNSIGNED NULL, prof_epreuve BIGINT UNSIGNED NOT NULL);
INSERT INTO zz_aff_ep (emo_id, ancien_prof, prof_epreuve)
SELECT e.emo_id, e.emo_prof_cours_id, MIN(x.exam_prof_id)
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 x.exam_prof_id IS NOT 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)
GROUP BY e.emo_id, e.emo_prof_cours_id
HAVING COUNT(DISTINCT x.exam_prof_id) = 1                                              -- un seul enseignant d'épreuve
   AND (SELECT COUNT(DISTINCT x2.exam_prof_id) FROM n_examens x2 WHERE x2.exam_emo_id = e.emo_id AND x2.deleted_at IS NULL) = 1;

-- Avant : ce qui va changer
SELECT c.classe_abbr AS classe, a.as_nom AS annee, m.mat_nom AS element,
       CONCAT(pa.prof_nom, ' ', pa.prof_prenom) AS enseignant_actuel, CONCAT(pn.prof_nom, ' ', pn.prof_prenom) AS enseignant_des_epreuves
FROM zz_aff_ep z JOIN n_elements_modules e ON e.emo_id = z.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.prof_epreuve
ORDER BY annee, classe, element;

-- Sauvegardes
CREATE TABLE IF NOT EXISTS zz_sauv_affect_epreuve_elements AS
  SELECT emo_id, emo_prof_cours_id, emo_prof_td_id, emo_prof_p_id, NOW() AS sauve_le FROM n_elements_modules WHERE emo_id IN (SELECT emo_id FROM zz_aff_ep);
CREATE TABLE IF NOT EXISTS zz_sauv_affect_epreuve_affectations AS SELECT * FROM n_element_affectations WHERE aff_emo_id IN (SELECT emo_id FROM zz_aff_ep);
CREATE TABLE IF NOT EXISTS zz_sauv_affect_epreuve_prof_elements AS SELECT * FROM n_prof_elements WHERE pe_emo_id IN (SELECT emo_id FROM zz_aff_ep);

-- Élément : cours (et TD / TP quand ils étaient au même enseignant)
UPDATE n_elements_modules e JOIN zz_aff_ep z ON z.emo_id = e.emo_id
SET e.emo_prof_td_id = IF(e.emo_prof_td_id <=> z.ancien_prof AND z.ancien_prof IS NOT NULL, z.prof_epreuve, e.emo_prof_td_id),
    e.emo_prof_p_id  = IF(e.emo_prof_p_id  <=> z.ancien_prof AND z.ancien_prof IS NOT NULL, z.prof_epreuve, e.emo_prof_p_id),
    e.emo_prof_cours_id = z.prof_epreuve;

-- Affectations « cours » de l'ancien enseignant → enseignant des épreuves ; sinon une affectation « cours » est créée
UPDATE IGNORE n_element_affectations af JOIN zz_aff_ep z ON z.emo_id = af.aff_emo_id
SET af.aff_prof_id = z.prof_epreuve WHERE af.aff_role = 'cours' AND af.aff_prof_id <=> z.ancien_prof;
INSERT INTO n_element_affectations (aff_emo_id, aff_groupe_id, aff_periode, aff_role, aff_prof_id, created_at)
SELECT z.emo_id, NULL, NULL, 'cours', z.prof_epreuve, NOW() FROM zz_aff_ep z
WHERE NOT EXISTS (SELECT 1 FROM n_element_affectations af WHERE af.aff_emo_id = z.emo_id AND af.aff_role = 'cours');

-- Liens enseignant-élément
UPDATE IGNORE n_prof_elements pe JOIN zz_aff_ep z ON z.emo_id = pe.pe_emo_id SET pe.pe_prof_id = z.prof_epreuve WHERE pe.pe_prof_id <=> z.ancien_prof;

DROP TABLE IF EXISTS zz_aff_ep;

-- Vérification : épreuves dont l'enseignant n'est toujours pas celui de l'élément (doit être 0)
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_elements_modules e JOIN zz_sauv_affect_epreuve_elements s ON s.emo_id = e.emo_id
--   SET e.emo_prof_cours_id = s.emo_prof_cours_id, e.emo_prof_td_id = s.emo_prof_td_id, e.emo_prof_p_id = s.emo_prof_p_id;
-- DELETE FROM n_element_affectations WHERE aff_emo_id IN (SELECT emo_id FROM zz_sauv_affect_epreuve_elements);
-- INSERT INTO n_element_affectations SELECT * FROM zz_sauv_affect_epreuve_affectations;
-- DELETE FROM n_prof_elements WHERE pe_emo_id IN (SELECT emo_id FROM zz_sauv_affect_epreuve_elements);
-- INSERT INTO n_prof_elements SELECT * FROM zz_sauv_affect_epreuve_prof_elements;
