-- 103_seed_prinzip_mappings.sql
-- Audit 2026-05-17: bulk insert missing PRINZIP project mappings
-- into objective_complex_mapping so mv_layout_velocity picks them up.
--
-- Problem: objective_corpus_room_month had active deals for these projects
-- but no row in objective_complex_mapping → mv_layout_velocity JOIN skipped them.
--
-- Audit source: audits/SiteFinder_FixList_May17.md #4
--
-- UNIQUE constraint on table: (objective_complex_name, objective_group)
-- MV join key: objective_complex_mapping.objective_complex_name = objective_corpus_room_month.project_name
-- One domrf_obj_id per project_name; mapped to the largest/most active corpus.
--
-- Skipped:
--   Парк Победы   — already mapped to obj_id=60160 (Строящиеся, PRINZIP)
--   Парковый квартал — 0 deals/12m, inactive
--   Первый        — belongs to ГК ИКАР, NOT PRINZIP; name collision, not a real match
--
-- After applying this migration run manually (post-deploy):
--   REFRESH MATERIALIZED VIEW CONCURRENTLY mv_layout_velocity;
-- See runbook: runbooks/Refresh_MV_Layout_Velocity.md
--
-- Dependencies: objective_complex_mapping (exists), mv_layout_velocity (refresh separately)
-- Deploy: auto-applied by deploy.yml via _schema_migrations tracking.
-- Idempotent: ON CONFLICT (objective_complex_name, objective_group) DO NOTHING

BEGIN;

INSERT INTO objective_complex_mapping
    (objective_complex_name, objective_group, domrf_obj_id,
     match_method, is_reviewed, note)
VALUES
    -- 98 deals/12m in Objective.
    -- obj_id 64701 = ЖК "Малевич" (Строящиеся, 244 кв, сдача 2027-09, дев: СЗ МАЛЕВИЧ).
    -- obj_id 3687  = ЖК "Малевич" (Сданный, 348 кв, сдан 2020-03, дев: PRINZIP) — исторический.
    -- 64701 выбран как активный primary corpus.
    ('Малевич', 'Екатеринбург', 64701, 'manual', true,
     'Audit 2026-05-17: Строящиеся 244кв сдача 2027-09; obj_id 3687 — сданный исторический'),

    -- 30 deals/12m in Objective.
    -- Корпуса: obj_id 47390 (411кв Сданные), 37880 (157кв), 47393 (86кв), 47392 (0кв skip).
    -- 47390 выбран как крупнейший corpus.
    ('Ньютон парк', 'Екатеринбург', 47390, 'manual', true,
     'Audit 2026-05-17: largest corpus 411кв из трёх Ньютон Парк корпусов')

ON CONFLICT (objective_complex_name, objective_group) DO NOTHING;

COMMIT;
