-- V29: Table de persistance de la configuration système (maintenance_mode, scheduled_maintenance, etc.)
-- Remplace le stockage en RAM (AtomicReference) pour les paramètres critiques.

CREATE TABLE IF NOT EXISTS system_config (
    id                  UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    date_creation       TIMESTAMP NOT NULL DEFAULT NOW(),
    date_modification   TIMESTAMP,
    cree_par            VARCHAR(255),
    modifie_par         VARCHAR(255),
    version             BIGINT DEFAULT 0,
    actif               BOOLEAN NOT NULL DEFAULT TRUE,
    config_key          VARCHAR(100) NOT NULL,
    config_value        TEXT,
    CONSTRAINT uk_system_config_key UNIQUE (config_key)
);

CREATE INDEX IF NOT EXISTS idx_system_config_key ON system_config (config_key);

-- Valeurs initiales
INSERT INTO system_config (config_key, config_value, cree_par) VALUES
    ('maintenance_mode',              'false',            'SYSTEM'),
    ('maintenance_emergency',         'false',            'SYSTEM'),
    ('scheduled_maintenance_at',      NULL,               'SYSTEM'),
    ('scheduled_maintenance_reason',  NULL,               'SYSTEM'),
    ('scheduled_maintenance_status',  'NONE',             'SYSTEM')
ON CONFLICT (config_key) DO NOTHING;
