-- WING-OPS Database Schema: Org & User Tables
-- wing 스키마 조직/사용자 테이블
-- (pg_dump에 없음, init.sql 기반으로 wing 스키마 변환)

-- ============================================================
-- 1. org — 조직 (자기참조 FK)
-- ============================================================
CREATE TABLE wing.org (
    org_sn integer NOT NULL,
    org_nm character varying(100) NOT NULL,
    org_abbr_nm character varying(20) NOT NULL,
    org_tp_cd character varying(20) NOT NULL,
    upper_org_sn integer,
    reg_dtm timestamp with time zone DEFAULT now() NOT NULL,
    CONSTRAINT pk_org PRIMARY KEY (org_sn)
);

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

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

COMMENT ON TABLE  wing.org IS '조직';
COMMENT ON COLUMN wing.org.org_sn       IS '조직순번';
COMMENT ON COLUMN wing.org.org_nm       IS '조직명 (기관 전체 명칭)';
COMMENT ON COLUMN wing.org.org_abbr_nm  IS '조직약칭명 (기관 약칭)';
COMMENT ON COLUMN wing.org.org_tp_cd    IS '조직유형코드 (ORG_TP: headquarters, regional, station, agency)';
COMMENT ON COLUMN wing.org.upper_org_sn IS '상위조직순번 (상위 기관 참조)';
COMMENT ON COLUMN wing.org.reg_dtm      IS '등록일시';


-- ============================================================
-- 2. user_info — 사용자정보 (FK: wing.org)
-- (USER는 PostgreSQL 예약어이므로 user_info 사용)
-- ============================================================
CREATE TABLE wing.user_info (
    user_id uuid DEFAULT public.uuid_generate_v4() NOT NULL,
    user_acnt character varying(50) NOT NULL,
    pswd_hash character varying(255) NOT NULL,
    user_nm character varying(50) NOT NULL,
    rnkp_nm character varying(30),
    org_sn integer,
    role_cd character varying(20) DEFAULT 'USER'::character varying NOT NULL,
    actv_yn character(1) DEFAULT 'Y'::bpchar NOT NULL,
    reg_dtm timestamp with time zone DEFAULT now() NOT NULL,
    CONSTRAINT pk_user_info PRIMARY KEY (user_id),
    CONSTRAINT uk_user_acnt UNIQUE (user_acnt),
    CONSTRAINT ck_user_role_cd CHECK (((role_cd)::text = ANY (ARRAY[('ADMIN'::character varying)::text, ('MANAGER'::character varying)::text, ('USER'::character varying)::text]))),
    CONSTRAINT ck_user_actv_yn CHECK ((actv_yn = ANY (ARRAY['Y'::bpchar, 'N'::bpchar])))
);

COMMENT ON TABLE  wing.user_info IS '사용자정보';
COMMENT ON COLUMN wing.user_info.user_id    IS '사용자아이디 (UUID)';
COMMENT ON COLUMN wing.user_info.user_acnt  IS '사용자계정 (로그인 ID)';
COMMENT ON COLUMN wing.user_info.pswd_hash  IS '비밀번호해시 (bcrypt 해시값)';
COMMENT ON COLUMN wing.user_info.user_nm    IS '사용자명';
COMMENT ON COLUMN wing.user_info.rnkp_nm    IS '직급명';
COMMENT ON COLUMN wing.user_info.org_sn     IS '조직순번 (소속 조직)';
COMMENT ON COLUMN wing.user_info.role_cd    IS '역할코드 (ADMIN, MANAGER, USER)';
COMMENT ON COLUMN wing.user_info.actv_yn    IS '활성여부 (Y:활성, N:비활성)';
COMMENT ON COLUMN wing.user_info.reg_dtm    IS '등록일시';


-- ============================================================
-- FK 제약조건
-- ============================================================
ALTER TABLE ONLY wing.org
    ADD CONSTRAINT fk_org_upper FOREIGN KEY (upper_org_sn) REFERENCES wing.org(org_sn);

ALTER TABLE ONLY wing.user_info
    ADD CONSTRAINT fk_user_org FOREIGN KEY (org_sn) REFERENCES wing.org(org_sn);


-- ============================================================
-- 인덱스
-- ============================================================
CREATE INDEX idx_user_org_sn ON wing.user_info USING btree (org_sn);
