-- =============================================================================
-- 002_map_layer_page_config_seed.sql
-- Populates map_layer_page_config for:
--   1. The 'map' page context — seeded from existing map_layers column values
--      so map_v5.js behaviour is preserved exactly when it switches to the
--      page-filtered endpoint.
--   2. Reference layers on all five non-map page contexts (visible = false).
--   3. Base layers on all five non-map page contexts (visible inherited from
--      map_layers.visible as a reasonable default).
--
-- Run after 001_map_layer_page_config.sql.
-- Run 003_map_layer_page_config_vector.sql next for vector layers per page.
-- =============================================================================


-- -----------------------------------------------------------------------------
-- 1. 'map' page context — seed from existing map_layers columns
--    Excludes page-specific vector layers added by 000_map_layers_corrections.sql
--    which were not on the original map page and have per-project URLs.
-- -----------------------------------------------------------------------------

INSERT INTO public.map_layer_page_config
    (layer_id, page_context, layer_editable, layer_allow_create, visible, snap_to_this, focus_on_load)
SELECT
    layer_id,
    'map',
    layer_editable,
    layer_allow_create,
    visible,
    snap_to_this,
    focus_on_load
FROM public.map_layers
WHERE layer_active = true
  AND layer_name NOT IN (
    'projectNeighbours',
    'PlanEquipment',
    'PlanStructures',
    'PlanCables',
    'PlanDuct',
    'PlanSubDuct',
    'projectPIABlockages',
    'StocklistPremises',
    'StocklistProjectBoundary',
    'opportunityProjects',
    'opportunityRoutes',
    'vw_opportunity_network_points'
  )
ON CONFLICT DO NOTHING;


-- -----------------------------------------------------------------------------
-- 2. Reference layers — all five non-map page contexts
--    Visible = false: reference layers are opt-in toggles, off by default.
-- -----------------------------------------------------------------------------

INSERT INTO public.map_layer_page_config
    (layer_id, page_context, layer_editable, layer_allow_create, visible, snap_to_this, focus_on_load)
SELECT
    l.layer_id,
    p.page_context,
    false,
    false,
    false,
    false,
    false
FROM public.map_layers l
CROSS JOIN (VALUES
    ('projectedit'),
    ('accountedit'),
    ('stocklistedit'),
    ('wayleaveedit'),
    ('opportunityedit')
) AS p(page_context)
WHERE l.layer_category = 'reference'
  AND l.layer_active = true
ON CONFLICT DO NOTHING;


-- -----------------------------------------------------------------------------
-- 3. Base layers — all five non-map page contexts
--    Visible inherited from map_layers.visible (matches current JS defaults).
-- -----------------------------------------------------------------------------

INSERT INTO public.map_layer_page_config
    (layer_id, page_context, layer_editable, layer_allow_create, visible, snap_to_this, focus_on_load)
SELECT
    l.layer_id,
    p.page_context,
    false,
    false,
    l.visible,
    false,
    false
FROM public.map_layers l
CROSS JOIN (VALUES
    ('projectedit'),
    ('accountedit'),
    ('stocklistedit'),
    ('wayleaveedit'),
    ('opportunityedit')
) AS p(page_context)
WHERE l.layer_category = 'base'
  AND l.layer_active = true
ON CONFLICT DO NOTHING;


-- -----------------------------------------------------------------------------
-- Verify: row counts per page context after seeding
-- -----------------------------------------------------------------------------
SELECT page_context, COUNT(*) AS layer_count
FROM public.map_layer_page_config
GROUP BY page_context
ORDER BY page_context;
