-- 051_wayleave_polygon_layer_raw_geom.sql
-- Serve the editable wayleave boundary layer as stored.
--
-- map_get_v2.php reads every layer through ST_MakeValid. For a display layer that is the
-- right answer: an invalid boundary is shown untangled rather than as a rendering
-- artefact. For an EDITABLE layer it is not, and the difference is not cosmetic.
--
-- ST_MakeValid resolves a self-intersecting outline by splitting it into separate polygons
-- meeting at the crossover. So the editor was handed a shape that is not the one in the
-- table: drag a vertex of that rendition, save, and the original ring is overwritten by
-- the repaired one. A boundary drawn with one stray crossing could never be untied - the
-- crossover had become a real vertex of two separate parts, with no self-intersection left
-- to undo (Dave, 2026-08-14).
--
-- map_get_v2.php now takes raw_geom=1 to skip the repair (and st_forcerhr with it - ring
-- orientation is presentation too, and an editor should round-trip what it was given).
-- This sets it on the one layer that is edited. Every other layer in every module keeps
-- the repair, which is why this is a per-layer flag and not a change to the shared default.
--
-- Belt and braces on the WHERE: the relation is stored three times on a map_layers row -
-- database_schema, database_table and again inside the url string - and only the url is
-- being changed here, so the other two identify the row.

DO $$
DECLARE
    v_n   integer;
    v_url text;
BEGIN
    SELECT url INTO v_url
      FROM public.map_layers
     WHERE database_schema = 'wayleave'
       AND database_table  = 'vw_wayleave_polygons';

    IF v_url IS NULL THEN
        RAISE EXCEPTION '051: no map_layers row for wayleave.vw_wayleave_polygons';
    END IF;

    -- Idempotent: re-running must not append the flag twice.
    IF v_url LIKE '%raw_geom=1%' THEN
        RAISE NOTICE '051: raw_geom already set, nothing to do';
        RETURN;
    END IF;

    UPDATE public.map_layers
       SET url = url || '&raw_geom=1'
     WHERE database_schema = 'wayleave'
       AND database_table  = 'vw_wayleave_polygons';
    GET DIAGNOSTICS v_n = ROW_COUNT;

    -- 025's map-layer UPDATE matched nothing and reported success, which is how two
    -- opportunity layers ended up permanently empty. Every statement asserts its count.
    IF v_n <> 1 THEN
        RAISE EXCEPTION '051: url update matched % row(s), expected 1', v_n;
    END IF;
END $$;

-- ── Verify ──────────────────────────────────────────────────────────────────

DO $$
DECLARE
    v_url text;
BEGIN
    SELECT url INTO v_url
      FROM public.map_layers
     WHERE database_schema = 'wayleave'
       AND database_table  = 'vw_wayleave_polygons';

    IF v_url NOT LIKE '%raw_geom=1%' THEN
        RAISE EXCEPTION '051: raw_geom=1 not present on the layer url: %', v_url;
    END IF;

    -- The flag is worthless if the url stopped pointing at the right relation.
    IF v_url NOT LIKE '%wayleave.vw_wayleave_polygons%' THEN
        RAISE EXCEPTION '051: layer url no longer names the polygon view: %', v_url;
    END IF;

    -- No other wayleave layer should have picked it up: the rest are display-only.
    IF EXISTS (SELECT 1 FROM public.map_layers
                WHERE database_schema = 'wayleave'
                  AND database_table <> 'vw_wayleave_polygons'
                  AND url LIKE '%raw_geom=1%') THEN
        RAISE EXCEPTION '051: raw_geom set on a display-only layer';
    END IF;
END $$;
