-- WING-OPS Database Schema: Boom & Backtrack Tables
-- wing 스키마 오일펜스/역추적/선박 테이블

-- ============================================================
-- 1. boom_line — 오일펜스배치
-- ============================================================
CREATE TABLE wing.boom_line (
    boom_line_sn integer NOT NULL,
    acdnt_sn integer NOT NULL,
    boom_nm character varying(100),
    priority_ord integer DEFAULT 0,
    geom public.geometry(LineString,4326),
    length_m numeric(8,1),
    efficiency_pct numeric(5,1),
    deploy_dtm timestamp with time zone,
    stts_cd character varying(20) DEFAULT 'PLANNED'::character varying,
    use_yn character(1) DEFAULT 'Y'::bpchar,
    reg_dtm timestamp with time zone DEFAULT now(),
    CONSTRAINT boom_line_pkey PRIMARY KEY (boom_line_sn),
    CONSTRAINT ck_boom_stts CHECK (((stts_cd)::text = ANY (ARRAY[('PLANNED'::character varying)::text, ('DEPLOYED'::character varying)::text, ('RETRIEVED'::character varying)::text])))
);

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

ALTER SEQUENCE wing.boom_line_boom_line_sn_seq OWNED BY wing.boom_line.boom_line_sn;
ALTER TABLE ONLY wing.boom_line ALTER COLUMN boom_line_sn SET DEFAULT nextval('wing.boom_line_boom_line_sn_seq'::regclass);


-- ============================================================
-- 2. backtrack — 역추적분석
-- ============================================================
CREATE TABLE wing.backtrack (
    backtrack_sn integer NOT NULL,
    acdnt_sn integer NOT NULL,
    est_spil_dtm timestamp with time zone,
    anlys_range character varying(20),
    lon numeric(10,6),
    lat numeric(9,6),
    geom public.geometry(Point,4326),
    loc_dc character varying(100),
    srch_radius_nm numeric(5,1),
    total_vessels integer,
    exec_stts_cd character varying(20) DEFAULT 'PENDING'::character varying,
    rslt_data jsonb,
    use_yn character(1) DEFAULT 'Y'::bpchar,
    reg_dtm timestamp with time zone DEFAULT now(),
    CONSTRAINT backtrack_pkey PRIMARY KEY (backtrack_sn),
    CONSTRAINT ck_backtrack_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.backtrack_backtrack_sn_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE wing.backtrack_backtrack_sn_seq OWNED BY wing.backtrack.backtrack_sn;
ALTER TABLE ONLY wing.backtrack ALTER COLUMN backtrack_sn SET DEFAULT nextval('wing.backtrack_backtrack_sn_seq'::regclass);


-- ============================================================
-- 3. vessel_info — 선박정보
-- ============================================================
CREATE TABLE wing.vessel_info (
    vessel_info_sn integer NOT NULL,
    acdnt_sn integer NOT NULL,
    imo_no character varying(10),
    mmsi_no character varying(10),
    vessel_nm character varying(100),
    vessel_tp character varying(30),
    loa_m numeric(6,1),
    breadth_m numeric(5,1),
    draft_m numeric(5,1),
    gt numeric(10,0),
    dwt numeric(10,0),
    built_yr smallint,
    flag_cd character varying(5),
    callsign character varying(10),
    engine_dc character varying(100),
    insurance_data jsonb,
    reg_dtm timestamp with time zone DEFAULT now(),
    CONSTRAINT vessel_info_pkey PRIMARY KEY (vessel_info_sn)
);

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

ALTER SEQUENCE wing.vessel_info_vessel_info_sn_seq OWNED BY wing.vessel_info.vessel_info_sn;
ALTER TABLE ONLY wing.vessel_info ALTER COLUMN vessel_info_sn SET DEFAULT nextval('wing.vessel_info_vessel_info_sn_seq'::regclass);


-- ============================================================
-- FK 제약조건
-- ============================================================
ALTER TABLE ONLY wing.boom_line
    ADD CONSTRAINT boom_line_acdnt_sn_fkey FOREIGN KEY (acdnt_sn) REFERENCES wing.acdnt(acdnt_sn) ON DELETE CASCADE;

ALTER TABLE ONLY wing.backtrack
    ADD CONSTRAINT backtrack_acdnt_sn_fkey FOREIGN KEY (acdnt_sn) REFERENCES wing.acdnt(acdnt_sn) ON DELETE CASCADE;

ALTER TABLE ONLY wing.vessel_info
    ADD CONSTRAINT vessel_info_acdnt_sn_fkey FOREIGN KEY (acdnt_sn) REFERENCES wing.acdnt(acdnt_sn) ON DELETE CASCADE;


-- ============================================================
-- 인덱스
-- ============================================================
-- 공간 인덱스
CREATE INDEX idx_boom_geom ON wing.boom_line USING gist (geom);
CREATE INDEX idx_backtrack_geom ON wing.backtrack USING gist (geom);

-- 외래키 인덱스
CREATE INDEX idx_boom_acdnt ON wing.boom_line USING btree (acdnt_sn);
CREATE INDEX idx_backtrack_acdnt ON wing.backtrack USING btree (acdnt_sn);
CREATE INDEX idx_vessel_acdnt ON wing.vessel_info USING btree (acdnt_sn);
