-- WING-OPS Database Schema: Board & Manual Tables
-- wing 스키마: board_post, board_attach, manual_file
-- 주의: board_post.author_id 는 auth.auth_user(user_id) 크로스스키마 FK

-- ============================================================
-- 1. 게시글 (board_post)
-- ============================================================
CREATE TABLE wing.board_post (
    post_sn     integer NOT NULL,
    category_cd character varying(20)  NOT NULL,
    title       character varying(200) NOT NULL,
    content     text,
    author_id   uuid                   NOT NULL,
    view_cnt    integer                DEFAULT 0 NOT NULL,
    pinned_yn   character(1)           DEFAULT 'N'::bpchar NOT NULL,
    use_yn      character(1)           DEFAULT 'Y'::bpchar NOT NULL,
    reg_dtm     timestamp with time zone DEFAULT now() NOT NULL,
    mdfcn_dtm   timestamp with time zone,
    CONSTRAINT board_post_pkey PRIMARY KEY (post_sn),
    CONSTRAINT ck_board_category CHECK (
        ((category_cd)::text = ANY ((ARRAY['NOTICE'::character varying, 'DATA'::character varying, 'QNA'::character varying, 'MANUAL'::character varying])::text[]))
    ),
    CONSTRAINT ck_board_pinned CHECK ((pinned_yn = ANY (ARRAY['Y'::bpchar, 'N'::bpchar]))),
    CONSTRAINT ck_board_use    CHECK ((use_yn    = ANY (ARRAY['Y'::bpchar, 'N'::bpchar])))
);

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

ALTER SEQUENCE wing.board_post_post_sn_seq OWNED BY wing.board_post.post_sn;
ALTER TABLE ONLY wing.board_post ALTER COLUMN post_sn SET DEFAULT nextval('wing.board_post_post_sn_seq'::regclass);


-- ============================================================
-- 2. 첨부파일 (board_attach)
-- ============================================================
CREATE TABLE wing.board_attach (
    attach_sn   integer NOT NULL,
    post_sn     integer NOT NULL,
    file_nm     character varying(200) NOT NULL,
    orgnl_nm    character varying(200),
    file_path   character varying(500),
    file_sz     integer,
    file_ext    character varying(10),
    sort_ord    integer DEFAULT 0,
    reg_dtm     timestamp with time zone DEFAULT now(),
    CONSTRAINT board_attach_pkey PRIMARY KEY (attach_sn)
);

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

ALTER SEQUENCE wing.board_attach_attach_sn_seq OWNED BY wing.board_attach.attach_sn;
ALTER TABLE ONLY wing.board_attach ALTER COLUMN attach_sn SET DEFAULT nextval('wing.board_attach_attach_sn_seq'::regclass);


-- ============================================================
-- 3. 매뉴얼 파일 (manual_file)
-- ============================================================
CREATE TABLE wing.manual_file (
    manual_sn   integer NOT NULL,
    catg_nm     character varying(50),
    title       character varying(200) NOT NULL,
    version     character varying(20),
    file_tp     character varying(20),
    file_sz     character varying(20),
    file_path   character varying(500),
    author_nm   character varying(50),
    dwnld_cnt   integer DEFAULT 0,
    use_yn      character(1)           DEFAULT 'Y'::bpchar,
    reg_dtm     timestamp with time zone DEFAULT now(),
    mdfcn_dtm   timestamp with time zone DEFAULT now(),
    CONSTRAINT manual_file_pkey PRIMARY KEY (manual_sn)
);

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

ALTER SEQUENCE wing.manual_file_manual_sn_seq OWNED BY wing.manual_file.manual_sn;
ALTER TABLE ONLY wing.manual_file ALTER COLUMN manual_sn SET DEFAULT nextval('wing.manual_file_manual_sn_seq'::regclass);


-- ============================================================
-- FK 제약조건
-- ============================================================
-- 크로스스키마 FK: board_post.author_id → auth.auth_user(user_id)
ALTER TABLE ONLY wing.board_post
    ADD CONSTRAINT fk_board_author
    FOREIGN KEY (author_id) REFERENCES auth.auth_user(user_id);

-- 동일스키마 FK: board_attach.post_sn → board_post
ALTER TABLE ONLY wing.board_attach
    ADD CONSTRAINT board_attach_post_sn_fkey
    FOREIGN KEY (post_sn) REFERENCES wing.board_post(post_sn) ON DELETE CASCADE;
