-- WING-OPS Database Schema: Aerial & Rescue Tables
-- wing 스키마: aerial_media, cctv_camera, sat_request, rescue_ops, rescue_scenario
-- FK 의존성: rescue_scenario → rescue_ops

-- ============================================================
-- 1. 항공 미디어 (aerial_media)
-- ============================================================
CREATE TABLE wing.aerial_media (
    aerial_media_sn integer NOT NULL,
    acdnt_sn        integer,
    file_nm         character varying(200) NOT NULL,
    orgnl_nm        character varying(200),
    file_path       character varying(500),
    lon             numeric(10,6),
    lat             numeric(9,6),
    geom            public.geometry(Point, 4326),
    loc_dc          character varying(100),
    equip_tp_cd     character varying(20),
    equip_nm        character varying(50),
    media_tp_cd     character varying(20),
    takng_dtm       timestamp with time zone,
    file_sz         character varying(20),
    resolution      character varying(30),
    use_yn          character(1) DEFAULT 'Y'::bpchar,
    reg_dtm         timestamp with time zone DEFAULT now(),
    CONSTRAINT aerial_media_pkey PRIMARY KEY (aerial_media_sn)
);

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

ALTER SEQUENCE wing.aerial_media_aerial_media_sn_seq OWNED BY wing.aerial_media.aerial_media_sn;
ALTER TABLE ONLY wing.aerial_media ALTER COLUMN aerial_media_sn SET DEFAULT nextval('wing.aerial_media_aerial_media_sn_seq'::regclass);


-- ============================================================
-- 2. CCTV 카메라 (cctv_camera)
-- ============================================================
CREATE TABLE wing.cctv_camera (
    cctv_sn    integer NOT NULL,
    camera_nm  character varying(100) NOT NULL,
    region_nm  character varying(20),
    lon        numeric(10,6),
    lat        numeric(9,6),
    geom       public.geometry(Point, 4326),
    loc_dc     character varying(200),
    coord_dc   character varying(50),
    stts_cd    character varying(20) DEFAULT 'LIVE'::character varying,
    ptz_yn     character(1)          DEFAULT 'N'::bpchar,
    source_nm  character varying(50),
    stream_url character varying(500),
    use_yn     character(1)          DEFAULT 'Y'::bpchar,
    reg_dtm    timestamp with time zone DEFAULT now(),
    CONSTRAINT cctv_camera_pkey PRIMARY KEY (cctv_sn)
);

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

ALTER SEQUENCE wing.cctv_camera_cctv_sn_seq OWNED BY wing.cctv_camera.cctv_sn;
ALTER TABLE ONLY wing.cctv_camera ALTER COLUMN cctv_sn SET DEFAULT nextval('wing.cctv_camera_cctv_sn_seq'::regclass);


-- ============================================================
-- 3. 위성 촬영 요청 (sat_request)
-- ============================================================
CREATE TABLE wing.sat_request (
    sat_req_sn        integer NOT NULL,
    req_cd            character varying(20)  NOT NULL,
    acdnt_sn          integer,
    lon               numeric(10,6),
    lat               numeric(9,6),
    geom              public.geometry(Point, 4326),
    zone_dc           character varying(200),
    coord_dc          character varying(50),
    zone_area_km2     numeric(8,2),
    sat_nm            character varying(50),
    provider_nm       character varying(50),
    resolution        character varying(20),
    purpose_dc        character varying(200),
    reqstr_nm         character varying(50),
    req_dtm           timestamp with time zone,
    expected_rcv_dtm  timestamp with time zone,
    stts_cd           character varying(20) DEFAULT 'PENDING'::character varying,
    use_yn            character(1)          DEFAULT 'Y'::bpchar,
    reg_dtm           timestamp with time zone DEFAULT now(),
    CONSTRAINT sat_request_pkey        PRIMARY KEY (sat_req_sn),
    CONSTRAINT sat_request_req_cd_key  UNIQUE (req_cd),
    CONSTRAINT ck_sat_stts CHECK (
        ((stts_cd)::text = ANY ((ARRAY['PENDING'::character varying, 'SHOOTING'::character varying, 'COMPLETED'::character varying, 'CANCELLED'::character varying])::text[]))
    )
);

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

ALTER SEQUENCE wing.sat_request_sat_req_sn_seq OWNED BY wing.sat_request.sat_req_sn;
ALTER TABLE ONLY wing.sat_request ALTER COLUMN sat_req_sn SET DEFAULT nextval('wing.sat_request_sat_req_sn_seq'::regclass);


-- ============================================================
-- 4. 구조 작전 (rescue_ops)
-- ============================================================
CREATE TABLE wing.rescue_ops (
    rescue_ops_sn integer NOT NULL,
    acdnt_sn      integer,
    ops_cd        character varying(20)  NOT NULL,
    acdnt_tp_cd   character varying(20),
    vessel_nm     character varying(100),
    commander_nm  character varying(50),
    lon           numeric(10,6),
    lat           numeric(9,6),
    geom          public.geometry(Point, 4326),
    loc_dc        character varying(100),
    depth_m       numeric(6,1),
    current_dc    character varying(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       character varying(20) DEFAULT 'ACTIVE'::character varying,
    use_yn        character(1)          DEFAULT 'Y'::bpchar,
    reg_dtm       timestamp with time zone DEFAULT now(),
    mdfcn_dtm     timestamp with time zone DEFAULT now(),
    CONSTRAINT rescue_ops_pkey       PRIMARY KEY (rescue_ops_sn),
    CONSTRAINT rescue_ops_ops_cd_key UNIQUE (ops_cd)
);

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

ALTER SEQUENCE wing.rescue_ops_rescue_ops_sn_seq OWNED BY wing.rescue_ops.rescue_ops_sn;
ALTER TABLE ONLY wing.rescue_ops ALTER COLUMN rescue_ops_sn SET DEFAULT nextval('wing.rescue_ops_rescue_ops_sn_seq'::regclass);


-- ============================================================
-- 5. 구조 시나리오 (rescue_scenario)
-- 의존: rescue_ops
-- ============================================================
CREATE TABLE wing.rescue_scenario (
    scenario_sn   integer NOT NULL,
    rescue_ops_sn integer NOT NULL,
    time_step     character varying(10)  NOT NULL,
    scenario_dtm  timestamp with time zone,
    svrt_cd       character varying(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       timestamp with time zone DEFAULT now(),
    CONSTRAINT rescue_scenario_pkey PRIMARY KEY (scenario_sn)
);

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

ALTER SEQUENCE wing.rescue_scenario_scenario_sn_seq OWNED BY wing.rescue_scenario.scenario_sn;
ALTER TABLE ONLY wing.rescue_scenario ALTER COLUMN scenario_sn SET DEFAULT nextval('wing.rescue_scenario_scenario_sn_seq'::regclass);


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

ALTER TABLE ONLY wing.sat_request
    ADD CONSTRAINT sat_request_acdnt_sn_fkey
    FOREIGN KEY (acdnt_sn) REFERENCES wing.acdnt(acdnt_sn);

ALTER TABLE ONLY wing.rescue_ops
    ADD CONSTRAINT rescue_ops_acdnt_sn_fkey
    FOREIGN KEY (acdnt_sn) REFERENCES wing.acdnt(acdnt_sn);

ALTER TABLE ONLY wing.rescue_scenario
    ADD CONSTRAINT rescue_scenario_rescue_ops_sn_fkey
    FOREIGN KEY (rescue_ops_sn) REFERENCES wing.rescue_ops(rescue_ops_sn) ON DELETE CASCADE;
