-- ============================================================
-- 010: PostGIS GEOMETRY 컬럼 일괄 추가
-- 기존 NUMERIC LAT/LNG 유지 + GEOMETRY(Point, 4326) 추가
-- ============================================================

SET search_path TO wing, public;

-- PostGIS 익스텐션 확인
CREATE EXTENSION IF NOT EXISTS postgis;

-- ============================================================
-- 1. ASSET_ORG — 방제자산 기관 위치
-- ============================================================
ALTER TABLE ASSET_ORG ADD COLUMN IF NOT EXISTS GEOM GEOMETRY(Point, 4326);

UPDATE ASSET_ORG
SET GEOM = ST_SetSRID(ST_MakePoint(LNG, LAT), 4326)
WHERE LAT IS NOT NULL AND LNG IS NOT NULL AND GEOM IS NULL;

CREATE INDEX IF NOT EXISTS IDX_ASSET_ORG_GEOM ON ASSET_ORG USING GIST(GEOM);

-- ============================================================
-- 2. ACDNT — 사고 위치
-- ============================================================
ALTER TABLE ACDNT ADD COLUMN IF NOT EXISTS LOC_GEOM GEOMETRY(Point, 4326);

UPDATE ACDNT
SET LOC_GEOM = ST_SetSRID(ST_MakePoint(LNG, LAT), 4326)
WHERE LAT IS NOT NULL AND LNG IS NOT NULL AND LOC_GEOM IS NULL;

CREATE INDEX IF NOT EXISTS IDX_ACDNT_LOC_GEOM ON ACDNT USING GIST(LOC_GEOM);
