-- WING-OPS Database Schema: Additional Indexes
-- 01~12 파일의 PK/UNIQUE 제약조건을 제외한 추가 인덱스 모음
-- (CREATE INDEX 만 포함, ALTER TABLE ... ADD CONSTRAINT 형태의 UNIQUE는 각 테이블 파일에 포함)

-- ============================================================
-- auth 스키마 인덱스
-- ============================================================

-- auth_audit_log
CREATE INDEX idx_audit_log_action ON auth.auth_audit_log USING btree (action_cd);
CREATE INDEX idx_audit_log_dtm    ON auth.auth_audit_log USING btree (req_dtm);
CREATE INDEX idx_audit_log_user   ON auth.auth_audit_log USING btree (user_id);

-- auth_login_hist
CREATE INDEX idx_auth_login_dtm  ON auth.auth_login_hist USING btree (login_dtm);
CREATE INDEX idx_auth_login_user ON auth.auth_login_hist USING btree (user_id);

-- auth_perm
CREATE INDEX idx_auth_perm_oper ON auth.auth_perm USING btree (oper_cd);
CREATE INDEX idx_auth_perm_role ON auth.auth_perm USING btree (role_sn);
CREATE INDEX idx_auth_perm_rsrc ON auth.auth_perm USING btree (rsrc_cd);

-- auth_user
CREATE INDEX idx_auth_user_org  ON auth.auth_user USING btree (org_sn);
CREATE INDEX idx_auth_user_stts ON auth.auth_user USING btree (user_stts_cd);

-- auth_perm_tree
CREATE INDEX idx_perm_tree_parent ON auth.auth_perm_tree USING btree (parent_cd);

-- auth_user — 부분 유니크 인덱스 (조건부)
CREATE UNIQUE INDEX uk_auth_user_email ON auth.auth_user USING btree (email)
    WHERE (email IS NOT NULL);

CREATE UNIQUE INDEX uk_auth_user_oauth ON auth.auth_user USING btree (oauth_provider, oauth_sub)
    WHERE (oauth_provider IS NOT NULL);


-- ============================================================
-- wing 스키마 인덱스 — GiST 공간 인덱스
-- ============================================================
CREATE INDEX idx_acdnt_loc_geom    ON wing.acdnt         USING gist (loc_geom);
CREATE INDEX idx_aerial_media_geom ON wing.aerial_media  USING gist (geom);
CREATE INDEX idx_asset_org_geom    ON wing.asset_org     USING gist (geom);
CREATE INDEX idx_backtrack_geom    ON wing.backtrack      USING gist (geom);
CREATE INDEX idx_boom_geom         ON wing.boom_line      USING gist (geom);
CREATE INDEX idx_cctv_geom         ON wing.cctv_camera   USING gist (geom);
CREATE INDEX idx_cst_sect_geom     ON wing.cst_sect      USING gist (geom);
CREATE INDEX idx_cst_srvy_zone_geom ON wing.cst_srvy_zone USING gist (geom);
CREATE INDEX idx_rescue_ops_geom   ON wing.rescue_ops    USING gist (geom);
CREATE INDEX idx_sat_req_geom      ON wing.sat_request   USING gist (geom);


-- ============================================================
-- wing 스키마 인덱스 — GIN (전문검색 / jsonb)
-- ============================================================
CREATE INDEX idx_hns_sbst_abbr   ON wing.hns_substance USING gin (abbreviation 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_nm_kr  ON wing.hns_substance USING gin (nm_kr         public.gin_trgm_ops);
CREATE INDEX idx_hns_sbst_data   ON wing.hns_substance USING gin (data jsonb_path_ops);


-- ============================================================
-- wing 스키마 인덱스 — btree
-- ============================================================

-- acdnt
CREATE INDEX idx_acdnt_occrn  ON wing.acdnt USING btree (occrn_dtm DESC);
CREATE INDEX idx_acdnt_region ON wing.acdnt USING btree (region_nm);
CREATE INDEX idx_acdnt_stts   ON wing.acdnt USING btree (acdnt_stts_cd);

-- aerial_media
CREATE INDEX idx_aerial_media_acdnt ON wing.aerial_media USING btree (acdnt_sn);

-- asset_org
CREATE INDEX idx_asset_org_jrsd ON wing.asset_org USING btree (jrsd_nm);
CREATE INDEX idx_asset_org_tp   ON wing.asset_org USING btree (org_tp);

-- asset_equip / asset_contact
CREATE INDEX idx_asset_equip_org   ON wing.asset_equip   USING btree (org_sn);
CREATE INDEX idx_asset_contact_org ON wing.asset_contact USING btree (org_sn);

-- backtrack
CREATE INDEX idx_backtrack_acdnt ON wing.backtrack USING btree (acdnt_sn);

-- board_post
CREATE INDEX idx_board_author   ON wing.board_post USING btree (author_id);
CREATE INDEX idx_board_category ON wing.board_post USING btree (category_cd);
CREATE INDEX idx_board_reg_dtm  ON wing.board_post USING btree (reg_dtm DESC);

-- boom_line
CREATE INDEX idx_boom_acdnt ON wing.boom_line USING btree (acdnt_sn);

-- cst_sect
CREATE INDEX idx_cst_sect_esi  ON wing.cst_sect USING btree (esi_num);
CREATE INDEX idx_cst_sect_stts ON wing.cst_sect USING btree (srvy_stts_cd);
CREATE INDEX idx_cst_sect_zone ON wing.cst_sect USING btree (cst_srvy_zone_sn);

-- hns_substance (btree)
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);
CREATE INDEX idx_hns_sbst_un   ON wing.hns_substance USING btree (un_no);

-- layer
CREATE INDEX idx_layer_level ON wing.layer USING btree (layer_level);
CREATE INDEX idx_layer_up    ON wing.layer USING btree (up_layer_cd);
CREATE INDEX idx_layer_use   ON wing.layer USING btree (use_yn);

-- acdnt_media
CREATE INDEX idx_media_acdnt ON wing.acdnt_media USING btree (acdnt_sn);

-- pred_exec
CREATE INDEX idx_pred_acdnt ON wing.pred_exec USING btree (acdnt_sn);

-- report
CREATE INDEX idx_report_author   ON wing.report USING btree (author_id);
CREATE INDEX idx_report_ctgr     ON wing.report USING btree (ctgr_sn);
CREATE INDEX idx_report_reg_dtm  ON wing.report USING btree (reg_dtm DESC);
CREATE INDEX idx_report_stts     ON wing.report USING btree (stts_cd);
CREATE INDEX idx_report_tmpl     ON wing.report USING btree (tmpl_sn);

-- report_sect_data
CREATE INDEX idx_report_sect_report ON wing.report_sect_data USING btree (report_sn);

-- rescue_ops / rescue_scenario
CREATE INDEX idx_rescue_ops_acdnt   ON wing.rescue_ops      USING btree (acdnt_sn);
CREATE INDEX idx_rescue_scenario_ops ON wing.rescue_scenario USING btree (rescue_ops_sn);

-- spil_data
CREATE INDEX idx_spil_acdnt ON wing.spil_data USING btree (acdnt_sn);

-- vessel_info
CREATE INDEX idx_vessel_acdnt ON wing.vessel_info USING btree (acdnt_sn);

-- acdnt_weather
CREATE INDEX idx_weather_acdnt ON wing.acdnt_weather USING btree (acdnt_sn);
