-- I.F.PRO : filières, formations et classes (feuilles 1 à 5 du fichier « template_inscrits ifpro »).
-- À lancer après parametres_centre_ifpro.sql. Sans risque, relançable (rien n'est créé en double).
-- phpMyAdmin : base I.F.PRO, onglet SQL, coller tout, Exécuter.

SET @ifp := (SELECT ct_id FROM n_centres WHERE ct_code = 'IFP');
SET @site := (SELECT site_id FROM n_sites WHERE site_centre_id = @ifp AND site_est_principal = 1 LIMIT 1);
SET @ts := (SELECT dip_id FROM n_diplomes WHERE dip_abbr = 'TS' LIMIT 1);

-- 1) Un seul diplôme TS (pour IP, GE et SRI). Si une version précédente de ce script a créé « TS3 », il est retiré
--    et la formation TS-IP-22 repasse sur le TS.
SET @ts3 := (SELECT dip_id FROM n_diplomes WHERE dip_abbr = 'TS3');
UPDATE n_formations SET for_diplome_id = @ts WHERE for_diplome_id = @ts3;
DELETE l FROM n_groupecritere_lignes l JOIN n_groupes_criteres g ON g.gc_id = l.gcl_gc_id WHERE g.gc_diplome_id = @ts3;
DELETE FROM n_groupes_criteres WHERE gc_diplome_id = @ts3;
DELETE FROM n_pv_access WHERE pva_diplome_id = @ts3;
DELETE FROM n_diplomes WHERE dip_id = @ts3;

-- 2) Domaines des filières (créés s'ils manquent : le paramédical a pu être supprimé)
INSERT INTO n_domaines (dom_nom, dom_code, dom_ordre, dom_est_actif, created_at)
SELECT x.nom, x.code, x.ordre, 1, NOW()
FROM (
  SELECT 'Paramédical' nom, 'PARAMEDICAL' code, 8 ordre
  UNION ALL SELECT 'Technologie d''Information et de Communication', 'TIC', 10
  UNION ALL SELECT 'Administration, Gestion et Commerce', 'AGC', 11
) x
WHERE NOT EXISTS (SELECT 1 FROM n_domaines d WHERE d.dom_code = x.code OR d.dom_nom = x.nom);

-- 3) Filières
INSERT INTO n_filieres (fil_abbr, fil_code_inscription, fil_nom, fil_dom_id, created_at)
SELECT x.abbr, x.code, x.nom, (SELECT dom_id FROM n_domaines WHERE dom_code = x.domaine OR dom_nom = x.nom_domaine ORDER BY dom_id LIMIT 1), NOW()
FROM (
  SELECT 'IP' abbr, '1' code, 'Infirmier Polyvalent' nom, 'PARAMEDICAL' domaine, 'Paramédical' nom_domaine
  UNION ALL SELECT 'GE', '2', 'Gestion des Entreprises', 'AGC', 'Administration, Gestion et Commerce'
  UNION ALL SELECT 'SRI', '3', 'Systèmes et Réseaux Informatiques', 'TIC', 'Technologie d''Information et de Communication'
  UNION ALL SELECT 'IA', '4', 'Infirmier Auxiliaire', 'PARAMEDICAL', 'Paramédical'
) x
WHERE NOT EXISTS (SELECT 1 FROM n_filieres f WHERE f.fil_abbr = x.abbr);

-- 4) Formations (2022-2023 → 2026-2027, accréditées, en français)
SET @debut := (SELECT as_id FROM n_annees_scolaires WHERE as_nom = '2022-2023');
SET @fin := (SELECT as_id FROM n_annees_scolaires WHERE as_nom = '2026-2027');
INSERT INTO n_formations (for_centre_id, for_abbr, for_nom, for_diplome_id, for_filiere_id, for_option_id, for_est_accreditee, for_langue,
    for_code_filiere, for_code_option, for_as_debut_id, for_as_fin_id, created_at)
SELECT @ifp, x.code, x.nom, d.dip_id, fl.fil_id, NULL, 1, 'fr', fl.fil_code_inscription, NULL, @debut, @fin, NOW()
FROM (
  SELECT 'TS-IP-22' code, 'Technicien Spécialisé Infirmier Polyvalent' nom, 'TS' dip, 'IP' fil
  UNION ALL SELECT 'TS-GE-22', 'Technicien Spécialisé Gestion des Entreprises', 'TS', 'GE'
  UNION ALL SELECT 'TS-SRI-22', 'Technicien Spécialisé Systèmes et Réseaux Informatiques', 'TS', 'SRI'
  UNION ALL SELECT 'T-IA-22', 'Technicien Infirmier Auxiliaire', 'T', 'IA'
) x
JOIN n_diplomes d ON d.dip_abbr = x.dip
JOIN n_filieres fl ON fl.fil_abbr = x.fil
WHERE @ifp IS NOT NULL AND NOT EXISTS (SELECT 1 FROM n_formations f WHERE f.for_centre_id = @ifp AND f.for_abbr = x.code);

-- 5) Classes (codes et noms du fichier)
INSERT INTO n_classes (classe_formation_id, classe_centre_id, classe_site_id, classe_abbr, classe_nom, classe_nom_court, classe_nom_complet,
    classe_niveau_filiere, classe_niveau_apresbac, classe_as_debut_id, classe_as_fin_id, classe_suivante_id, classe_est_visible, classe_note_est_affichee, created_at)
SELECT f.for_id, @ifp, @site, x.abbr, x.abbr, x.court, x.complet, x.niveau, x.apresbac, @debut, @fin, NULL, 1, 0, NOW()
FROM (
  SELECT 'TS-IP-22' formation, '1A-TS-IP-22' abbr, '1ère Année Technicien Spécialisé IP' court, '1ère Année Technicien Spécialisé Infirmier Polyvalent' complet, 1 niveau, 1 apresbac
  UNION ALL SELECT 'TS-IP-22', '2A-TS-IP-22', '2ème Année Technicien Spécialisé IP', '2ème Année Technicien Spécialisé Infirmier Polyvalent', 2, 2
  UNION ALL SELECT 'TS-IP-22', '3A-TS-IP-22', '3ème Année Technicien Spécialisé IP', '3ème Année Technicien Spécialisé Infirmier Polyvalent', 3, 3
  UNION ALL SELECT 'TS-GE-22', '1A-TS-GE-22', '1ère Année Technicien Spécialisé GE', '1ère Année Technicien Spécialisé Gestion des Entreprises', 1, 1
  UNION ALL SELECT 'TS-GE-22', '2A-TS-GE-22', '2ème Année Technicien Spécialisé GE', '2ème Année Technicien Spécialisé Gestion des Entreprises', 2, 2
  UNION ALL SELECT 'TS-SRI-22', '1A-TS-SRI-22', '1ère Année Technicien Spécialisé SRI', '1ère Année Technicien Spécialisé Systèmes et Réseaux Informatiques', 1, 1
  UNION ALL SELECT 'TS-SRI-22', '2A-TS-SRI-22', '2ème Année Technicien Spécialisé SRI', '2ème Année Technicien Spécialisé Systèmes et Réseaux Informatiques', 2, 2
  UNION ALL SELECT 'T-IA-22', '1A-T-IA-22', '1ère Année Technicien IA', '1ère Année Technicien Infirmier Auxiliaire', 1, 1
  UNION ALL SELECT 'T-IA-22', '2A-T-IA-22', '2ème Année Technicien IA', '2ème Année Technicien Infirmier Auxiliaire', 2, 2
) x
JOIN n_formations f ON f.for_centre_id = @ifp AND f.for_abbr = x.formation
WHERE NOT EXISTS (SELECT 1 FROM n_classes c WHERE c.classe_formation_id = f.for_id AND c.classe_abbr = x.abbr);

-- Classe suivante (1A → 2A → 3A)
UPDATE n_classes c JOIN n_classes s ON s.classe_formation_id = c.classe_formation_id AND s.classe_niveau_filiere = c.classe_niveau_filiere + 1
SET c.classe_suivante_id = s.classe_id WHERE c.classe_centre_id = @ifp AND c.classe_suivante_id IS NULL;

-- Vérification
SELECT f.for_abbr, f.for_nom, d.dip_abbr, d.dip_nb_annees, fl.fil_abbr, COUNT(c.classe_id) AS classes, GROUP_CONCAT(c.classe_abbr ORDER BY c.classe_niveau_filiere) AS liste
FROM n_formations f JOIN n_diplomes d ON d.dip_id = f.for_diplome_id JOIN n_filieres fl ON fl.fil_id = f.for_filiere_id
LEFT JOIN n_classes c ON c.classe_formation_id = f.for_id WHERE f.for_centre_id = @ifp GROUP BY f.for_id, f.for_abbr, f.for_nom, d.dip_abbr, d.dip_nb_annees, fl.fil_abbr;
