-- =============================================================================
-- 001_map_layer_page_config.sql
-- Creates the junction table that controls which layers appear on which pages
-- and stores per-page overrides for editability, visibility, snap, etc.
--
-- Run after 000_map_layers_corrections.sql.
-- =============================================================================


CREATE TABLE public.map_layer_page_config (
    layer_id            integer NOT NULL REFERENCES public.map_layers(layer_id) ON DELETE CASCADE,
    page_context        text    NOT NULL,
    layer_editable      boolean NOT NULL DEFAULT false,
    layer_allow_create  boolean NOT NULL DEFAULT false,
    visible             boolean NOT NULL DEFAULT true,
    snap_to_this        boolean NOT NULL DEFAULT false,
    focus_on_load       boolean NOT NULL DEFAULT false,
    PRIMARY KEY (layer_id, page_context)
);

CREATE INDEX idx_mlpc_page ON public.map_layer_page_config(page_context);

COMMENT ON TABLE public.map_layer_page_config IS
    'Junction table: presence of a row means the layer is shown on that page. '
    'Column values are per-page overrides that take precedence over map_layers defaults.';

COMMENT ON COLUMN public.map_layer_page_config.page_context IS
    'Matches the page identifier used in JS calls: projectedit, accountedit, '
    'stocklistedit, wayleaveedit, opportunityedit, map';
