-- 016_map_layer_allow_delete.sql
-- Restore the per-layer delete permission lost in the JSON→DB layer-config move
-- (docs/improvement-opportunities.md, Frontend / Assets, 2026-07-19).
--
-- The old hardcoded layer JSON in project_edit_v2.js carried a layerAllowDelete
-- flag per layer; the JS still gates its delete flow on it, but migrations
-- 001–004 (archived) never carried the flag into public.map_layer_page_config,
-- so delete has been silently disabled everywhere since the move.
--
-- Adds layer_allow_delete as a per-page override alongside layer_editable /
-- layer_allow_create, then seeds true for exactly the six projectedit layers
-- that had "layerAllowDelete": true in the pre-move JSON (commit 6937459^).
-- Every other row keeps the false default — delete is only intended on the
-- project editor (wayleave's polygon delete is hardcoded in its own JS and does
-- not go through this config).

ALTER TABLE public.map_layer_page_config
    ADD COLUMN layer_allow_delete boolean DEFAULT false NOT NULL;

COMMENT ON COLUMN public.map_layer_page_config.layer_allow_delete IS
    'Per-page flag: users may delete features from this layer on this page. Consumed as allowDelete by the editor JS.';

DO $$
DECLARE
    v_names   text[] := ARRAY[
        'PlanEquipment',
        'PlanStructures',
        'PlanCables',
        'PlanDuct',
        'PlanSubDuct',
        'projectPIABlockages'
    ];
    v_missing text;
    v_updated integer;
BEGIN
    -- All six layers must still exist under their pre-move names.
    SELECT string_agg(n, ', ') INTO v_missing
    FROM unnest(v_names) AS n
    WHERE NOT EXISTS (
        SELECT 1 FROM public.map_layers l WHERE l.layer_name = n
    );

    IF v_missing IS NOT NULL THEN
        RAISE EXCEPTION 'map_layers: no row(s) named % — layer renamed or removed since the JSON-era seed; resolve manually rather than editing this migration', v_missing;
    END IF;

    -- ...and each must have a projectedit page-config row to carry the flag.
    SELECT string_agg(l.layer_name, ', ') INTO v_missing
    FROM public.map_layers l
    WHERE l.layer_name = ANY (v_names)
      AND NOT EXISTS (
          SELECT 1 FROM public.map_layer_page_config p
          WHERE p.layer_id = l.layer_id AND p.page_context = 'projectedit'
      );

    IF v_missing IS NOT NULL THEN
        RAISE EXCEPTION 'map_layer_page_config: no projectedit row for % — layer(s) not configured for the project editor; resolve manually rather than editing this migration', v_missing;
    END IF;

    UPDATE public.map_layer_page_config p
    SET layer_allow_delete = true
    FROM public.map_layers l
    WHERE l.layer_id = p.layer_id
      AND p.page_context = 'projectedit'
      AND l.layer_name = ANY (v_names);

    GET DIAGNOSTICS v_updated = ROW_COUNT;

    IF v_updated <> array_length(v_names, 1) THEN
        RAISE EXCEPTION 'expected to seed % rows, updated % — duplicate or missing projectedit config rows', array_length(v_names, 1), v_updated;
    END IF;
END $$;
