-- ============================================================
-- 016_rescue.sql — 구조 시나리오(Rescue) 탭 테이블 + 초기 데이터
-- RESCUE_OPS (5건), RESCUE_SCENARIO (5건)
-- ============================================================

SET search_path TO wing, public;

-- ============================================================
-- 1. RESCUE_OPS — 구조 작전 정보
-- ============================================================
CREATE TABLE IF NOT EXISTS RESCUE_OPS (
  RESCUE_OPS_SN    SERIAL PRIMARY KEY,
  ACDNT_SN         INTEGER REFERENCES ACDNT(ACDNT_SN),
  OPS_CD           VARCHAR(20) NOT NULL UNIQUE,
  ACDNT_TP_CD      VARCHAR(20),
  VESSEL_NM        VARCHAR(100),
  COMMANDER_NM     VARCHAR(50),
  LON              NUMERIC(10,6),
  LAT              NUMERIC(9,6),
  GEOM             GEOMETRY(Point, 4326),
  LOC_DC           VARCHAR(100),
  DEPTH_M          NUMERIC(6,1),
  CURRENT_DC       VARCHAR(50),
  GM_M             NUMERIC(5,2),
  LIST_DEG         NUMERIC(5,1),
  TRIM_M           NUMERIC(5,2),
  BUOYANCY_PCT     NUMERIC(5,1),
  OIL_RATE_LPM     NUMERIC(8,1),
  BM_RATIO_PCT     NUMERIC(5,1),
  TOTAL_CREW       INTEGER,
  SURVIVORS        INTEGER,
  MISSING          INTEGER,
  HYDRO_DATA       JSONB,
  GMDSS_DATA       JSONB,
  STTS_CD          VARCHAR(20) DEFAULT 'ACTIVE',
  USE_YN           CHAR(1) DEFAULT 'Y',
  REG_DTM          TIMESTAMPTZ DEFAULT NOW(),
  MDFCN_DTM        TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_rescue_ops_acdnt ON RESCUE_OPS(ACDNT_SN);
CREATE INDEX IF NOT EXISTS idx_rescue_ops_geom ON RESCUE_OPS USING GIST(GEOM);

-- ============================================================
-- 2. RESCUE_SCENARIO — 시나리오 타임스텝
-- ============================================================
CREATE TABLE IF NOT EXISTS RESCUE_SCENARIO (
  SCENARIO_SN      SERIAL PRIMARY KEY,
  RESCUE_OPS_SN    INTEGER NOT NULL REFERENCES RESCUE_OPS(RESCUE_OPS_SN) ON DELETE CASCADE,
  TIME_STEP        VARCHAR(10) NOT NULL,
  SCENARIO_DTM     TIMESTAMPTZ,
  SVRT_CD          VARCHAR(20),
  GM_M             NUMERIC(5,2),
  LIST_DEG         NUMERIC(5,1),
  TRIM_M           NUMERIC(5,2),
  BUOYANCY_PCT     NUMERIC(5,1),
  OIL_RATE_LPM     NUMERIC(8,1),
  BM_RATIO_PCT     NUMERIC(5,1),
  DESCRIPTION      TEXT,
  COMPARTMENTS     JSONB,
  ASSESSMENT       JSONB,
  ACTIONS          JSONB,
  SORT_ORD         INTEGER DEFAULT 0,
  REG_DTM          TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_rescue_scenario_ops ON RESCUE_SCENARIO(RESCUE_OPS_SN);

-- ============================================================
-- 3. RESCUE_OPS 시드 데이터 (5건)
-- ============================================================
INSERT INTO RESCUE_OPS (
  ACDNT_SN, OPS_CD, ACDNT_TP_CD, VESSEL_NM, COMMANDER_NM,
  LON, LAT, GEOM, LOC_DC, DEPTH_M, CURRENT_DC,
  GM_M, LIST_DEG, TRIM_M, BUOYANCY_PCT, OIL_RATE_LPM, BM_RATIO_PCT,
  TOTAL_CREW, SURVIVORS, MISSING,
  HYDRO_DATA, GMDSS_DATA,
  STTS_CD, USE_YN
) VALUES
(
  1, 'RSC-2026-001', 'collision', 'M/V SEA GUARDIAN', NULL,
  126.25, 37.467,
  ST_SetSRID(ST_MakePoint(126.25::float, 37.467::float), 4326),
  '37°28''N, 126°15''E', 25.0, '2.5kn NE',
  0.8, 15.0, 2.5, 30.0, 100.0, 92.0,
  20, 15, 5,
  '{"displacement":"8,420t","draft":"5.8m","kg":"6.2m","km":"6.9m","tpc":"18.5","mtc":"195"}',
  '{"mmsi":"440234567","dscAlert":"VHF Ch.16 자동발신"}',
  'ACTIVE', 'Y'
),
(
  2, 'RSC-2026-002', 'grounding', 'M/V OCEAN BREEZE', NULL,
  128.62, 35.09,
  ST_SetSRID(ST_MakePoint(128.62::float, 35.09::float), 4326),
  '35°05''N, 128°37''E', 12.0, '1.8kn SW',
  0.3, 22.0, 4.8, 15.0, 250.0, 78.0,
  18, 18, 0,
  NULL, NULL,
  'ACTIVE', 'Y'
),
(
  3, 'RSC-2026-003', 'flooding', 'F/V DONG JIN', NULL,
  129.08, 35.15,
  ST_SetSRID(ST_MakePoint(129.08::float, 35.15::float), 4326),
  '35°09''N, 129°05''E', 30.0, '3.0kn N',
  0.5, 8.0, 1.5, 45.0, 50.0, 85.0,
  8, 7, 1,
  NULL, NULL,
  'ACTIVE', 'Y'
),
(
  4, 'RSC-2025-045', 'capsizing', 'M/V PACIFIC STAR', NULL,
  126.57, 33.24,
  ST_SetSRID(ST_MakePoint(126.57::float, 33.24::float), 4326),
  '33°14''N, 126°34''E', 45.0, '2.0kn SE',
  0.1, 35.0, 6.0, 10.0, 300.0, 65.0,
  25, 20, 5,
  NULL, NULL,
  'RESOLVED', 'Y'
),
(
  5, 'RSC-2025-040', 'turning', 'M/V GOLDEN WAVE', NULL,
  127.68, 34.73,
  ST_SetSRID(ST_MakePoint(127.68::float, 34.73::float), 4326),
  '34°44''N, 127°41''E', 18.0, '1.5kn W',
  1.2, 5.0, 0.8, 65.0, 20.0, 95.0,
  12, 12, 0,
  NULL, NULL,
  'RESOLVED', 'Y'
);

-- ============================================================
-- 4. RESCUE_SCENARIO 시드 데이터 (5건, RESCUE_OPS_SN=1 기준)
-- ============================================================
INSERT INTO RESCUE_SCENARIO (
  RESCUE_OPS_SN, TIME_STEP, SCENARIO_DTM, SVRT_CD,
  GM_M, LIST_DEG, TRIM_M, BUOYANCY_PCT, OIL_RATE_LPM, BM_RATIO_PCT,
  DESCRIPTION, COMPARTMENTS, ASSESSMENT, ACTIONS, SORT_ORD
) VALUES
(
  1, 'T+0h', '2024-10-27 10:30:00+09', 'CRITICAL',
  0.8, 15.0, 2.5, 30.0, 100.0, 92.0,
  '좌현 35° 충돌로 No.1P 화물탱크 파공, 벙커C유 유출 개시. 좌현 경사 15°, GM 위험수준.',
  '[{"name":"#1 FP Tank","status":"FLOODED","color":"var(--red)"},{"name":"#1 Port Tank","status":"BREACHED","color":"var(--red)"},{"name":"#2 Port Tank","status":"RISK","color":"var(--orange)"},{"name":"Engine Room","status":"INTACT","color":"var(--green)"},{"name":"#3 Stbd Tank","status":"INTACT","color":"var(--green)"}]',
  '[{"label":"복원력","value":"위험 (GM < 1.0m)","color":"var(--red)"},{"label":"유출 위험","value":"활발 유출중","color":"var(--red)"},{"label":"선체 강도","value":"BM 92% (경계)","color":"var(--orange)"},{"label":"승선인원","value":"15/20 확인, 5명 수색중","color":"var(--red)"}]',
  '[{"time":"10:30","text":"충돌 발생, VHF Ch.16 조난 통보","color":"var(--red)"},{"time":"10:35","text":"해경 3009함 출동 지시","color":"var(--orange)"},{"time":"10:42","text":"인근 선박 구조 활동 개시","color":"var(--cyan)"},{"time":"10:50","text":"유출유 방제선 배치 요청","color":"var(--orange)"}]',
  1
),
(
  1, 'T+2h', '2024-10-27 12:30:00+09', 'HIGH',
  0.6, 18.0, 3.2, 25.0, 150.0, 88.0,
  '침수 확대로 경사 증가, 유출량 증가 추세. 긴급 이초 작업 검토 필요.',
  '[{"name":"#1 FP Tank","status":"FLOODED","color":"var(--red)"},{"name":"#1 Port Tank","status":"FLOODED","color":"var(--red)"},{"name":"#2 Port Tank","status":"FLOODED","color":"var(--red)"},{"name":"Engine Room","status":"RISK","color":"var(--orange)"},{"name":"#3 Stbd Tank","status":"INTACT","color":"var(--green)"}]',
  '[{"label":"복원력","value":"위험 (GM 0.6m)","color":"var(--red)"},{"label":"유출 위험","value":"증가 추세","color":"var(--red)"},{"label":"선체 강도","value":"BM 88%","color":"var(--orange)"},{"label":"승선인원","value":"전원 퇴선 완료","color":"var(--green)"}]',
  '[{"time":"12:00","text":"2차 침수 확인 (#2 PT)","color":"var(--red)"},{"time":"12:15","text":"긴급 이초 작업 개시","color":"var(--orange)"},{"time":"12:20","text":"오일펜스 1차 전개 완료","color":"var(--cyan)"},{"time":"12:30","text":"항공기 유출유 촬영 요청","color":"var(--cyan)"}]',
  2
),
(
  1, 'T+6h', '2024-10-27 16:30:00+09', 'HIGH',
  0.4, 12.0, 2.8, 35.0, 80.0, 90.0,
  '평형수 이동으로 경사 일부 복원. 유출률 감소 추세.',
  '[{"name":"#1 FP Tank","status":"FLOODED","color":"var(--red)"},{"name":"#1 Port Tank","status":"FLOODED","color":"var(--red)"},{"name":"#2 Port Tank","status":"FLOODED","color":"var(--red)"},{"name":"Engine Room","status":"RISK","color":"var(--orange)"},{"name":"#3 Stbd Tank","status":"RISK","color":"var(--orange)"}]',
  '[{"label":"복원력","value":"개선 추세 (GM 0.4m)","color":"var(--orange)"},{"label":"유출 위험","value":"감소 추세","color":"var(--orange)"},{"label":"선체 강도","value":"BM 90%","color":"var(--orange)"},{"label":"구조 상황","value":"구조 작전 진행중","color":"var(--cyan)"}]',
  '[{"time":"14:00","text":"평형수 이동 작업 개시","color":"var(--cyan)"},{"time":"15:00","text":"해상크레인 도착","color":"var(--cyan)"},{"time":"15:30","text":"잔류유 이적 작업 개시","color":"var(--orange)"},{"time":"16:30","text":"예인준비 완료","color":"var(--green)"}]',
  3
),
(
  1, 'T+12h', '2024-10-27 22:30:00+09', 'MEDIUM',
  0.6, 8.0, 1.5, 50.0, 30.0, 94.0,
  '예인 작업 진행중, 선체 안정화 확인. 유출 대부분 차단.',
  '[{"name":"#1 FP Tank","status":"FLOODED","color":"var(--red)"},{"name":"#1 Port Tank","status":"FLOODED","color":"var(--red)"},{"name":"#2 Port Tank","status":"SEALED","color":"var(--orange)"},{"name":"Engine Room","status":"INTACT","color":"var(--green)"},{"name":"#3 Stbd Tank","status":"INTACT","color":"var(--green)"}]',
  '[{"label":"복원력","value":"안정 (GM 0.6m)","color":"var(--orange)"},{"label":"유출 위험","value":"대부분 차단","color":"var(--green)"},{"label":"선체 강도","value":"BM 94%","color":"var(--green)"},{"label":"예인 상태","value":"목포항 예인 진행중","color":"var(--cyan)"}]',
  '[{"time":"18:00","text":"예인 개시 (목포항 방향)","color":"var(--cyan)"},{"time":"19:00","text":"유출유 차단 확인","color":"var(--green)"},{"time":"20:00","text":"야간 감시 체제 전환","color":"var(--orange)"},{"time":"22:30","text":"예인 50% 진행","color":"var(--cyan)"}]',
  4
),
(
  1, 'T+24h', '2024-10-28 10:30:00+09', 'RESOLVED',
  1.2, 3.0, 0.5, 75.0, 5.0, 98.0,
  '목포항 도착, 선체 안정. 잔류유 이적 완료.',
  '[{"name":"#1 FP Tank","status":"SEALED","color":"var(--orange)"},{"name":"#1 Port Tank","status":"SEALED","color":"var(--orange)"},{"name":"#2 Port Tank","status":"SEALED","color":"var(--orange)"},{"name":"Engine Room","status":"INTACT","color":"var(--green)"},{"name":"#3 Stbd Tank","status":"INTACT","color":"var(--green)"}]',
  '[{"label":"복원력","value":"안전 (GM 1.2m)","color":"var(--green)"},{"label":"유출 위험","value":"차단 완료","color":"var(--green)"},{"label":"선체 강도","value":"BM 98% 정상","color":"var(--green)"},{"label":"예인 상태","value":"목포항 접안 완료","color":"var(--green)"}]',
  '[{"time":"06:00","text":"목포항 접근","color":"var(--cyan)"},{"time":"08:00","text":"도선사 승선, 접안 개시","color":"var(--cyan)"},{"time":"09:30","text":"접안 완료","color":"var(--green)"},{"time":"10:30","text":"잔류유 이적 완료, 상황 종료","color":"var(--green)"}]',
  5
);
