-- Mise à jour zone DMZ pour les 51 serveurs majoritairement classés
-- "DMZ" dans le fichier Plan de Patching serveurs 2026.xlsx.
-- Source : extraction automatique (au moins 50% des feuilles classées DMZ).
-- Idempotent.

\echo '═══ ZONE DMZ ═══'

-- 1) Créer la zone DMZ si elle n'existe pas
INSERT INTO public.zones (name, description, is_dmz)
SELECT 'DMZ', 'Zone démilitarisée — serveurs en DMZ pour Satellite vpdsiasat1', true
 WHERE NOT EXISTS (
    SELECT 1 FROM public.zones
     WHERE UPPER(name) = 'DMZ' OR is_dmz = true
 );

-- Récupérer l'id de la zone DMZ (la 1ère trouvée par is_dmz=true ou name=DMZ)
\echo '═══ AVANT (zone actuelle des hôtes ciblés) ═══'

WITH dmz_zone AS (
    SELECT id FROM public.zones
     WHERE is_dmz = true OR UPPER(name) = 'DMZ'
     ORDER BY id LIMIT 1
)
SELECT z.name AS zone_actuelle, COUNT(*) AS nb_hosts
  FROM public.servers s
  LEFT JOIN public.zones z ON s.zone_id = z.id
 WHERE LOWER(s.hostname::text) IN (
            'vipeahbst1',
            'vpaiiaazu1',
            'vpaiiacam2',
            'vpaiiadns1',
            'vpaiiadns2',
            'vpaiiadns3',
            'vpaiiadns4',
            'vpaiiagml1',
            'vpameased1',
            'vpameatra1',
            'vpbipamod1',
            'vpburaaov1',
            'vpburaaov2',
            'vpburaexc1',
            'vpburaexc2',
            'vpburawap1',
            'vpburawap2',
            'vpdsiaclo1',
            'vpdsiagrd1',
            'vpdsiawsus1',
            'vpdsismtp1',
            'vpdsismtp2',
            'vpexpbdech1',
            'vpgeoagps2',
            'vpgmoaprx1',
            'vpintaprx2',
            'vpintaweb2',
            'vpintaweb3',
            'vppataels1',
            'vppatbsip1',
            'vppeaabst1',
            'vppeaabst2',
            'vppeahbst1',
            'vppintaprx1',
            'vppintaweb1',
            'vppintaweb2',
            'vprauahtp2',
            'vpsimaxsr1',
            'vpssiandes1',
            'vpssiapki3',
            'vptrabkme1',
            'vptrabpxp1',
            'vrameased1',
            'vrdsialab1',
            'vrexpbtex1',
            'vrosapsrv1',
            'vrpeahbst1',
            'vrsamaext1',
            'vrtrabkme1',
            'vrvpnaaov1',
            'vrvpnaaov2'
       )
 GROUP BY z.name
 ORDER BY z.name;

\echo '═══ UPDATE ═══'

UPDATE public.servers
   SET zone_id = (SELECT id FROM public.zones WHERE is_dmz=true OR UPPER(name)='DMZ' ORDER BY id LIMIT 1),
       satellite_url = 'vpdsiasat1.sanef.groupe',
       updated_at = NOW()
 WHERE LOWER(hostname::text) IN (
            'vipeahbst1',
            'vpaiiaazu1',
            'vpaiiacam2',
            'vpaiiadns1',
            'vpaiiadns2',
            'vpaiiadns3',
            'vpaiiadns4',
            'vpaiiagml1',
            'vpameased1',
            'vpameatra1',
            'vpbipamod1',
            'vpburaaov1',
            'vpburaaov2',
            'vpburaexc1',
            'vpburaexc2',
            'vpburawap1',
            'vpburawap2',
            'vpdsiaclo1',
            'vpdsiagrd1',
            'vpdsiawsus1',
            'vpdsismtp1',
            'vpdsismtp2',
            'vpexpbdech1',
            'vpgeoagps2',
            'vpgmoaprx1',
            'vpintaprx2',
            'vpintaweb2',
            'vpintaweb3',
            'vppataels1',
            'vppatbsip1',
            'vppeaabst1',
            'vppeaabst2',
            'vppeahbst1',
            'vppintaprx1',
            'vppintaweb1',
            'vppintaweb2',
            'vprauahtp2',
            'vpsimaxsr1',
            'vpssiandes1',
            'vpssiapki3',
            'vptrabkme1',
            'vptrabpxp1',
            'vrameased1',
            'vrdsialab1',
            'vrexpbtex1',
            'vrosapsrv1',
            'vrpeahbst1',
            'vrsamaext1',
            'vrtrabkme1',
            'vrvpnaaov1',
            'vrvpnaaov2'
       );

\echo '═══ APRÈS ═══'

SELECT z.name AS zone_apres, COUNT(*) AS nb_hosts
  FROM public.servers s
  LEFT JOIN public.zones z ON s.zone_id = z.id
 WHERE LOWER(s.hostname::text) IN (
            'vipeahbst1',
            'vpaiiaazu1',
            'vpaiiacam2',
            'vpaiiadns1',
            'vpaiiadns2',
            'vpaiiadns3',
            'vpaiiadns4',
            'vpaiiagml1',
            'vpameased1',
            'vpameatra1',
            'vpbipamod1',
            'vpburaaov1',
            'vpburaaov2',
            'vpburaexc1',
            'vpburaexc2',
            'vpburawap1',
            'vpburawap2',
            'vpdsiaclo1',
            'vpdsiagrd1',
            'vpdsiawsus1',
            'vpdsismtp1',
            'vpdsismtp2',
            'vpexpbdech1',
            'vpgeoagps2',
            'vpgmoaprx1',
            'vpintaprx2',
            'vpintaweb2',
            'vpintaweb3',
            'vppataels1',
            'vppatbsip1',
            'vppeaabst1',
            'vppeaabst2',
            'vppeahbst1',
            'vppintaprx1',
            'vppintaweb1',
            'vppintaweb2',
            'vprauahtp2',
            'vpsimaxsr1',
            'vpssiandes1',
            'vpssiapki3',
            'vptrabkme1',
            'vptrabpxp1',
            'vrameased1',
            'vrdsialab1',
            'vrexpbtex1',
            'vrosapsrv1',
            'vrpeahbst1',
            'vrsamaext1',
            'vrtrabkme1',
            'vrvpnaaov1',
            'vrvpnaaov2'
       )
 GROUP BY z.name
 ORDER BY z.name;

SELECT 'satellite_url=vpdsiasat1' AS scope, COUNT(*) AS nb
  FROM public.servers
 WHERE satellite_url = 'vpdsiasat1.sanef.groupe'
 UNION ALL
SELECT 'satellite_url=vpdsiasat2' AS scope, COUNT(*) AS nb
  FROM public.servers
 WHERE satellite_url = 'vpdsiasat2.sanef.groupe';
