-- WING-OPS Database Schema: SCAT Tables
-- wing 스키마 해안조사/SCAT 테이블

-- ============================================================
-- 1. cst_srvy_zone — 해안조사구역
-- ============================================================
CREATE TABLE wing.cst_srvy_zone (
    cst_srvy_zone_sn integer NOT NULL,
    zone_cd character varying(10) NOT NULL,
    zone_nm character varying(100) NOT NULL,
    jrsd_nm character varying(20),
    sect_cnt integer DEFAULT 0,
    lat_center numeric(9,6),
    lng_center numeric(9,6),
    lat_range numeric(9,6),
    lng_range numeric(9,6),
    geom public.geometry(Point,4326),
    use_yn character(1) DEFAULT 'Y'::bpchar,
    reg_dtm timestamp with time zone DEFAULT now(),
    CONSTRAINT cst_srvy_zone_pkey PRIMARY KEY (cst_srvy_zone_sn),
    CONSTRAINT cst_srvy_zone_zone_cd_key UNIQUE (zone_cd)
);

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

ALTER SEQUENCE wing.cst_srvy_zone_cst_srvy_zone_sn_seq OWNED BY wing.cst_srvy_zone.cst_srvy_zone_sn;
ALTER TABLE ONLY wing.cst_srvy_zone ALTER COLUMN cst_srvy_zone_sn SET DEFAULT nextval('wing.cst_srvy_zone_cst_srvy_zone_sn_seq'::regclass);


-- ============================================================
-- 2. cst_sect — 해안구간
-- ============================================================
CREATE TABLE wing.cst_sect (
    cst_sect_sn integer NOT NULL,
    cst_srvy_zone_sn integer,
    sect_cd character varying(20) NOT NULL,
    sect_nm character varying(200),
    cst_tp_cd character varying(30),
    esi_cd character varying(5),
    esi_num smallint,
    len_m numeric(8,1),
    snstvt_cd character varying(10),
    srvy_stts_cd character varying(10) DEFAULT '미조사'::character varying,
    lat numeric(9,6),
    lng numeric(9,6),
    geom public.geometry(Point,4326),
    tags jsonb DEFAULT '[]'::jsonb,
    shore_tp character varying(20),
    access_dc character varying(500),
    access_pt character varying(200),
    sensitive_info jsonb DEFAULT '[]'::jsonb,
    cleanup_methods jsonb DEFAULT '[]'::jsonb,
    end_criteria jsonb DEFAULT '[]'::jsonb,
    notes jsonb DEFAULT '[]'::jsonb,
    use_yn character(1) DEFAULT 'Y'::bpchar,
    reg_dtm timestamp with time zone DEFAULT now(),
    CONSTRAINT cst_sect_pkey PRIMARY KEY (cst_sect_sn),
    CONSTRAINT cst_sect_sect_cd_key UNIQUE (sect_cd)
);

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

ALTER SEQUENCE wing.cst_sect_cst_sect_sn_seq OWNED BY wing.cst_sect.cst_sect_sn;
ALTER TABLE ONLY wing.cst_sect ALTER COLUMN cst_sect_sn SET DEFAULT nextval('wing.cst_sect_cst_sect_sn_seq'::regclass);


-- ============================================================
-- 3. scat_srvy — SCAT조사
-- (pg_dump에 없음, init.sql 기반으로 wing 스키마 변환)
-- ============================================================
CREATE TABLE wing.scat_srvy (
    scat_srvy_sn integer NOT NULL,
    cst_sect_sn integer NOT NULL,
    acdnt_sn integer,
    srvy_ymd character(8),
    srvy_tp_cd character varying(20),
    srvy_phase_cd character varying(20),
    polut_cd character varying(20),
    srvyr_id uuid,
    rmk character varying(500),
    reg_dtm timestamp with time zone DEFAULT now() NOT NULL,
    CONSTRAINT pk_scat_srvy PRIMARY KEY (scat_srvy_sn)
);

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

ALTER SEQUENCE wing.scat_srvy_scat_srvy_sn_seq OWNED BY wing.scat_srvy.scat_srvy_sn;
ALTER TABLE ONLY wing.scat_srvy ALTER COLUMN scat_srvy_sn SET DEFAULT nextval('wing.scat_srvy_scat_srvy_sn_seq'::regclass);

COMMENT ON TABLE  wing.scat_srvy IS 'SCAT조사';
COMMENT ON COLUMN wing.scat_srvy.scat_srvy_sn  IS 'SCAT조사순번';
COMMENT ON COLUMN wing.scat_srvy.cst_sect_sn   IS '해안구간순번 (해안구간 참조)';
COMMENT ON COLUMN wing.scat_srvy.acdnt_sn      IS '사고순번 (관련 사고 참조)';
COMMENT ON COLUMN wing.scat_srvy.srvy_ymd      IS '조사일자 (YYYYMMDD 저장형식)';
COMMENT ON COLUMN wing.scat_srvy.srvy_tp_cd    IS '조사유형코드 (SRVY_TP: Pre-SCAT, SCAT, Post-SCAT)';
COMMENT ON COLUMN wing.scat_srvy.srvy_phase_cd IS '조사단계코드 (SRVY_PHASE: 1차, 2차, 3차 등)';
COMMENT ON COLUMN wing.scat_srvy.polut_cd      IS '오염도코드 (POLUT: HEAVY, MODERATE, LIGHT, CLEAN)';
COMMENT ON COLUMN wing.scat_srvy.srvyr_id      IS '조사자아이디';
COMMENT ON COLUMN wing.scat_srvy.rmk           IS '비고';
COMMENT ON COLUMN wing.scat_srvy.reg_dtm       IS '등록일시';


-- ============================================================
-- 4. scat_srvy_photo — SCAT조사사진
-- (pg_dump에 없음, init.sql 기반으로 wing 스키마 변환)
-- ============================================================
CREATE TABLE wing.scat_srvy_photo (
    scat_photo_sn integer NOT NULL,
    scat_srvy_sn integer NOT NULL,
    photo_file_path character varying(500) NOT NULL,
    photo_dc character varying(200),
    takng_dtm timestamp with time zone,
    sort_ord integer DEFAULT 0,
    reg_dtm timestamp with time zone DEFAULT now() NOT NULL,
    CONSTRAINT pk_scat_photo PRIMARY KEY (scat_photo_sn)
);

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

ALTER SEQUENCE wing.scat_srvy_photo_scat_photo_sn_seq OWNED BY wing.scat_srvy_photo.scat_photo_sn;
ALTER TABLE ONLY wing.scat_srvy_photo ALTER COLUMN scat_photo_sn SET DEFAULT nextval('wing.scat_srvy_photo_scat_photo_sn_seq'::regclass);

COMMENT ON TABLE  wing.scat_srvy_photo IS 'SCAT조사사진';
COMMENT ON COLUMN wing.scat_srvy_photo.scat_photo_sn   IS 'SCAT조사사진순번';
COMMENT ON COLUMN wing.scat_srvy_photo.scat_srvy_sn    IS 'SCAT조사순번 (SCAT조사 참조)';
COMMENT ON COLUMN wing.scat_srvy_photo.photo_file_path  IS '사진파일경로';
COMMENT ON COLUMN wing.scat_srvy_photo.photo_dc         IS '사진설명';
COMMENT ON COLUMN wing.scat_srvy_photo.takng_dtm        IS '촬영일시';
COMMENT ON COLUMN wing.scat_srvy_photo.sort_ord         IS '정렬순서';
COMMENT ON COLUMN wing.scat_srvy_photo.reg_dtm          IS '등록일시';


-- ============================================================
-- FK 제약조건
-- ============================================================
ALTER TABLE ONLY wing.cst_sect
    ADD CONSTRAINT cst_sect_cst_srvy_zone_sn_fkey FOREIGN KEY (cst_srvy_zone_sn) REFERENCES wing.cst_srvy_zone(cst_srvy_zone_sn);

ALTER TABLE ONLY wing.scat_srvy
    ADD CONSTRAINT fk_scat_sect FOREIGN KEY (cst_sect_sn) REFERENCES wing.cst_sect(cst_sect_sn);

ALTER TABLE ONLY wing.scat_srvy
    ADD CONSTRAINT fk_scat_acdnt FOREIGN KEY (acdnt_sn) REFERENCES wing.acdnt(acdnt_sn);

ALTER TABLE ONLY wing.scat_srvy_photo
    ADD CONSTRAINT fk_photo_srvy FOREIGN KEY (scat_srvy_sn) REFERENCES wing.scat_srvy(scat_srvy_sn) ON DELETE CASCADE;


-- ============================================================
-- 인덱스
-- ============================================================
-- 공간 인덱스
CREATE INDEX idx_cst_srvy_zone_geom ON wing.cst_srvy_zone USING gist (geom);
CREATE INDEX idx_cst_sect_geom ON wing.cst_sect USING gist (geom);

-- 외래키/상태 인덱스
CREATE INDEX idx_cst_sect_zone ON wing.cst_sect USING btree (cst_srvy_zone_sn);
CREATE INDEX idx_cst_sect_stts ON wing.cst_sect USING btree (srvy_stts_cd);
CREATE INDEX idx_cst_sect_esi ON wing.cst_sect USING btree (esi_num);

-- SCAT 조사 인덱스
CREATE INDEX idx_scat_sect_sn ON wing.scat_srvy USING btree (cst_sect_sn);
CREATE INDEX idx_scat_acdnt_sn ON wing.scat_srvy USING btree (acdnt_sn);
CREATE INDEX idx_photo_srvy_sn ON wing.scat_srvy_photo USING btree (scat_srvy_sn);
