-- ============================================================================
-- V3: Correction des colonnes métier manquantes
-- ============================================================================
-- Auteur: Lions Dev
-- Date: 2026-03-16
-- Description: Ajoute les colonnes métier manquantes dans les tables
--              adresses et alert_configuration
-- ============================================================================

-- Table adresses - colonnes manquantes
ALTER TABLE adresses ADD COLUMN IF NOT EXISTS adresse VARCHAR(500);
ALTER TABLE adresses ADD COLUMN IF NOT EXISTS complement_adresse VARCHAR(200);
ALTER TABLE adresses ADD COLUMN IF NOT EXISTS principale BOOLEAN DEFAULT false;
ALTER TABLE adresses ADD COLUMN IF NOT EXISTS libelle VARCHAR(100);
ALTER TABLE adresses ADD COLUMN IF NOT EXISTS notes VARCHAR(500);
ALTER TABLE adresses ADD COLUMN IF NOT EXISTS organisation_id UUID;
ALTER TABLE adresses ADD COLUMN IF NOT EXISTS membre_id UUID;
ALTER TABLE adresses ADD COLUMN IF NOT EXISTS evenement_id UUID;

-- Ajouter NOT NULL après coup (si la colonne existe déjà, ça échouera silencieusement)
DO $$
BEGIN
    BEGIN
        ALTER TABLE adresses ALTER COLUMN principale SET NOT NULL;
    EXCEPTION WHEN OTHERS THEN
        NULL;
    END;
END $$;

-- Index pour adresses (s'ils n'existent pas déjà)
CREATE INDEX IF NOT EXISTS idx_adresse_organisation ON adresses(organisation_id);
CREATE INDEX IF NOT EXISTS idx_adresse_membre ON adresses(membre_id);
CREATE INDEX IF NOT EXISTS idx_adresse_evenement ON adresses(evenement_id);

-- Foreign keys pour adresses (avec gestion des doublons)
DO $$
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'fk_adresse_organisation') THEN
        ALTER TABLE adresses ADD CONSTRAINT fk_adresse_organisation
        FOREIGN KEY (organisation_id) REFERENCES organisations(id) ON DELETE CASCADE;
    END IF;

    IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'fk_adresse_membre') THEN
        ALTER TABLE adresses ADD CONSTRAINT fk_adresse_membre
        FOREIGN KEY (membre_id) REFERENCES utilisateurs(id) ON DELETE CASCADE;
    END IF;

    IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'fk_adresse_evenement') THEN
        ALTER TABLE adresses ADD CONSTRAINT fk_adresse_evenement
        FOREIGN KEY (evenement_id) REFERENCES evenements(id) ON DELETE CASCADE;
    END IF;
END $$;

-- Table alert_configuration - colonnes manquantes
ALTER TABLE alert_configuration ADD COLUMN IF NOT EXISTS cpu_high_alert_enabled BOOLEAN DEFAULT true;
ALTER TABLE alert_configuration ADD COLUMN IF NOT EXISTS cpu_threshold_percent INTEGER DEFAULT 80;
ALTER TABLE alert_configuration ADD COLUMN IF NOT EXISTS cpu_duration_minutes INTEGER DEFAULT 5;
ALTER TABLE alert_configuration ADD COLUMN IF NOT EXISTS memory_low_alert_enabled BOOLEAN DEFAULT true;
ALTER TABLE alert_configuration ADD COLUMN IF NOT EXISTS memory_threshold_percent INTEGER DEFAULT 85;
ALTER TABLE alert_configuration ADD COLUMN IF NOT EXISTS critical_error_alert_enabled BOOLEAN DEFAULT true;
ALTER TABLE alert_configuration ADD COLUMN IF NOT EXISTS error_alert_enabled BOOLEAN DEFAULT true;
ALTER TABLE alert_configuration ADD COLUMN IF NOT EXISTS connection_failure_alert_enabled BOOLEAN DEFAULT true;
ALTER TABLE alert_configuration ADD COLUMN IF NOT EXISTS connection_failure_threshold INTEGER DEFAULT 100;
ALTER TABLE alert_configuration ADD COLUMN IF NOT EXISTS connection_failure_window_minutes INTEGER DEFAULT 5;
ALTER TABLE alert_configuration ADD COLUMN IF NOT EXISTS email_notifications_enabled BOOLEAN DEFAULT true;
ALTER TABLE alert_configuration ADD COLUMN IF NOT EXISTS push_notifications_enabled BOOLEAN DEFAULT false;
ALTER TABLE alert_configuration ADD COLUMN IF NOT EXISTS sms_notifications_enabled BOOLEAN DEFAULT false;
ALTER TABLE alert_configuration ADD COLUMN IF NOT EXISTS alert_email_recipients VARCHAR(1000) DEFAULT 'admin@unionflow.test';

-- Ajouter NOT NULL après coup
DO $$
BEGIN
    BEGIN ALTER TABLE alert_configuration ALTER COLUMN cpu_high_alert_enabled SET NOT NULL; EXCEPTION WHEN OTHERS THEN NULL; END;
    BEGIN ALTER TABLE alert_configuration ALTER COLUMN cpu_threshold_percent SET NOT NULL; EXCEPTION WHEN OTHERS THEN NULL; END;
    BEGIN ALTER TABLE alert_configuration ALTER COLUMN cpu_duration_minutes SET NOT NULL; EXCEPTION WHEN OTHERS THEN NULL; END;
    BEGIN ALTER TABLE alert_configuration ALTER COLUMN memory_low_alert_enabled SET NOT NULL; EXCEPTION WHEN OTHERS THEN NULL; END;
    BEGIN ALTER TABLE alert_configuration ALTER COLUMN memory_threshold_percent SET NOT NULL; EXCEPTION WHEN OTHERS THEN NULL; END;
    BEGIN ALTER TABLE alert_configuration ALTER COLUMN critical_error_alert_enabled SET NOT NULL; EXCEPTION WHEN OTHERS THEN NULL; END;
    BEGIN ALTER TABLE alert_configuration ALTER COLUMN error_alert_enabled SET NOT NULL; EXCEPTION WHEN OTHERS THEN NULL; END;
    BEGIN ALTER TABLE alert_configuration ALTER COLUMN connection_failure_alert_enabled SET NOT NULL; EXCEPTION WHEN OTHERS THEN NULL; END;
    BEGIN ALTER TABLE alert_configuration ALTER COLUMN connection_failure_threshold SET NOT NULL; EXCEPTION WHEN OTHERS THEN NULL; END;
    BEGIN ALTER TABLE alert_configuration ALTER COLUMN connection_failure_window_minutes SET NOT NULL; EXCEPTION WHEN OTHERS THEN NULL; END;
    BEGIN ALTER TABLE alert_configuration ALTER COLUMN email_notifications_enabled SET NOT NULL; EXCEPTION WHEN OTHERS THEN NULL; END;
    BEGIN ALTER TABLE alert_configuration ALTER COLUMN push_notifications_enabled SET NOT NULL; EXCEPTION WHEN OTHERS THEN NULL; END;
    BEGIN ALTER TABLE alert_configuration ALTER COLUMN sms_notifications_enabled SET NOT NULL; EXCEPTION WHEN OTHERS THEN NULL; END;
END $$;

-- Message de confirmation
DO $$
BEGIN
    RAISE NOTICE '✅ Colonnes métier manquantes ajoutées';
END $$;
