-- Supprimer les triggers existants
DROP TRIGGER IF EXISTS before_insert_users;
DROP TRIGGER IF EXISTS before_update_users;

-- Trigger BEFORE INSERT
DELIMITER //

CREATE TRIGGER before_insert_users
BEFORE INSERT ON users
FOR EACH ROW
BEGIN
    IF NEW.is_personnel = 1 OR NEW.personnel_catg = 2 THEN
        SET NEW.is_absences_agent = 1;
        SET NEW.is_historiqueabs_agent = 1;
        SET NEW.is_pointage_agent = 1;
        SET NEW.is_archives_agent = 0;
        SET NEW.is_renseignm_agent = 1;
        SET NEW.is_charges_agent = 0;
        SET NEW.is_demandedocs_agent = 1;
        SET NEW.is_assiduite_agent = 1;
        SET NEW.is_affectation_agent = 1;
        SET NEW.is_appels_agent = 1;
    END IF;
END;
//

DELIMITER ;

-- Trigger BEFORE UPDATE
DELIMITER //

CREATE TRIGGER before_update_users
BEFORE UPDATE ON users
FOR EACH ROW
BEGIN
    IF NEW.is_personnel = 1 OR NEW.personnel_catg = 2 THEN
        SET NEW.is_absences_agent = 1;
        SET NEW.is_historiqueabs_agent = 1;
        SET NEW.is_pointage_agent = 1;
        SET NEW.is_archives_agent = 0;
        SET NEW.is_renseignm_agent = 1;
        SET NEW.is_charges_agent = 0;
        SET NEW.is_demandedocs_agent = 1;
        SET NEW.is_assiduite_agent = 1;
        SET NEW.is_affectation_agent = 1;
        SET NEW.is_appels_agent = 1;   
    END IF;
END;
//

DELIMITER ;

-- Mise à jour des enregistrements existants
UPDATE users
SET
    is_absences_agent = 1,
    is_historiqueabs_agent = 1,
    is_pointage_agent = 1,
    is_archives_agent = 0,
    is_renseignm_agent = 1,
    is_charges_agent = 0,
    is_demandedocs_agent = 1,
    is_assiduite_agent = 1,
    is_affectation_agent = 1,
    is_appels_agent = 1
WHERE
    is_personnel = 1 OR personnel_catg = 2;
