-- One-shot : normalise les valeurs env/domaine existantes vers les formes canoniques
-- (cf table _canonicalize_env / _canonicalize_domain dans planning_import.py).
-- À jouer une fois après pull du code qui ajoute la canonicalisation à l'import.
-- Idempotent : on peut le rejouer sans effet de bord.

-- ─── Environnements ──────────────────────────────────────────

UPDATE patch_planning_import_rows SET environnement = 'Production'
 WHERE LOWER(environnement) IN ('production','prod','prd')
   AND environnement != 'Production';

UPDATE patch_planning_import_rows SET environnement = 'Pré-Prod'
 WHERE LOWER(REGEXP_REPLACE(environnement, '\s+', ' ', 'g')) IN
       ('pré-prod','pre-prod','preprod','pre prod','pré prod',
        'pre-production','pré-production','preproduction','préproduction')
   AND environnement != 'Pré-Prod';

UPDATE patch_planning_import_rows SET environnement = 'Recette'
 WHERE LOWER(environnement) IN ('recette','rec','recettes')
   AND environnement != 'Recette';

UPDATE patch_planning_import_rows SET environnement = 'Test'
 WHERE LOWER(environnement) IN ('test','tests')
   AND environnement != 'Test';

UPDATE patch_planning_import_rows SET environnement = 'Test 1'
 WHERE LOWER(REPLACE(REPLACE(environnement, '_', ' '), '  ', ' ')) IN ('test 1','test1')
   AND environnement != 'Test 1';

UPDATE patch_planning_import_rows SET environnement = 'Test 2'
 WHERE LOWER(REPLACE(REPLACE(environnement, '_', ' '), '  ', ' ')) IN ('test 2','test2')
   AND environnement != 'Test 2';

UPDATE patch_planning_import_rows SET environnement = 'Développement'
 WHERE LOWER(environnement) IN ('développement','developpement','dev','develop')
   AND environnement != 'Développement';

UPDATE patch_planning_import_rows SET environnement = 'Qualif'
 WHERE LOWER(environnement) IN ('qualif','qualification')
   AND environnement != 'Qualif';

-- ─── Domaines ────────────────────────────────────────────────

UPDATE patch_planning_import_rows SET domaine = 'Flux Libre'
 WHERE LOWER(REGEXP_REPLACE(domaine, '\s+', ' ', 'g')) IN ('flux libre','flux-libre','fluxlibre')
   AND domaine != 'Flux Libre';

UPDATE patch_planning_import_rows SET domaine = 'Péage'
 WHERE LOWER(domaine) IN ('peage','péage','peagé','pèage')
   AND domaine != 'Péage';

UPDATE patch_planning_import_rows SET domaine = 'Infrastructure'
 WHERE LOWER(domaine) IN ('infrastructure','infra')
   AND domaine != 'Infrastructure';

UPDATE patch_planning_import_rows SET domaine = 'Trafic'
 WHERE LOWER(domaine) IN ('trafic','traffic')
   AND domaine != 'Trafic';

UPDATE patch_planning_import_rows SET domaine = UPPER(domaine)
 WHERE LOWER(domaine) IN ('dmz','lan','bi','emv')
   AND domaine != UPPER(domaine);

UPDATE patch_planning_import_rows SET domaine = 'Gestion'
 WHERE LOWER(domaine) = 'gestion' AND domaine != 'Gestion';

-- ─── Vérification ───────────────────────────────────────────

-- SELECT environnement, COUNT(*) FROM patch_planning_import_rows GROUP BY 1 ORDER BY 1;
-- SELECT domaine, COUNT(*) FROM patch_planning_import_rows GROUP BY 1 ORDER BY 1;
