-- ============================================================
-- 013_hns_analysis.sql
-- HNS 대기확산 분석 테이블 + 시드 데이터
-- ============================================================

SET search_path TO wing, public;

CREATE TABLE IF NOT EXISTS HNS_ANALYSIS (
  HNS_ANLYS_SN     SERIAL PRIMARY KEY,
  ACDNT_SN         INTEGER REFERENCES ACDNT(ACDNT_SN),
  ANLYS_NM         VARCHAR(200) NOT NULL,
  ACDNT_DTM        TIMESTAMPTZ,
  LOC_NM           VARCHAR(200),
  -- 위치 (표준: LON/LAT/GEOM)
  LON              NUMERIC(10,6),
  LAT              NUMERIC(9,6),
  GEOM             GEOMETRY(Point, 4326),
  LOC_DC           VARCHAR(100),
  -- 물질 정보
  SBST_SN          INTEGER,
  SBST_NM          VARCHAR(100),
  UN_NO            VARCHAR(10),
  CAS_NO           VARCHAR(20),
  SPIL_QTY         NUMERIC(14,10),
  SPIL_UNIT_CD     VARCHAR(10) DEFAULT 'KL',
  SPIL_TP_CD       VARCHAR(20),
  FCST_HR          INTEGER,
  ALGO_CD          VARCHAR(20),
  CRIT_MDL_CD      VARCHAR(10),
  -- 기상 조건
  WIND_SPD         NUMERIC(5,1),
  WIND_DIR         VARCHAR(10),
  TEMP             NUMERIC(4,1),
  HUMID            NUMERIC(4,1),
  ATM_STBL_CD      VARCHAR(10),
  -- 실행 상태
  EXEC_STTS_CD     VARCHAR(20) DEFAULT 'COMPLETED',
  RISK_CD          VARCHAR(20),
  ANALYST_NM       VARCHAR(50),
  -- 결과
  RSLT_DATA        JSONB,
  USE_YN           CHAR(1) DEFAULT 'Y',
  REG_DTM          TIMESTAMPTZ DEFAULT NOW(),
  MDFCN_DTM        TIMESTAMPTZ DEFAULT NOW(),
  CONSTRAINT CK_HNS_STTS CHECK (EXEC_STTS_CD IN ('PENDING','RUNNING','COMPLETED','FAILED'))
);

-- ============================================================
-- HNS_ANALYSIS 시드 데이터 (8건 — 기존 mock 데이터 기반)
-- ============================================================

INSERT INTO HNS_ANALYSIS (
  ANLYS_NM, ACDNT_DTM, LOC_NM, LON, LAT, GEOM, LOC_DC,
  SBST_NM, SPIL_QTY, SPIL_UNIT_CD, FCST_HR, ALGO_CD, CRIT_MDL_CD,
  WIND_SPD, WIND_DIR, TEMP, HUMID, ATM_STBL_CD,
  EXEC_STTS_CD, RISK_CD, ANALYST_NM,
  RSLT_DATA, REG_DTM
) VALUES
(
  '울산 온산항 톨루엔 누출', '2025-02-11 14:15'::timestamptz,
  '부산항 신항', 129.067, 35.078, ST_SetSRID(ST_MakePoint(129.067, 35.078), 4326), '129.067 + 35.078',
  '톨루엔 (Toluene)', 12.0, 'KL', 24, 'ALOHA', 'AEGL',
  5.2, 'SW', 18.5, 65, 'D',
  'COMPLETED', 'HIGH', '운영팀, 방재팀',
  '{"aegl3": true, "aegl2": true, "aegl1": true, "damageRadius": "1.8 km", "zones": [{"level":"AEGL-3","radius":500},{"level":"AEGL-2","radius":1000},{"level":"AEGL-1","radius":1500}]}'::jsonb,
  '2025-02-11 14:15'::timestamptz
),
(
  '여수 엠프시아 누출', '2025-02-09 08:40'::timestamptz,
  '여수항', 127.662, 34.740, ST_SetSRID(ST_MakePoint(127.662, 34.740), 4326), '127.662 + 34.740',
  '벤젠 (Benzene)', 5.0, 'TON', 12, 'ALOHA', 'AEGL',
  4.1, 'NE', 12.3, 58, 'C',
  'COMPLETED', 'HIGH', '남해팀, 방재팀',
  '{"aegl3": true, "aegl2": true, "aegl1": true, "damageRadius": "2.4 km", "zones": [{"level":"AEGL-3","radius":600},{"level":"AEGL-2","radius":1200},{"level":"AEGL-1","radius":1800}]}'::jsonb,
  '2025-02-09 08:40'::timestamptz
),
(
  '부산 수소 추진연 폭발', '2025-02-07 12:15'::timestamptz,
  '부산항', 129.043, 35.097, ST_SetSRID(ST_MakePoint(129.043, 35.097), 4326), '129.043 + 35.097',
  '수소 (Hydrogen)', 0.8, 'TON', 6, 'CAMEO', 'AEGL',
  6.5, 'W', 15.0, 55, 'D',
  'COMPLETED', 'CRITICAL', '남해팀, 방재팀',
  '{"aegl3": true, "aegl2": true, "aegl1": true, "damageRadius": "0.22 km", "zones": [{"level":"AEGL-3","radius":100},{"level":"AEGL-2","radius":150},{"level":"AEGL-1","radius":220}]}'::jsonb,
  '2025-02-07 12:15'::timestamptz
),
(
  '인천항 메탄올 유출', '2025-02-03 16:50'::timestamptz,
  '인천항', 126.598, 37.449, ST_SetSRID(ST_MakePoint(126.598, 37.449), 4326), '126.598 + 37.449',
  '메탄올 (Methanol)', 8.5, 'KL', 24, 'ALOHA', 'AEGL',
  3.8, 'SE', 8.2, 72, 'E',
  'COMPLETED', 'MEDIUM', '중부팀, 방재팀',
  '{"aegl3": true, "aegl2": true, "aegl1": true, "damageRadius": "1.2 km", "zones": [{"level":"AEGL-3","radius":300},{"level":"AEGL-2","radius":700},{"level":"AEGL-1","radius":1200}]}'::jsonb,
  '2025-02-03 16:50'::timestamptz
),
(
  '평택항 LPG 누출', '2025-01-28 09:20'::timestamptz,
  '평택항', 126.822, 36.969, ST_SetSRID(ST_MakePoint(126.822, 36.969), 4326), '126.822 + 36.969',
  'LPG', 3.2, 'TON', 12, 'CAMEO', 'AEGL',
  4.5, 'N', 5.5, 68, 'D',
  'COMPLETED', 'MEDIUM', '중부팀, 방재팀',
  '{"aegl3": true, "aegl2": true, "aegl1": true, "damageRadius": "0.95 km", "zones": [{"level":"AEGL-3","radius":250},{"level":"AEGL-2","radius":550},{"level":"AEGL-1","radius":950}]}'::jsonb,
  '2025-01-28 09:20'::timestamptz
),
(
  '광양항 벤젠 누출', '2025-01-22 11:30'::timestamptz,
  '광양항', 127.736, 34.930, ST_SetSRID(ST_MakePoint(127.736, 34.930), 4326), '127.736 + 34.930',
  '벤젠 (Benzene)', 6.0, 'KL', 24, 'ALOHA', 'AEGL',
  3.2, 'S', 10.1, 60, 'C',
  'COMPLETED', 'LOW', '남해팀, 방재팀',
  '{"aegl3": true, "aegl2": true, "aegl1": true, "damageRadius": "1.5 km", "zones": [{"level":"AEGL-3","radius":400},{"level":"AEGL-2","radius":800},{"level":"AEGL-1","radius":1500}]}'::jsonb,
  '2025-01-22 11:30'::timestamptz
),
(
  '목포항 염소스가 유출', '2025-01-15 14:10'::timestamptz,
  '서구항', 126.392, 34.793, ST_SetSRID(ST_MakePoint(126.392, 34.793), 4326), '126.392 + 34.793',
  '염소 (Chlorine)', 2.0, 'TON', 12, 'CAMEO', 'AEGL',
  5.0, 'NW', 7.5, 62, 'D',
  'COMPLETED', 'LOW', '서해팀, 방재팀',
  '{"aegl3": true, "aegl2": true, "aegl1": true, "damageRadius": "1.0 km", "zones": [{"level":"AEGL-3","radius":300},{"level":"AEGL-2","radius":600},{"level":"AEGL-1","radius":1000}]}'::jsonb,
  '2025-01-15 14:10'::timestamptz
),
(
  '제주 에탄올링 탱크 파열', '2025-01-08 07:55'::timestamptz,
  '제주항', 126.528, 33.519, ST_SetSRID(ST_MakePoint(126.528, 33.519), 4326), '126.528 + 33.519',
  '에탄올 (Ethanol)', 1.5, 'TON', 6, 'CAMEO', 'AEGL',
  6.8, 'NE', 12.0, 55, 'C',
  'COMPLETED', 'LOW', '제주팀, 일대팀',
  '{"aegl3": true, "aegl2": true, "aegl1": true, "damageRadius": "0.8 km", "zones": [{"level":"AEGL-3","radius":200},{"level":"AEGL-2","radius":450},{"level":"AEGL-1","radius":800}]}'::jsonb,
  '2025-01-08 07:55'::timestamptz
);
