-- WING-OPS Database Schema: Asset Management Tables
-- wing 스키마: asset_org, asset_equip, asset_contact, asset_upload_log

-- ============================================================
-- 1. 자산 기관 (asset_org)
-- ============================================================
CREATE TABLE wing.asset_org (
    org_sn          integer NOT NULL,
    org_tp          character varying(30)  NOT NULL,
    jrsd_nm         character varying(50)  NOT NULL,
    area_nm         character varying(30)  NOT NULL,
    org_nm          character varying(100) NOT NULL,
    addr            character varying(300),
    tel             character varying(30),
    lat             numeric(9,6),
    lng             numeric(10,6),
    pin_size        character varying(5)   DEFAULT 'md'::character varying,
    vessel_cnt      smallint               DEFAULT 0,
    skimmer_cnt     smallint               DEFAULT 0,
    pump_cnt        smallint               DEFAULT 0,
    vehicle_cnt     smallint               DEFAULT 0,
    sprayer_cnt     smallint               DEFAULT 0,
    total_assets    smallint               DEFAULT 0,
    use_yn          character(1)           DEFAULT 'Y'::bpchar,
    reg_dtm         timestamp with time zone DEFAULT now(),
    mdfcn_dtm       timestamp with time zone,
    geom            public.geometry(Point, 4326),
    CONSTRAINT asset_org_pkey PRIMARY KEY (org_sn),
    CONSTRAINT ck_asset_org_pin CHECK (
        ((pin_size)::text = ANY ((ARRAY['hq'::character varying, 'lg'::character varying, 'md'::character varying])::text[]))
    )
);

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

ALTER SEQUENCE wing.asset_org_org_sn_seq OWNED BY wing.asset_org.org_sn;
ALTER TABLE ONLY wing.asset_org ALTER COLUMN org_sn SET DEFAULT nextval('wing.asset_org_org_sn_seq'::regclass);


-- ============================================================
-- 2. 자산 장비 (asset_equip)
-- ============================================================
CREATE TABLE wing.asset_equip (
    equip_sn    integer NOT NULL,
    org_sn      integer NOT NULL,
    ctgr_nm     character varying(50) NOT NULL,
    icon        character varying(10),
    qty         smallint DEFAULT 0,
    sort_ord    smallint DEFAULT 0,
    CONSTRAINT asset_equip_pkey PRIMARY KEY (equip_sn),
    CONSTRAINT asset_equip_org_sn_ctgr_nm_key UNIQUE (org_sn, ctgr_nm)
);

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

ALTER SEQUENCE wing.asset_equip_equip_sn_seq OWNED BY wing.asset_equip.equip_sn;
ALTER TABLE ONLY wing.asset_equip ALTER COLUMN equip_sn SET DEFAULT nextval('wing.asset_equip_equip_sn_seq'::regclass);


-- ============================================================
-- 3. 자산 연락처 (asset_contact)
-- ============================================================
CREATE TABLE wing.asset_contact (
    contact_sn  integer NOT NULL,
    org_sn      integer NOT NULL,
    role_nm     character varying(50),
    contact_nm  character varying(50),
    tel         character varying(30),
    sort_ord    smallint DEFAULT 0,
    CONSTRAINT asset_contact_pkey PRIMARY KEY (contact_sn)
);

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

ALTER SEQUENCE wing.asset_contact_contact_sn_seq OWNED BY wing.asset_contact.contact_sn;
ALTER TABLE ONLY wing.asset_contact ALTER COLUMN contact_sn SET DEFAULT nextval('wing.asset_contact_contact_sn_seq'::regclass);


-- ============================================================
-- 4. 자산 업로드 로그 (asset_upload_log)
-- ============================================================
CREATE TABLE wing.asset_upload_log (
    log_sn       integer NOT NULL,
    file_nm      character varying(200) NOT NULL,
    uploader_nm  character varying(50),
    upload_cnt   integer DEFAULT 0,
    reg_dtm      timestamp with time zone DEFAULT now(),
    CONSTRAINT asset_upload_log_pkey PRIMARY KEY (log_sn)
);

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

ALTER SEQUENCE wing.asset_upload_log_log_sn_seq OWNED BY wing.asset_upload_log.log_sn;
ALTER TABLE ONLY wing.asset_upload_log ALTER COLUMN log_sn SET DEFAULT nextval('wing.asset_upload_log_log_sn_seq'::regclass);


-- ============================================================
-- FK 제약조건
-- ============================================================
ALTER TABLE ONLY wing.asset_equip
    ADD CONSTRAINT asset_equip_org_sn_fkey
    FOREIGN KEY (org_sn) REFERENCES wing.asset_org(org_sn) ON DELETE CASCADE;

ALTER TABLE ONLY wing.asset_contact
    ADD CONSTRAINT asset_contact_org_sn_fkey
    FOREIGN KEY (org_sn) REFERENCES wing.asset_org(org_sn) ON DELETE CASCADE;
