-- Fusion des 3 fiches de HAYAT EL OMRANI (18, 21, 23 : même e-mail hayatlomrani@gmail.com) dans la fiche 21,
-- et suppression (archivage) de la fiche de test « hhhh Fatima zahra » (16).
-- Les affectations, éléments de module, examens, surveillances et jurys des fiches 18 et 23 passent sur la fiche 21 ;
-- la fiche 21 reçoit le compte hayatlomrani@gmail.com (rôle Enseignant) et le CIN de la fiche 18. Sans variables.
START TRANSACTION;

UPDATE n_element_affectations SET aff_prof_id = 21 WHERE aff_prof_id IN (18, 23);
UPDATE n_elements_modules SET emo_prof_cours_id = 21 WHERE emo_prof_cours_id IN (18, 23);
UPDATE n_elements_modules SET emo_prof_h_id = 21 WHERE emo_prof_h_id IN (18, 23);
UPDATE n_elements_modules SET emo_prof_p_id = 21 WHERE emo_prof_p_id IN (18, 23);
UPDATE n_elements_modules SET emo_prof_td_id = 21 WHERE emo_prof_td_id IN (18, 23);
UPDATE n_examens SET exam_prof_id = 21 WHERE exam_prof_id IN (18, 23);
UPDATE n_prof_elements SET pe_prof_id = 21 WHERE pe_prof_id IN (18, 23);
UPDATE n_pv_deliberations SET pv_jury_id = 21 WHERE pv_jury_id IN (18, 23);
UPDATE n_seance_surveillants SET ss_prof_id = 21 WHERE ss_prof_id IN (18, 23);
UPDATE n_seances SET se_prof_id = 21 WHERE se_prof_id IN (18, 23);
UPDATE n_seances SET se_prof_bkp_id = 21 WHERE se_prof_bkp_id IN (18, 23);
DELETE FROM n_enseignant_affectations WHERE ea_prof_id IN (18, 23);

-- Compte de connexion (créé s'il manque) relié à la fiche 21
INSERT INTO n_utilisateurs (user_email, user_password, user_nom_affiche, user_est_actif, created_at)
SELECT 'hayatlomrani@gmail.com', NULL, 'Hayat EL OMRANI', 1, NOW() FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM n_utilisateurs WHERE user_email = 'hayatlomrani@gmail.com');
UPDATE n_enseignants SET prof_user_id = NULL WHERE prof_id IN (18, 23);
UPDATE n_enseignants SET prof_user_id = (SELECT user_id FROM n_utilisateurs WHERE user_email = 'hayatlomrani@gmail.com') WHERE prof_id = 21;
INSERT IGNORE INTO n_utilisateur_roles (ur_user_id, ur_role_id)
SELECT (SELECT user_id FROM n_utilisateurs WHERE user_email = 'hayatlomrani@gmail.com'), (SELECT role_id FROM n_roles WHERE role_code = 'ENSEIGNANT');

-- CIN de la fiche 18 repris sur la fiche 21
UPDATE n_enseignants SET prof_cin = NULL WHERE prof_id = 18 AND prof_cin = 'AD186686';
UPDATE n_enseignants SET prof_cin = 'AD186686' WHERE prof_id = 21 AND prof_cin IS NULL;

-- Archivage des fiches fusionnées (18, 23) et de la fiche de test (16)
UPDATE n_enseignants SET deleted_at = NOW() WHERE prof_id IN (16, 18, 23) AND deleted_at IS NULL;

COMMIT;

-- Vérification : fiche 21 avec son compte et ses éléments ; fiches 16, 18, 23 archivées
SELECT e.prof_id, e.prof_nom, e.prof_prenom, e.prof_cin, u.user_email AS compte, e.deleted_at AS archivee,
  (SELECT COUNT(*) FROM n_element_affectations WHERE aff_prof_id = e.prof_id) AS affectations,
  (SELECT COUNT(*) FROM n_examens WHERE exam_prof_id = e.prof_id) AS examens
FROM n_enseignants e LEFT JOIN n_utilisateurs u ON u.user_id = e.prof_user_id WHERE e.prof_id IN (16, 18, 21, 23) ORDER BY e.prof_id;
