-- WING-OPS Database Schema: HNS Tables
-- wing 스키마 HNS(위험유해물질) 분석 테이블

-- ============================================================
-- 1. hns_substance — HNS물질정보
-- ============================================================
CREATE TABLE wing.hns_substance (
    sbst_sn integer NOT NULL,
    abbreviation character varying(50),
    nm_kr character varying(200) NOT NULL,
    nm_en character varying(200),
    un_no character varying(10),
    cas_no character varying(20),
    sebc character varying(50),
    data jsonb NOT NULL,
    use_yn character(1) DEFAULT 'Y'::bpchar NOT NULL,
    reg_dtm timestamp with time zone DEFAULT now() NOT NULL,
    CONSTRAINT pk_hns_substance PRIMARY KEY (sbst_sn),
    CONSTRAINT ck_hns_sbst_use CHECK ((use_yn = ANY (ARRAY['Y'::bpchar, 'N'::bpchar])))
);

CREATE SEQUENCE wing.hns_substance_sbst_sn_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE wing.hns_substance_sbst_sn_seq OWNED BY wing.hns_substance.sbst_sn;
ALTER TABLE ONLY wing.hns_substance ALTER COLUMN sbst_sn SET DEFAULT nextval('wing.hns_substance_sbst_sn_seq'::regclass);


-- ============================================================
-- 2. hns_analysis — HNS분석
-- ============================================================
CREATE TABLE wing.hns_analysis (
    hns_anlys_sn integer NOT NULL,
    acdnt_sn integer,
    anlys_nm character varying(200) NOT NULL,
    acdnt_dtm timestamp with time zone,
    loc_nm character varying(200),
    lon numeric(10,6),
    lat numeric(9,6),
    geom public.geometry(Point,4326),
    loc_dc character varying(100),
    sbst_sn integer,
    sbst_nm character varying(100),
    un_no character varying(10),
    cas_no character varying(20),
    spil_qty numeric(10,2),
    spil_unit_cd character varying(10) DEFAULT 'KL'::character varying,
    spil_tp_cd character varying(20),
    fcst_hr integer,
    algo_cd character varying(20),
    crit_mdl_cd character varying(10),
    wind_spd numeric(5,1),
    wind_dir character varying(10),
    temp numeric(4,1),
    humid numeric(4,1),
    atm_stbl_cd character varying(10),
    exec_stts_cd character varying(20) DEFAULT 'COMPLETED'::character varying,
    risk_cd character varying(20),
    analyst_nm character varying(50),
    rslt_data jsonb,
    use_yn character(1) DEFAULT 'Y'::bpchar,
    reg_dtm timestamp with time zone DEFAULT now(),
    mdfcn_dtm timestamp with time zone DEFAULT now(),
    CONSTRAINT hns_analysis_pkey PRIMARY KEY (hns_anlys_sn),
    CONSTRAINT ck_hns_stts CHECK (((exec_stts_cd)::text = ANY (ARRAY[('PENDING'::character varying)::text, ('RUNNING'::character varying)::text, ('COMPLETED'::character varying)::text, ('FAILED'::character varying)::text])))
);

CREATE SEQUENCE wing.hns_analysis_hns_anlys_sn_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE wing.hns_analysis_hns_anlys_sn_seq OWNED BY wing.hns_analysis.hns_anlys_sn;
ALTER TABLE ONLY wing.hns_analysis ALTER COLUMN hns_anlys_sn SET DEFAULT nextval('wing.hns_analysis_hns_anlys_sn_seq'::regclass);


-- ============================================================
-- FK 제약조건
-- ============================================================
ALTER TABLE ONLY wing.hns_analysis
    ADD CONSTRAINT hns_analysis_acdnt_sn_fkey FOREIGN KEY (acdnt_sn) REFERENCES wing.acdnt(acdnt_sn);


-- ============================================================
-- 인덱스
-- ============================================================
-- hns_substance 검색 인덱스 (GIN trgm — 텍스트 검색)
CREATE INDEX idx_hns_sbst_nm_kr ON wing.hns_substance USING gin (nm_kr public.gin_trgm_ops);
CREATE INDEX idx_hns_sbst_nm_en ON wing.hns_substance USING gin (nm_en public.gin_trgm_ops);
CREATE INDEX idx_hns_sbst_abbr ON wing.hns_substance USING gin (abbreviation public.gin_trgm_ops);

-- hns_substance 코드 인덱스
CREATE INDEX idx_hns_sbst_un ON wing.hns_substance USING btree (un_no);
CREATE INDEX idx_hns_sbst_cas ON wing.hns_substance USING btree (cas_no);
CREATE INDEX idx_hns_sbst_sebc ON wing.hns_substance USING btree (sebc);

-- hns_substance JSONB 인덱스
CREATE INDEX idx_hns_sbst_data ON wing.hns_substance USING gin (data jsonb_path_ops);
