-- ============================================================
-- AX HUB 역할-Tool/지식 권한 관리 DDL
-- 대상 스키마 : 프로젝트 AP 스키마 (S_EIAM_TIS 는 읽기전용 참조)
-- 작성일 : 2026-06-25
-- ============================================================

-- ① Tool 마스터
CREATE TABLE AX_TOOL_MST (
    TOOL_ID          VARCHAR(20)  NOT NULL,   -- Tool ID (PK)
    TOOL_NM          VARCHAR(100) NOT NULL,   -- Tool명
    TOOL_DS          VARCHAR(500),            -- Tool설명
    TOOL_TYPE_CD     VARCHAR(20),             -- Tool유형코드 (MCP / FUNC / EXT)
    PUSE_YN          VARCHAR(1)   DEFAULT 'Y',-- 사용여부
    SYST_RGI_DT      DATE,
    SYST_RGI_PRAF_NO VARCHAR(8),
    SYST_RGI_OGNZ_NO VARCHAR(7),
    SYST_RGI_SYST_CD VARCHAR(3),
    SYST_RGI_PRGR_ID VARCHAR(100),
    SYST_CHG_DT      DATE,
    SYST_CHG_PRAF_NO VARCHAR(8),
    SYST_CHG_OGNZ_NO VARCHAR(7),
    SYST_CHG_SYST_CD VARCHAR(3),
    SYST_CHG_PRGR_ID VARCHAR(100),
    CONSTRAINT PK_AX_TOOL_MST PRIMARY KEY (TOOL_ID)
);

COMMENT ON TABLE  AX_TOOL_MST              IS 'AX HUB Tool 마스터';
COMMENT ON COLUMN AX_TOOL_MST.TOOL_ID      IS 'Tool ID (PK)';
COMMENT ON COLUMN AX_TOOL_MST.TOOL_NM      IS 'Tool명';
COMMENT ON COLUMN AX_TOOL_MST.TOOL_DS      IS 'Tool설명';
COMMENT ON COLUMN AX_TOOL_MST.TOOL_TYPE_CD IS 'Tool유형코드 (MCP/FUNC/EXT)';
COMMENT ON COLUMN AX_TOOL_MST.PUSE_YN      IS '사용여부 (Y/N)';


-- ② 지식(Knowledge) 마스터
CREATE TABLE AX_KNWL_MST (
    KNWL_ID          VARCHAR(20)  NOT NULL,   -- 지식 ID (PK)
    KNWL_NM          VARCHAR(100) NOT NULL,   -- 지식명
    KNWL_DS          VARCHAR(500),            -- 지식설명
    KNWL_TYPE_CD     VARCHAR(20),             -- 지식유형코드 (PRODUCT/MANUAL/LEGAL 등)
    PUSE_YN          VARCHAR(1)   DEFAULT 'Y',
    SYST_RGI_DT      DATE,
    SYST_RGI_PRAF_NO VARCHAR(8),
    SYST_RGI_OGNZ_NO VARCHAR(7),
    SYST_RGI_SYST_CD VARCHAR(3),
    SYST_RGI_PRGR_ID VARCHAR(100),
    SYST_CHG_DT      DATE,
    SYST_CHG_PRAF_NO VARCHAR(8),
    SYST_CHG_OGNZ_NO VARCHAR(7),
    SYST_CHG_SYST_CD VARCHAR(3),
    SYST_CHG_PRGR_ID VARCHAR(100),
    CONSTRAINT PK_AX_KNWL_MST PRIMARY KEY (KNWL_ID)
);

COMMENT ON TABLE  AX_KNWL_MST              IS 'AX HUB 지식 마스터';
COMMENT ON COLUMN AX_KNWL_MST.KNWL_ID      IS '지식 ID (PK)';
COMMENT ON COLUMN AX_KNWL_MST.KNWL_NM      IS '지식명';
COMMENT ON COLUMN AX_KNWL_MST.KNWL_DS      IS '지식설명';
COMMENT ON COLUMN AX_KNWL_MST.KNWL_TYPE_CD IS '지식유형코드 (PRODUCT/MANUAL/LEGAL/REPORT)';
COMMENT ON COLUMN AX_KNWL_MST.PUSE_YN      IS '사용여부 (Y/N)';


-- ③ 역할-Tool 권한 매핑
--   SYST_ID + ROLE_NO → S_EIAM_TIS.AA_ROLE 참조 (FK 미설정: 스키마 분리 환경)
CREATE TABLE AX_ROLE_TOOL_ATHR (
    ROLE_TOOL_ATHR_ID VARCHAR(20)  NOT NULL,  -- 역할Tool권한ID (PK)
    SYST_ID           VARCHAR(10)  NOT NULL,  -- 시스템ID (EIAM 연동 키)
    ROLE_NO           VARCHAR(50)  NOT NULL,  -- 역할번호 (EIAM AA_ROLE.ROLE_NO)
    TOOL_ID           VARCHAR(20)  NOT NULL,  -- Tool ID
    PUSE_YN           VARCHAR(1)   DEFAULT 'Y',
    SYST_RGI_DT       DATE,
    SYST_RGI_PRAF_NO  VARCHAR(8),
    SYST_RGI_OGNZ_NO  VARCHAR(7),
    SYST_RGI_SYST_CD  VARCHAR(3),
    SYST_RGI_PRGR_ID  VARCHAR(100),
    SYST_CHG_DT       DATE,
    SYST_CHG_PRAF_NO  VARCHAR(8),
    SYST_CHG_OGNZ_NO  VARCHAR(7),
    SYST_CHG_SYST_CD  VARCHAR(3),
    SYST_CHG_PRGR_ID  VARCHAR(100),
    CONSTRAINT PK_AX_ROLE_TOOL_ATHR PRIMARY KEY (ROLE_TOOL_ATHR_ID)
);

-- (SYST_ID, ROLE_NO, TOOL_ID) 중복 방지
CREATE UNIQUE INDEX UK_AX_ROLE_TOOL_ATHR
    ON AX_ROLE_TOOL_ATHR (SYST_ID, ROLE_NO, TOOL_ID);

COMMENT ON TABLE  AX_ROLE_TOOL_ATHR                   IS '역할-Tool 권한 매핑';
COMMENT ON COLUMN AX_ROLE_TOOL_ATHR.ROLE_TOOL_ATHR_ID IS '역할Tool권한ID (PK)';
COMMENT ON COLUMN AX_ROLE_TOOL_ATHR.SYST_ID           IS '시스템ID';
COMMENT ON COLUMN AX_ROLE_TOOL_ATHR.ROLE_NO           IS '역할번호 (EIAM)';
COMMENT ON COLUMN AX_ROLE_TOOL_ATHR.TOOL_ID           IS 'Tool ID';
COMMENT ON COLUMN AX_ROLE_TOOL_ATHR.PUSE_YN           IS '사용여부';


-- ④ 역할-지식 권한 매핑
CREATE TABLE AX_ROLE_KNWL_ATHR (
    ROLE_KNWL_ATHR_ID VARCHAR(20)  NOT NULL,  -- 역할지식권한ID (PK)
    SYST_ID           VARCHAR(10)  NOT NULL,
    ROLE_NO           VARCHAR(50)  NOT NULL,
    KNWL_ID           VARCHAR(20)  NOT NULL,
    PUSE_YN           VARCHAR(1)   DEFAULT 'Y',
    SYST_RGI_DT       DATE,
    SYST_RGI_PRAF_NO  VARCHAR(8),
    SYST_RGI_OGNZ_NO  VARCHAR(7),
    SYST_RGI_SYST_CD  VARCHAR(3),
    SYST_RGI_PRGR_ID  VARCHAR(100),
    SYST_CHG_DT       DATE,
    SYST_CHG_PRAF_NO  VARCHAR(8),
    SYST_CHG_OGNZ_NO  VARCHAR(7),
    SYST_CHG_SYST_CD  VARCHAR(3),
    SYST_CHG_PRGR_ID  VARCHAR(100),
    CONSTRAINT PK_AX_ROLE_KNWL_ATHR PRIMARY KEY (ROLE_KNWL_ATHR_ID)
);

CREATE UNIQUE INDEX UK_AX_ROLE_KNWL_ATHR
    ON AX_ROLE_KNWL_ATHR (SYST_ID, ROLE_NO, KNWL_ID);

COMMENT ON TABLE  AX_ROLE_KNWL_ATHR                   IS '역할-지식 권한 매핑';
COMMENT ON COLUMN AX_ROLE_KNWL_ATHR.ROLE_KNWL_ATHR_ID IS '역할지식권한ID (PK)';
COMMENT ON COLUMN AX_ROLE_KNWL_ATHR.SYST_ID           IS '시스템ID';
COMMENT ON COLUMN AX_ROLE_KNWL_ATHR.ROLE_NO           IS '역할번호 (EIAM)';
COMMENT ON COLUMN AX_ROLE_KNWL_ATHR.KNWL_ID           IS '지식ID';
COMMENT ON COLUMN AX_ROLE_KNWL_ATHR.PUSE_YN           IS '사용여부';


-- ============================================================
-- 샘플 데이터 (개발용)
-- ============================================================
INSERT INTO AX_TOOL_MST
    (TOOL_ID, TOOL_NM, TOOL_DS, TOOL_TYPE_CD, PUSE_YN,
     SYST_RGI_DT, SYST_RGI_PRAF_NO, SYST_RGI_OGNZ_NO, SYST_RGI_SYST_CD, SYST_RGI_PRGR_ID,
     SYST_CHG_DT, SYST_CHG_PRAF_NO, SYST_CHG_OGNZ_NO, SYST_CHG_SYST_CD, SYST_CHG_PRGR_ID)
VALUES
    ('TOOL_A', '지식 검색', '지식베이스 내 문서 의미 기반 검색', 'MCP', 'Y',
     CURRENT_DATE, 'SYSTEM', 'SYSTEM', 'AXH', 'SOATM0100',
     CURRENT_DATE, 'SYSTEM', 'SYSTEM', 'AXH', 'SOATM0100'),
    ('TOOL_B', 'DB 조회', '사내 데이터베이스 직접 조회 (GLOW)', 'MCP', 'Y',
     CURRENT_DATE, 'SYSTEM', 'SYSTEM', 'AXH', 'SOATM0100',
     CURRENT_DATE, 'SYSTEM', 'SYSTEM', 'AXH', 'SOATM0100'),
    ('TOOL_C', '문서 생성', 'AI 기반 문서 초안 자동 생성', 'FUNC', 'Y',
     CURRENT_DATE, 'SYSTEM', 'SYSTEM', 'AXH', 'SOATM0100',
     CURRENT_DATE, 'SYSTEM', 'SYSTEM', 'AXH', 'SOATM0100');

INSERT INTO AX_KNWL_MST
    (KNWL_ID, KNWL_NM, KNWL_DS, KNWL_TYPE_CD, PUSE_YN,
     SYST_RGI_DT, SYST_RGI_PRAF_NO, SYST_RGI_OGNZ_NO, SYST_RGI_SYST_CD, SYST_RGI_PRGR_ID,
     SYST_CHG_DT, SYST_CHG_PRAF_NO, SYST_CHG_OGNZ_NO, SYST_CHG_SYST_CD, SYST_CHG_PRGR_ID)
VALUES
    ('KNWL_Q', '상품기초서류', '보험 상품 기초서류 약 8만건', 'PRODUCT', 'Y',
     CURRENT_DATE, 'SYSTEM', 'SYSTEM', 'AXH', 'SOATM0100',
     CURRENT_DATE, 'SYSTEM', 'SYSTEM', 'AXH', 'SOATM0100'),
    ('KNWL_W', '업무매뉴얼', '내부 업무 매뉴얼 및 프로세스 가이드', 'MANUAL', 'Y',
     CURRENT_DATE, 'SYSTEM', 'SYSTEM', 'AXH', 'SOATM0100',
     CURRENT_DATE, 'SYSTEM', 'SYSTEM', 'AXH', 'SOATM0100'),
    ('KNWL_E', '법률/규정', '보험업법 및 금융당국 규정', 'LEGAL', 'Y',
     CURRENT_DATE, 'SYSTEM', 'SYSTEM', 'AXH', 'SOATM0100',
     CURRENT_DATE, 'SYSTEM', 'SYSTEM', 'AXH', 'SOATM0100');

COMMIT;
