-- =============================================================================
-- 003_map_layer_page_config_vector.sql
-- Populates map_layer_page_config for all project-category vector layers.
--
-- Existing DB layer_ids are hardcoded directly.
-- Layers added by 000_map_layers_corrections.sql are looked up by layer_name
-- since their IDs are DB-assigned and not known in advance.
--
-- Run after 002_map_layer_page_config_seed.sql.
-- =============================================================================


-- -----------------------------------------------------------------------------
-- 1. projectedit
--    9 vector layers: ProjectBoundary, Meet Me Points, Project Neighbours,
--    and the 6 Plan + PIA Blockages editing layers.
-- -----------------------------------------------------------------------------

INSERT INTO public.map_layer_page_config
    (layer_id, page_context, layer_editable, layer_allow_create, visible, snap_to_this, focus_on_load)
VALUES

-- Project Boundary (existing DB record, layer_id = 18)
(18, 'projectedit', true,  false, true,  false, true),

-- Project Meet Me Points (existing DB record, layer_id = 15)
(15, 'projectedit', false, false, true,  false, false)

ON CONFLICT DO NOTHING;

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, 'projectedit', editable, allow_create, true, snap, false
FROM (VALUES
    ('projectNeighbours',   false, false, false),
    ('PlanEquipment',       true,  true,  true),
    ('PlanStructures',      true,  true,  true),
    ('PlanCables',          true,  true,  true),
    ('PlanDuct',            true,  true,  true),
    ('PlanSubDuct',         true,  true,  true),
    ('projectPIABlockages', true,  true,  false)
) AS v(lname, editable, allow_create, snap)
JOIN public.map_layers ON layer_name = lname
ON CONFLICT DO NOTHING;


-- -----------------------------------------------------------------------------
-- 2. accountedit
--    2 vector layers: Project Boundary (editable), Stocklist Premises (read).
-- -----------------------------------------------------------------------------

INSERT INTO public.map_layer_page_config
    (layer_id, page_context, layer_editable, layer_allow_create, visible, snap_to_this, focus_on_load)
VALUES
(18, 'accountedit', true, false, true, false, true)
ON CONFLICT DO NOTHING;

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, 'accountedit', false, false, true, false, false
FROM public.map_layers
WHERE layer_name = 'StocklistPremises'
ON CONFLICT DO NOTHING;


-- -----------------------------------------------------------------------------
-- 3. stocklistedit
--    2 vector layers: Stocklist Premises (focus on load), Stocklist Project
--    Boundary (separate record from projectedit ProjectBoundary — different
--    data source: stocklists.stocklist_projects_geom).
-- -----------------------------------------------------------------------------

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, 'stocklistedit', editable, false, true, false, fol
FROM (VALUES
    ('StocklistPremises',       false, true),
    ('StocklistProjectBoundary', false, false)
) AS v(lname, editable, fol)
JOIN public.map_layers ON layer_name = lname
ON CONFLICT DO NOTHING;


-- -----------------------------------------------------------------------------
-- 4. opportunityedit
--    3 vector layers: Opportunity Projects, Opportunity Routes,
--    Opportunity Equipment (all read-only).
-- -----------------------------------------------------------------------------

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, 'opportunityedit', false, false, true, false, fol
FROM (VALUES
    ('opportunityProjects',          true),
    ('opportunityRoutes',            true),
    ('vw_opportunity_network_points', false)
) AS v(lname, fol)
JOIN public.map_layers ON layer_name = lname
ON CONFLICT DO NOTHING;


-- -----------------------------------------------------------------------------
-- 5. wayleaveedit — 5 vector layers TBD
--    Layer names, geometry types, and editability to be confirmed before
--    inserting DB records. Populate this section once that work is done.
-- -----------------------------------------------------------------------------

-- 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, 'wayleaveedit', ...
-- FROM public.map_layers WHERE layer_name IN (...);


-- -----------------------------------------------------------------------------
-- Verify: all vector page config rows
-- -----------------------------------------------------------------------------
SELECT
    l.layer_name,
    l.layer_title,
    p.page_context,
    p.layer_editable,
    p.layer_allow_create,
    p.visible,
    p.snap_to_this,
    p.focus_on_load
FROM public.map_layer_page_config p
JOIN public.map_layers l ON l.layer_id = p.layer_id
WHERE l.layer_category = 'project'
ORDER BY p.page_context, l.layer_name;
