-- 킨텍스 — 홀 마스터 시드 (멱등, PLANNING §7-1 / floorplans README 실측치)
-- 재실행 안전: ON CONFLICT DO UPDATE 로 최신 규격 갱신.

INSERT INTO hall (id, exhibition_center, label, width_m, depth_m, height_m, area_m2,
                  floor_load_t_per_m2, floor_type, booth_capacity, has_gas, is_assumed_trench)
VALUES
  -- 제1전시장 홀1~5: 63×171×15m, 5t/㎡, 콘크리트 폴리싱, 약 600부스
  ('H1', 1, '제1전시장 홀1', 63, 171, 15, 10611, 5.0, 'concrete_polished', 600, true,  true),
  ('H2', 1, '제1전시장 홀2', 63, 171, 15, 10773, 5.0, 'concrete_polished', 600, false, true),
  ('H3', 1, '제1전시장 홀3', 63, 171, 15, 10773, 5.0, 'concrete_polished', 600, false, true),
  ('H4', 1, '제1전시장 홀4', 63, 171, 15, 10773, 5.0, 'concrete_polished', 600, false, true),
  ('H5', 1, '제1전시장 홀5', 63, 171, 15, 10611, 5.0, 'concrete_polished', 600, false, true),
  -- 옥외전시장 2,849㎡ (56×52)
  ('OUTDOOR', 1, '옥외전시장', 56, 52, NULL, 2849, 5.0, 'outdoor', NULL, false, true),
  -- 제2전시장 홀6(이벤트홀): 93×60×10m, 2t/㎡, 카펫, 200부스
  ('H6', 2, '제2전시장 홀6', 93, 60, 10, 5580, 2.0, 'carpet', 200, false, true),
  -- 홀7/8: 126×90×12m, 5t/㎡, 510부스 (홀7 가스)
  ('H7', 2, '제2전시장 홀7', 126, 90, 12, 11290, 5.0, 'concrete_polished', 510, true,  true),
  ('H8', 2, '제2전시장 홀8', 126, 90, 12, 11290, 5.0, 'concrete_polished', 510, false, true),
  -- 홀9/10: 132×99×15m, 5t/㎡, 550부스
  ('H9',  2, '제2전시장 홀9',  132, 99, 15, 13238, 5.0, 'concrete_polished', 550, false, true),
  ('H10', 2, '제2전시장 홀10', 132, 99, 15, 13072, 5.0, 'concrete_polished', 550, false, true)
ON CONFLICT (id) DO UPDATE SET
    exhibition_center   = EXCLUDED.exhibition_center,
    label               = EXCLUDED.label,
    width_m             = EXCLUDED.width_m,
    depth_m             = EXCLUDED.depth_m,
    height_m            = EXCLUDED.height_m,
    area_m2             = EXCLUDED.area_m2,
    floor_load_t_per_m2 = EXCLUDED.floor_load_t_per_m2,
    floor_type          = EXCLUDED.floor_type,
    booth_capacity      = EXCLUDED.booth_capacity,
    has_gas             = EXCLUDED.has_gas,
    is_assumed_trench   = EXCLUDED.is_assumed_trench;

-- 비상구(가정): 사각형 홀 각 변 중앙 4개소, 이격 3m. CAD 실측 추출 시 교체(TODO).
INSERT INTO hall_exit (id, hall_id, geom, clearance_m, is_assumed)
SELECT h.id || '-EXIT-' || g.tag,
       h.id,
       ST_SetSRID(ST_MakePoint(g.x, g.y), 0),
       3.0,
       true
FROM hall h
CROSS JOIN LATERAL (VALUES
    ('S', h.width_m / 2.0, 0.0),
    ('N', h.width_m / 2.0, h.depth_m),
    ('W', 0.0,             h.depth_m / 2.0),
    ('E', h.width_m,       h.depth_m / 2.0)
) AS g(tag, x, y)
WHERE h.id <> 'OUTDOOR' AND h.width_m IS NOT NULL AND h.depth_m IS NOT NULL
ON CONFLICT (id) DO UPDATE SET
    geom        = EXCLUDED.geom,
    clearance_m = EXCLUDED.clearance_m,
    is_assumed  = EXCLUDED.is_assumed;
