-- WING-OPS Database Schema: Common Tables
-- wing 스키마 공통 테이블 (공통코드, 레이어)

-- ============================================================
-- 1. cmn_cd_grp — 공통코드그룹
-- (pg_dump에 없음, init.sql 기반으로 wing 스키마 변환)
-- ============================================================
CREATE TABLE wing.cmn_cd_grp (
    cmn_cd_grp_id character varying(20) NOT NULL,
    cmn_cd_grp_nm character varying(100) NOT NULL,
    grp_dc character varying(500),
    use_yn character(1) DEFAULT 'Y'::bpchar NOT NULL,
    reg_dtm timestamp with time zone DEFAULT now() NOT NULL,
    CONSTRAINT pk_cmn_cd_grp PRIMARY KEY (cmn_cd_grp_id),
    CONSTRAINT ck_cmn_cd_grp_use_yn CHECK ((use_yn = ANY (ARRAY['Y'::bpchar, 'N'::bpchar])))
);

COMMENT ON TABLE  wing.cmn_cd_grp IS '공통코드그룹';
COMMENT ON COLUMN wing.cmn_cd_grp.cmn_cd_grp_id IS '공통코드그룹아이디';
COMMENT ON COLUMN wing.cmn_cd_grp.cmn_cd_grp_nm IS '공통코드그룹명';
COMMENT ON COLUMN wing.cmn_cd_grp.grp_dc         IS '그룹설명';
COMMENT ON COLUMN wing.cmn_cd_grp.use_yn         IS '사용여부 (Y:사용, N:미사용)';
COMMENT ON COLUMN wing.cmn_cd_grp.reg_dtm        IS '등록일시';


-- ============================================================
-- 2. cmn_cd — 공통코드
-- (pg_dump에 없음, init.sql 기반으로 wing 스키마 변환)
-- ============================================================
CREATE TABLE wing.cmn_cd (
    cmn_cd_grp_id character varying(20) NOT NULL,
    cmn_cd character varying(20) NOT NULL,
    cmn_cd_nm character varying(100) NOT NULL,
    cd_dc character varying(500),
    sort_ord integer DEFAULT 0,
    use_yn character(1) DEFAULT 'Y'::bpchar NOT NULL,
    reg_dtm timestamp with time zone DEFAULT now() NOT NULL,
    CONSTRAINT pk_cmn_cd PRIMARY KEY (cmn_cd_grp_id, cmn_cd),
    CONSTRAINT ck_cmn_cd_use_yn CHECK ((use_yn = ANY (ARRAY['Y'::bpchar, 'N'::bpchar])))
);

COMMENT ON TABLE  wing.cmn_cd IS '공통코드';
COMMENT ON COLUMN wing.cmn_cd.cmn_cd_grp_id IS '공통코드그룹아이디';
COMMENT ON COLUMN wing.cmn_cd.cmn_cd        IS '공통코드';
COMMENT ON COLUMN wing.cmn_cd.cmn_cd_nm     IS '공통코드명';
COMMENT ON COLUMN wing.cmn_cd.cd_dc         IS '코드설명';
COMMENT ON COLUMN wing.cmn_cd.sort_ord      IS '정렬순서';
COMMENT ON COLUMN wing.cmn_cd.use_yn        IS '사용여부 (Y:사용, N:미사용)';
COMMENT ON COLUMN wing.cmn_cd.reg_dtm       IS '등록일시';


-- ============================================================
-- 3. layer — 레이어 (자기참조 FK)
-- ============================================================
CREATE TABLE wing.layer (
    layer_cd character varying(50) NOT NULL,
    up_layer_cd character varying(50),
    layer_full_nm character varying(200) NOT NULL,
    layer_nm character varying(100) NOT NULL,
    layer_level integer NOT NULL,
    wms_layer_nm character varying(100),
    use_yn character(1) DEFAULT 'Y'::bpchar NOT NULL,
    sort_ord integer DEFAULT 0,
    reg_dtm timestamp with time zone DEFAULT now() NOT NULL,
    CONSTRAINT pk_layer PRIMARY KEY (layer_cd),
    CONSTRAINT ck_layer_use_yn CHECK ((use_yn = ANY (ARRAY['Y'::bpchar, 'N'::bpchar])))
);


-- ============================================================
-- FK 제약조건
-- ============================================================
ALTER TABLE ONLY wing.cmn_cd
    ADD CONSTRAINT fk_cmn_cd_grp FOREIGN KEY (cmn_cd_grp_id) REFERENCES wing.cmn_cd_grp(cmn_cd_grp_id);

ALTER TABLE ONLY wing.layer
    ADD CONSTRAINT fk_layer_up FOREIGN KEY (up_layer_cd) REFERENCES wing.layer(layer_cd);


-- ============================================================
-- 인덱스
-- ============================================================
CREATE INDEX idx_cmn_cd_grp_use ON wing.cmn_cd_grp USING btree (use_yn);
CREATE INDEX idx_cmn_cd_use ON wing.cmn_cd USING btree (use_yn);

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