-- =============================================
-- 05_triggers.sql : Timestamps automatiques
-- - Ajoute (si absent) cree_le DEFAULT now() et modifie_le DEFAULT now()
-- - Crée un trigger BEFORE UPDATE pour mettre à jour modifie_le
-- - Idempotent (DROP TRIGGER IF EXISTS / IF NOT EXISTS)
-- A exécuter après 01_init.sql, 02_indexes.sql, 03_checks.sql
-- =============================================

-- 1) Fonction unique de mise à jour du timestamp
CREATE OR REPLACE FUNCTION set_modifie_le()
RETURNS TRIGGER AS $$
BEGIN
  NEW.modifie_le := NOW();
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

-- Helper macro-like: pour chaque table, s'assurer des colonnes & trigger
-- (on ne peut pas faire de macro, donc on répète pour chaque table)

-- Liste des tables concernées :
-- utilisateurs, assistantes_maternelles, parents, enfants, enfants_parents,
-- dossiers, messages, contrats, avenants_contrats, evenements,
-- signalements_bugs, uploads, notifications, validations

-- ========== UTILISATEURS
ALTER TABLE utilisateurs
  ADD COLUMN IF NOT EXISTS cree_le     TIMESTAMP DEFAULT NOW(),
  ADD COLUMN IF NOT EXISTS modifie_le  TIMESTAMP DEFAULT NOW();
DROP TRIGGER IF EXISTS trg_utilisateurs_set_modifie_le ON utilisateurs;
CREATE TRIGGER trg_utilisateurs_set_modifie_le
BEFORE UPDATE ON utilisateurs
FOR EACH ROW EXECUTE FUNCTION set_modifie_le();

-- ========== ASSISTANTES_MATERNELLES
ALTER TABLE assistantes_maternelles
  ADD COLUMN IF NOT EXISTS cree_le     TIMESTAMP DEFAULT NOW(),
  ADD COLUMN IF NOT EXISTS modifie_le  TIMESTAMP DEFAULT NOW();
DROP TRIGGER IF EXISTS trg_am_set_modifie_le ON assistantes_maternelles;
CREATE TRIGGER trg_am_set_modifie_le
BEFORE UPDATE ON assistantes_maternelles
FOR EACH ROW EXECUTE FUNCTION set_modifie_le();

-- ========== PARENTS
ALTER TABLE parents
  ADD COLUMN IF NOT EXISTS cree_le     TIMESTAMP DEFAULT NOW(),
  ADD COLUMN IF NOT EXISTS modifie_le  TIMESTAMP DEFAULT NOW();
DROP TRIGGER IF EXISTS trg_parents_set_modifie_le ON parents;
CREATE TRIGGER trg_parents_set_modifie_le
BEFORE UPDATE ON parents
FOR EACH ROW EXECUTE FUNCTION set_modifie_le();

-- ========== ENFANTS
ALTER TABLE enfants
  ADD COLUMN IF NOT EXISTS cree_le     TIMESTAMP DEFAULT NOW(),
  ADD COLUMN IF NOT EXISTS modifie_le  TIMESTAMP DEFAULT NOW();
DROP TRIGGER IF EXISTS trg_enfants_set_modifie_le ON enfants;
CREATE TRIGGER trg_enfants_set_modifie_le
BEFORE UPDATE ON enfants
FOR EACH ROW EXECUTE FUNCTION set_modifie_le();

-- ========== ENFANTS_PARENTS (table de liaison)
ALTER TABLE enfants_parents
  ADD COLUMN IF NOT EXISTS cree_le     TIMESTAMP DEFAULT NOW(),
  ADD COLUMN IF NOT EXISTS modifie_le  TIMESTAMP DEFAULT NOW();
DROP TRIGGER IF EXISTS trg_enfants_parents_set_modifie_le ON enfants_parents;
CREATE TRIGGER trg_enfants_parents_set_modifie_le
BEFORE UPDATE ON enfants_parents
FOR EACH ROW EXECUTE FUNCTION set_modifie_le();

-- ========== DOSSIERS
ALTER TABLE dossiers
  ADD COLUMN IF NOT EXISTS cree_le     TIMESTAMP DEFAULT NOW(),
  ADD COLUMN IF NOT EXISTS modifie_le  TIMESTAMP DEFAULT NOW();
DROP TRIGGER IF EXISTS trg_dossiers_set_modifie_le ON dossiers;
CREATE TRIGGER trg_dossiers_set_modifie_le
BEFORE UPDATE ON dossiers
FOR EACH ROW EXECUTE FUNCTION set_modifie_le();

-- ========== MESSAGES
ALTER TABLE messages
  ADD COLUMN IF NOT EXISTS cree_le     TIMESTAMP DEFAULT NOW(),
  ADD COLUMN IF NOT EXISTS modifie_le  TIMESTAMP DEFAULT NOW();
DROP TRIGGER IF EXISTS trg_messages_set_modifie_le ON messages;
CREATE TRIGGER trg_messages_set_modifie_le
BEFORE UPDATE ON messages
FOR EACH ROW EXECUTE FUNCTION set_modifie_le();

-- ========== CONTRATS
ALTER TABLE contrats
  ADD COLUMN IF NOT EXISTS cree_le     TIMESTAMP DEFAULT NOW(),
  ADD COLUMN IF NOT EXISTS modifie_le  TIMESTAMP DEFAULT NOW();
DROP TRIGGER IF EXISTS trg_contrats_set_modifie_le ON contrats;
CREATE TRIGGER trg_contrats_set_modifie_le
BEFORE UPDATE ON contrats
FOR EACH ROW EXECUTE FUNCTION set_modifie_le();

-- ========== AVENANTS_CONTRATS
ALTER TABLE avenants_contrats
  ADD COLUMN IF NOT EXISTS cree_le     TIMESTAMP DEFAULT NOW(),
  ADD COLUMN IF NOT EXISTS modifie_le  TIMESTAMP DEFAULT NOW();
DROP TRIGGER IF EXISTS trg_avenants_contrats_set_modifie_le ON avenants_contrats;
CREATE TRIGGER trg_avenants_contrats_set_modifie_le
BEFORE UPDATE ON avenants_contrats
FOR EACH ROW EXECUTE FUNCTION set_modifie_le();

-- ========== EVENEMENTS
ALTER TABLE evenements
  ADD COLUMN IF NOT EXISTS cree_le     TIMESTAMP DEFAULT NOW(),
  ADD COLUMN IF NOT EXISTS modifie_le  TIMESTAMP DEFAULT NOW();
DROP TRIGGER IF EXISTS trg_evenements_set_modifie_le ON evenements;
CREATE TRIGGER trg_evenements_set_modifie_le
BEFORE UPDATE ON evenements
FOR EACH ROW EXECUTE FUNCTION set_modifie_le();

-- ========== SIGNALEMENTS_BUGS
ALTER TABLE signalements_bugs
  ADD COLUMN IF NOT EXISTS cree_le     TIMESTAMP DEFAULT NOW(),
  ADD COLUMN IF NOT EXISTS modifie_le  TIMESTAMP DEFAULT NOW();
DROP TRIGGER IF EXISTS trg_signalements_bugs_set_modifie_le ON signalements_bugs;
CREATE TRIGGER trg_signalements_bugs_set_modifie_le
BEFORE UPDATE ON signalements_bugs
FOR EACH ROW EXECUTE FUNCTION set_modifie_le();

-- ========== UPLOADS
ALTER TABLE uploads
  ADD COLUMN IF NOT EXISTS cree_le     TIMESTAMP DEFAULT NOW(),
  ADD COLUMN IF NOT EXISTS modifie_le  TIMESTAMP DEFAULT NOW();
DROP TRIGGER IF EXISTS trg_uploads_set_modifie_le ON uploads;
CREATE TRIGGER trg_uploads_set_modifie_le
BEFORE UPDATE ON uploads
FOR EACH ROW EXECUTE FUNCTION set_modifie_le();

-- ========== NOTIFICATIONS
ALTER TABLE notifications
  ADD COLUMN IF NOT EXISTS cree_le     TIMESTAMP DEFAULT NOW(),
  ADD COLUMN IF NOT EXISTS modifie_le  TIMESTAMP DEFAULT NOW();
DROP TRIGGER IF EXISTS trg_notifications_set_modifie_le ON notifications;
CREATE TRIGGER trg_notifications_set_modifie_le
BEFORE UPDATE ON notifications
FOR EACH ROW EXECUTE FUNCTION set_modifie_le();

-- ========== VALIDATIONS
ALTER TABLE validations
  ADD COLUMN IF NOT EXISTS cree_le     TIMESTAMP DEFAULT NOW(),
  ADD COLUMN IF NOT EXISTS modifie_le  TIMESTAMP DEFAULT NOW();
DROP TRIGGER IF EXISTS trg_validations_set_modifie_le ON validations;
CREATE TRIGGER trg_validations_set_modifie_le
BEFORE UPDATE ON validations
FOR EACH ROW EXECUTE FUNCTION set_modifie_le();
