-- WING-OPS Database Schema: Report Tables
-- wing 스키마: report_tmpl, report_tmpl_sect, report_analysis_ctgr,
--              report_ctgr_tmpl, report_ctgr_sect, report, report_sect_data
-- FK 의존성 순서: report_tmpl, report_analysis_ctgr → report → report_sect_data

-- ============================================================
-- 1. 보고서 템플릿 (report_tmpl)
-- ============================================================
CREATE TABLE wing.report_tmpl (
    tmpl_sn     integer NOT NULL,
    tmpl_cd     character varying(30)  NOT NULL,
    tmpl_nm     character varying(100) NOT NULL,
    icon        character varying(10),
    tmpl_dc     character varying(500),
    sort_ord    smallint DEFAULT 0,
    use_yn      character(1) DEFAULT 'Y'::bpchar,
    reg_dtm     timestamp with time zone DEFAULT now(),
    CONSTRAINT report_tmpl_pkey      PRIMARY KEY (tmpl_sn),
    CONSTRAINT report_tmpl_tmpl_cd_key UNIQUE (tmpl_cd)
);

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

ALTER SEQUENCE wing.report_tmpl_tmpl_sn_seq OWNED BY wing.report_tmpl.tmpl_sn;
ALTER TABLE ONLY wing.report_tmpl ALTER COLUMN tmpl_sn SET DEFAULT nextval('wing.report_tmpl_tmpl_sn_seq'::regclass);


-- ============================================================
-- 2. 템플릿 섹션 (report_tmpl_sect)
-- ============================================================
CREATE TABLE wing.report_tmpl_sect (
    sect_sn     integer NOT NULL,
    tmpl_sn     integer NOT NULL,
    sect_cd     character varying(50)  NOT NULL,
    sect_nm     character varying(100) NOT NULL,
    field_def   jsonb                  NOT NULL,
    sort_ord    smallint DEFAULT 0,
    use_yn      character(1) DEFAULT 'Y'::bpchar,
    CONSTRAINT report_tmpl_sect_pkey                   PRIMARY KEY (sect_sn),
    CONSTRAINT report_tmpl_sect_tmpl_sn_sect_cd_key    UNIQUE (tmpl_sn, sect_cd)
);

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

ALTER SEQUENCE wing.report_tmpl_sect_sect_sn_seq OWNED BY wing.report_tmpl_sect.sect_sn;
ALTER TABLE ONLY wing.report_tmpl_sect ALTER COLUMN sect_sn SET DEFAULT nextval('wing.report_tmpl_sect_sect_sn_seq'::regclass);


-- ============================================================
-- 3. 분석 카테고리 (report_analysis_ctgr)
-- ============================================================
CREATE TABLE wing.report_analysis_ctgr (
    ctgr_sn      integer NOT NULL,
    ctgr_cd      character varying(30)  NOT NULL,
    ctgr_nm      character varying(100) NOT NULL,
    icon         character varying(10),
    ctgr_dc      character varying(500),
    color_cd     character varying(30),
    border_color character varying(50),
    bg_active    character varying(50),
    report_nm    character varying(100),
    sort_ord     smallint DEFAULT 0,
    use_yn       character(1) DEFAULT 'Y'::bpchar,
    CONSTRAINT report_analysis_ctgr_pkey       PRIMARY KEY (ctgr_sn),
    CONSTRAINT report_analysis_ctgr_ctgr_cd_key UNIQUE (ctgr_cd)
);

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

ALTER SEQUENCE wing.report_analysis_ctgr_ctgr_sn_seq OWNED BY wing.report_analysis_ctgr.ctgr_sn;
ALTER TABLE ONLY wing.report_analysis_ctgr ALTER COLUMN ctgr_sn SET DEFAULT nextval('wing.report_analysis_ctgr_ctgr_sn_seq'::regclass);


-- ============================================================
-- 4. 카테고리 템플릿 (report_ctgr_tmpl)
-- ============================================================
CREATE TABLE wing.report_ctgr_tmpl (
    ctgr_tmpl_sn integer NOT NULL,
    ctgr_sn      integer NOT NULL,
    icon         character varying(10),
    label        character varying(50) NOT NULL,
    sort_ord     smallint DEFAULT 0,
    CONSTRAINT report_ctgr_tmpl_pkey PRIMARY KEY (ctgr_tmpl_sn)
);

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

ALTER SEQUENCE wing.report_ctgr_tmpl_ctgr_tmpl_sn_seq OWNED BY wing.report_ctgr_tmpl.ctgr_tmpl_sn;
ALTER TABLE ONLY wing.report_ctgr_tmpl ALTER COLUMN ctgr_tmpl_sn SET DEFAULT nextval('wing.report_ctgr_tmpl_ctgr_tmpl_sn_seq'::regclass);


-- ============================================================
-- 5. 카테고리 섹션 (report_ctgr_sect)
-- ============================================================
CREATE TABLE wing.report_ctgr_sect (
    sect_sn  integer NOT NULL,
    ctgr_sn  integer NOT NULL,
    sect_cd  character varying(50)  NOT NULL,
    sect_nm  character varying(100) NOT NULL,
    icon     character varying(10),
    sect_dc  character varying(200),
    dflt_yn  character(1) DEFAULT 'Y'::bpchar,
    sort_ord smallint DEFAULT 0,
    CONSTRAINT report_ctgr_sect_pkey                    PRIMARY KEY (sect_sn),
    CONSTRAINT report_ctgr_sect_ctgr_sn_sect_cd_key     UNIQUE (ctgr_sn, sect_cd)
);

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

ALTER SEQUENCE wing.report_ctgr_sect_sect_sn_seq OWNED BY wing.report_ctgr_sect.sect_sn;
ALTER TABLE ONLY wing.report_ctgr_sect ALTER COLUMN sect_sn SET DEFAULT nextval('wing.report_ctgr_sect_sect_sn_seq'::regclass);


-- ============================================================
-- 6. 보고서 (report)
-- 의존: report_tmpl, report_analysis_ctgr, auth.auth_user, wing.acdnt
-- ============================================================
CREATE TABLE wing.report (
    report_sn integer NOT NULL,
    tmpl_sn   integer,
    ctgr_sn   integer,
    acdnt_sn  integer,
    title     character varying(200) NOT NULL,
    jrsd_cd   character varying(20),
    stts_cd   character varying(20)  DEFAULT 'DRAFT'::character varying,
    author_id uuid                   NOT NULL,
    use_yn    character(1)           DEFAULT 'Y'::bpchar,
    reg_dtm   timestamp with time zone DEFAULT now(),
    mdfcn_dtm timestamp with time zone,
    CONSTRAINT report_pkey PRIMARY KEY (report_sn),
    CONSTRAINT ck_report_status CHECK (
        ((stts_cd)::text = ANY ((ARRAY['DRAFT'::character varying, 'IN_PROGRESS'::character varying, 'COMPLETED'::character varying])::text[]))
    )
);

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

ALTER SEQUENCE wing.report_report_sn_seq OWNED BY wing.report.report_sn;
ALTER TABLE ONLY wing.report ALTER COLUMN report_sn SET DEFAULT nextval('wing.report_report_sn_seq'::regclass);


-- ============================================================
-- 7. 보고서 섹션 데이터 (report_sect_data)
-- 의존: report
-- ============================================================
CREATE TABLE wing.report_sect_data (
    data_sn    integer NOT NULL,
    report_sn  integer NOT NULL,
    sect_cd    character varying(50) NOT NULL,
    include_yn character(1)          DEFAULT 'Y'::bpchar,
    sect_data  jsonb                 DEFAULT '{}'::jsonb NOT NULL,
    sort_ord   smallint              DEFAULT 0,
    CONSTRAINT report_sect_data_pkey                     PRIMARY KEY (data_sn),
    CONSTRAINT report_sect_data_report_sn_sect_cd_key    UNIQUE (report_sn, sect_cd)
);

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

ALTER SEQUENCE wing.report_sect_data_data_sn_seq OWNED BY wing.report_sect_data.data_sn;
ALTER TABLE ONLY wing.report_sect_data ALTER COLUMN data_sn SET DEFAULT nextval('wing.report_sect_data_data_sn_seq'::regclass);


-- ============================================================
-- FK 제약조건
-- ============================================================
ALTER TABLE ONLY wing.report_tmpl_sect
    ADD CONSTRAINT report_tmpl_sect_tmpl_sn_fkey
    FOREIGN KEY (tmpl_sn) REFERENCES wing.report_tmpl(tmpl_sn);

ALTER TABLE ONLY wing.report_ctgr_tmpl
    ADD CONSTRAINT report_ctgr_tmpl_ctgr_sn_fkey
    FOREIGN KEY (ctgr_sn) REFERENCES wing.report_analysis_ctgr(ctgr_sn);

ALTER TABLE ONLY wing.report_ctgr_sect
    ADD CONSTRAINT report_ctgr_sect_ctgr_sn_fkey
    FOREIGN KEY (ctgr_sn) REFERENCES wing.report_analysis_ctgr(ctgr_sn);

-- 크로스스키마 FK: report.author_id → auth.auth_user
ALTER TABLE ONLY wing.report
    ADD CONSTRAINT report_author_id_fkey
    FOREIGN KEY (author_id) REFERENCES auth.auth_user(user_id);

ALTER TABLE ONLY wing.report
    ADD CONSTRAINT report_tmpl_sn_fkey
    FOREIGN KEY (tmpl_sn) REFERENCES wing.report_tmpl(tmpl_sn);

ALTER TABLE ONLY wing.report
    ADD CONSTRAINT report_ctgr_sn_fkey
    FOREIGN KEY (ctgr_sn) REFERENCES wing.report_analysis_ctgr(ctgr_sn);

ALTER TABLE ONLY wing.report_sect_data
    ADD CONSTRAINT report_sect_data_report_sn_fkey
    FOREIGN KEY (report_sn) REFERENCES wing.report(report_sn) ON DELETE CASCADE;
