--


-- ZONAGE

CREATE OR REPLACE FUNCTION urba_plui_ecriture.tf_zoneurba()
RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
    v_editable boolean;
    v_id_version int;
BEGIN
    IF TG_OP = 'INSERT' THEN

        -- 1) Déduire la version depuis l'idurba (ex: M3_PH1_ZON_001_01)
        v_id_version := urba_plui_ecriture.func_id_version_depuis_idurba(NEW.idurba);

        -- 2) Recaler la FK version portée par l'objet
        NEW.lib_id_version := v_id_version;

        -- 3) Vérifier que la version est éditable
        SELECT s.editable
          INTO v_editable
          FROM urba_plui_ecriture.pluiv_versions v
          JOIN urba_plui_ecriture.pluiv_versionlibellestatut s
            ON v.statut = s.valeur
         WHERE v.id = NEW.lib_id_version;

        IF COALESCE(v_editable, false) IS NOT TRUE THEN
            RAISE EXCEPTION 'La version % (idurba=%) n''est pas éditable.', NEW.lib_id_version, NEW.idurba;
        END IF;

        PERFORM urba_plui_ecriture.qgis_creer_zoneurba(hstore(NEW));
        RETURN NEW;

    ELSIF TG_OP = 'UPDATE' THEN

        -- Option: si idurba change (copie / bascule), on recalcule la version
        IF NEW.idurba IS DISTINCT FROM OLD.idurba THEN
            NEW.lib_id_version := urba_plui_ecriture.func_id_version_depuis_idurba(NEW.idurba);
        END IF;

        -- Vérifier que la version cible est éditable
        SELECT s.editable
          INTO v_editable
          FROM urba_plui_ecriture.pluiv_versions v
          JOIN urba_plui_ecriture.pluiv_versionlibellestatut s
            ON v.statut = s.valeur
         WHERE v.id = NEW.lib_id_version;

        IF COALESCE(v_editable, false) IS NOT TRUE THEN
            RAISE EXCEPTION 'La version % (idurba=%) n''est pas éditable.', NEW.lib_id_version, NEW.idurba;
        END IF;

        PERFORM urba_plui_ecriture.qgis_modifier_zoneurba(hstore(OLD), hstore(NEW));
        RETURN NEW;

    ELSIF TG_OP = 'DELETE' THEN

        -- Vérifier que la version est éditable
        SELECT s.editable
          INTO v_editable
          FROM urba_plui_ecriture.pluiv_versions v
          JOIN urba_plui_ecriture.pluiv_versionlibellestatut s
            ON v.statut = s.valeur
         WHERE v.id = OLD.lib_id_version;

        IF COALESCE(v_editable, false) IS NOT TRUE THEN
            RAISE EXCEPTION 'La version % (idurba=%) n''est pas éditable.', OLD.lib_id_version, OLD.idurba;
        END IF;

        PERFORM urba_plui_ecriture.qgis_supprimer_zoneurba(hstore(OLD));
        RETURN OLD;
    END IF;

    -- Par sécurité (ne devrait pas arriver)
    RETURN NULL;
END;
$$;


-- INFORMATION

-- INFORMATION

CREATE OR REPLACE FUNCTION urba_plui_ecriture.tf_information()
RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
    v_editable boolean;
BEGIN
    IF TG_OP = 'INSERT' THEN

        -- Recaler la version cible depuis idurba (MAJ8_PH1_I02_001_01, etc.)
        NEW.lib_id_version := urba_plui_ecriture.func_id_version_depuis_idurba(NEW.idurba);

        -- Vérifier éditabilité via fonction existante
        SELECT urba_plui_ecriture.func_verifier_statut_modifiable(NEW.lib_id_version)
          INTO v_editable;

        IF COALESCE(v_editable,false) IS NOT TRUE THEN
            RAISE EXCEPTION 'La version % (idurba=%) n''est pas éditable.', NEW.lib_id_version, NEW.idurba;
        END IF;

        PERFORM urba_plui_ecriture.qgis_creer_information(hstore(NEW), TG_ARGV[0]);
        RETURN NEW;

    ELSIF TG_OP = 'UPDATE' THEN

        -- Option : si idurba change, on recalcule la version
        IF NEW.idurba IS DISTINCT FROM OLD.idurba THEN
            NEW.lib_id_version := urba_plui_ecriture.func_id_version_depuis_idurba(NEW.idurba);
        END IF;

        IF NOT (
            SELECT s.editable
            FROM urba_plui_ecriture.pluiv_versions v
            JOIN urba_plui_ecriture.pluiv_versionlibellestatut s ON v.statut = s.valeur
            WHERE v.id = NEW.lib_id_version
        )
        THEN
            RAISE EXCEPTION 'La version % (idurba=%) n''est pas éditable.', NEW.lib_id_version, NEW.idurba;
        END IF;

        PERFORM urba_plui_ecriture.qgis_modifier_information(hstore(OLD), hstore(NEW));
        RETURN NEW;

    ELSIF TG_OP = 'DELETE' THEN

        IF NOT (
            SELECT s.editable
            FROM urba_plui_ecriture.pluiv_versions v
            JOIN urba_plui_ecriture.pluiv_versionlibellestatut s ON v.statut = s.valeur
            WHERE v.id = OLD.lib_id_version
        )
        THEN
            RAISE EXCEPTION 'La version % (idurba=%) n''est pas éditable.', OLD.lib_id_version, OLD.idurba;
        END IF;

        PERFORM urba_plui_ecriture.qgis_supprimer_information(hstore(OLD));
        RETURN OLD;
    END IF;

    RETURN NULL;
END;
$$;
-- PRESCRIPTION

CREATE OR REPLACE FUNCTION urba_plui_ecriture.tf_prescription()
RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
    v_editable boolean;
BEGIN
    IF TG_OP = 'INSERT' THEN

        -- Recaler la version depuis idurba (ex: MAJ8_PH1_P12_001_01)
        NEW.lib_id_version := urba_plui_ecriture.func_id_version_depuis_idurba(NEW.idurba);

        -- Vérification du statut de la version à mettre à jour
        SELECT urba_plui_ecriture.func_verifier_statut_modifiable(NEW.lib_id_version)
          INTO v_editable;

        IF COALESCE(v_editable,false) IS NOT TRUE THEN
            RAISE EXCEPTION 'La version % (idurba=%) n''est pas éditable.', NEW.lib_id_version, NEW.idurba;
        END IF;

        PERFORM urba_plui_ecriture.qgis_creer_prescription(hstore(NEW), TG_ARGV[0]);
        RETURN NEW;

    ELSIF TG_OP = 'UPDATE' THEN

        -- Si idurba change, on recalcule la version
        IF NEW.idurba IS DISTINCT FROM OLD.idurba THEN
            NEW.lib_id_version := urba_plui_ecriture.func_id_version_depuis_idurba(NEW.idurba);
        END IF;

        IF NOT (
            SELECT s.editable
            FROM urba_plui_ecriture.pluiv_versions v
            JOIN urba_plui_ecriture.pluiv_versionlibellestatut s ON v.statut = s.valeur
            WHERE v.id = NEW.lib_id_version
        )
        THEN
            RAISE EXCEPTION 'La version % (idurba=%) n''est pas éditable.', NEW.lib_id_version, NEW.idurba;
        END IF;

        PERFORM urba_plui_ecriture.qgis_modifier_prescription(hstore(OLD), hstore(NEW));
        RETURN NEW;

    ELSIF TG_OP = 'DELETE' THEN

        IF NOT (
            SELECT s.editable
            FROM urba_plui_ecriture.pluiv_versions v
            JOIN urba_plui_ecriture.pluiv_versionlibellestatut s ON v.statut = s.valeur
            WHERE v.id = OLD.lib_id_version
        )
        THEN
            RAISE EXCEPTION 'La version % (idurba=%) n''est pas éditable.', OLD.lib_id_version, OLD.idurba;
        END IF;

        PERFORM urba_plui_ecriture.qgis_supprimer_prescription(hstore(OLD));
        RETURN OLD;
    END IF;

    RETURN NULL;
END;
$$;

-- HABILLAGE

CREATE OR REPLACE FUNCTION urba_plui_ecriture.tf_habillage()
RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
    IF TG_OP = 'INSERT' THEN

        -- Recaler la version depuis idurba (ex: MAJ8_PH1_HAB_001_01)
        NEW.lib_id_version := urba_plui_ecriture.func_id_version_depuis_idurba(NEW.idurba);

        -- Vérification du statut de la version à mettre à jour
        IF NOT (
            SELECT s.editable
            FROM urba_plui_ecriture.pluiv_versions v
            JOIN urba_plui_ecriture.pluiv_versionlibellestatut s ON v.statut = s.valeur
            WHERE v.id = NEW.lib_id_version
        )
        THEN
            RAISE EXCEPTION 'La version % (idurba=%) n''est pas éditable.', NEW.lib_id_version, NEW.idurba;
        END IF;

        PERFORM urba_plui_ecriture.qgis_creer_habillage(hstore(NEW), TG_ARGV[0]);
        RETURN NEW;

    ELSIF TG_OP = 'UPDATE' THEN

        -- Si idurba change, on recalcule la version
        IF NEW.idurba IS DISTINCT FROM OLD.idurba THEN
            NEW.lib_id_version := urba_plui_ecriture.func_id_version_depuis_idurba(NEW.idurba);
        END IF;

        IF NOT (
            SELECT s.editable
            FROM urba_plui_ecriture.pluiv_versions v
            JOIN urba_plui_ecriture.pluiv_versionlibellestatut s ON v.statut = s.valeur
            WHERE v.id = NEW.lib_id_version
        )
        THEN
            RAISE EXCEPTION 'La version % (idurba=%) n''est pas éditable.', NEW.lib_id_version, NEW.idurba;
        END IF;

        PERFORM urba_plui_ecriture.qgis_modifier_habillage(hstore(OLD), hstore(NEW));
        RETURN NEW;

    ELSIF TG_OP = 'DELETE' THEN

        IF NOT (
            SELECT s.editable
            FROM urba_plui_ecriture.pluiv_versions v
            JOIN urba_plui_ecriture.pluiv_versionlibellestatut s ON v.statut = s.valeur
            WHERE v.id = OLD.lib_id_version
        )
        THEN
            RAISE EXCEPTION 'La version % (idurba=%) n''est pas éditable.', OLD.lib_id_version, OLD.idurba;
        END IF;

        PERFORM urba_plui_ecriture.qgis_supprimer_habillage(hstore(OLD));
        RETURN OLD;
    END IF;

    RETURN NULL;
END;
$$;
