-- V95: incar 갭 수수료 도메인 7개 PAGE 메뉴 등록
--      GRP_COMMISSION 하위 PAGE 7개 추가 (V54 패턴 동일).
--      권한: READ/CREATE/UPDATE/DELETE/EXPORT
--      역할: SUPER_ADMIN/ADMIN 전체, MANAGER READ/CREATE/UPDATE/EXPORT,
--            AGENT READ (INCOME_COMM 제외 — 본인 관련 도메인만)

-- ============================================================
-- 1. GRP_COMMISSION 하위 PAGE 7개 (incar P7)
-- ============================================================

INSERT INTO menu (parent_menu_id, menu_code, menu_name, menu_type, menu_path, component_path, menu_level, sort_order)
SELECT p.menu_id, x.code, x.name, 'PAGE', x.path, x.cmpt, 2, x.sort
FROM menu p
JOIN (VALUES
    ('INCOME_COMM',          '수입수수료/수지차',      '/commission/income',           'commission/IncomeCommission',  80),
    ('REFERRAL_COMM',        '소개·이관수수료',        '/commission/referral',         'commission/ReferralCommission',90),
    ('SETTLING_SUPPORT',     '정착지원금',             '/commission/settling-support', 'commission/SettlingSupport',  100),
    ('RECOGNIZED_PERF',      '인정실적',               '/commission/recognized',       'commission/RecognizedPerformance',110),
    ('AWARD',                '시상',                   '/commission/award',            'commission/Award',            120),
    ('LIFE_OP_SUPPORT',      '생보운영지원수수료',      '/commission/life-support',     'commission/LifeSupport',      130),
    ('BRANCH_MGR_ALLOWANCE', '지점장수당',             '/commission/branch-allowance', 'commission/BranchAllowance',  140)
) AS x(code, name, path, cmpt, sort)
ON p.menu_code = 'GRP_COMMISSION'
WHERE NOT EXISTS (SELECT 1 FROM menu WHERE menu_code = x.code);

-- ============================================================
-- 2. 7개 메뉴에 권한 항목 등록 (READ/CREATE/UPDATE/DELETE/EXPORT)
-- ============================================================

INSERT INTO menu_permission (menu_id, perm_code, perm_name, sort_order)
SELECT m.menu_id, p.code, p.name, p.sort
FROM menu m
CROSS JOIN (VALUES
    ('READ',   '조회', 10),
    ('CREATE', '등록', 20),
    ('UPDATE', '수정', 30),
    ('DELETE', '삭제', 40),
    ('EXPORT', '엑셀', 60)
) AS p(code, name, sort)
WHERE m.menu_code IN (
    'INCOME_COMM','REFERRAL_COMM','SETTLING_SUPPORT',
    'RECOGNIZED_PERF','AWARD','LIFE_OP_SUPPORT','BRANCH_MGR_ALLOWANCE'
)
ON CONFLICT (menu_id, perm_code) DO NOTHING;

-- ============================================================
-- 3. 역할별 권한 부여
-- ============================================================

-- SUPER_ADMIN / ADMIN — 7개 메뉴 전체 권한
INSERT INTO role_menu_permission (role_id, menu_id, perm_code, is_granted)
SELECT r.role_id, mp.menu_id, mp.perm_code, 'Y'
FROM role r
CROSS JOIN menu_permission mp
JOIN menu m ON m.menu_id = mp.menu_id
WHERE r.role_code IN ('SUPER_ADMIN','ADMIN')
  AND m.menu_code IN (
      'INCOME_COMM','REFERRAL_COMM','SETTLING_SUPPORT',
      'RECOGNIZED_PERF','AWARD','LIFE_OP_SUPPORT','BRANCH_MGR_ALLOWANCE'
  )
ON CONFLICT (role_id, menu_id, perm_code) DO NOTHING;

-- MANAGER — READ/CREATE/UPDATE/EXPORT (DELETE 제외)
INSERT INTO role_menu_permission (role_id, menu_id, perm_code, is_granted)
SELECT r.role_id, mp.menu_id, mp.perm_code, 'Y'
FROM role r
CROSS JOIN menu_permission mp
JOIN menu m ON m.menu_id = mp.menu_id
WHERE r.role_code = 'MANAGER'
  AND mp.perm_code IN ('READ','CREATE','UPDATE','EXPORT')
  AND m.menu_code IN (
      'INCOME_COMM','REFERRAL_COMM','SETTLING_SUPPORT',
      'RECOGNIZED_PERF','AWARD','LIFE_OP_SUPPORT','BRANCH_MGR_ALLOWANCE'
  )
ON CONFLICT (role_id, menu_id, perm_code) DO NOTHING;

-- AGENT — INCOME_COMM 제외하고 READ 만
--         (INCOME_COMM은 보험사-GA 수입 측 데이터로 설계사 공개 불가)
INSERT INTO role_menu_permission (role_id, menu_id, perm_code, is_granted)
SELECT r.role_id, mp.menu_id, mp.perm_code, 'Y'
FROM role r
CROSS JOIN menu_permission mp
JOIN menu m ON m.menu_id = mp.menu_id
WHERE r.role_code = 'AGENT'
  AND mp.perm_code = 'READ'
  AND m.menu_code IN (
      'REFERRAL_COMM','SETTLING_SUPPORT',
      'RECOGNIZED_PERF','AWARD','LIFE_OP_SUPPORT','BRANCH_MGR_ALLOWANCE'
  )
ON CONFLICT (role_id, menu_id, perm_code) DO NOTHING;
