-- V59: 신규 화면 메뉴 등록 (에이전트팀 개발분)
-- GRP_RULE 하위: 분급비율, 환수등급
-- GRP_SETTLE 하위: 공제차감, 마감관리, 정산정정, 세금계산서, 이의신청, 시책프로그램

-- ============================================================
-- 1. GRP_RULE 하위 신규 PAGE 2개
-- ============================================================
INSERT INTO menu (parent_menu_id, menu_code, menu_name, menu_type, menu_path, component_path, menu_level, sort_order, is_visible, is_active)
SELECT p.menu_id, x.code, x.name, 'PAGE', x.path, x.cmpt, 2, x.sort, 'Y', 'Y'
FROM menu p
JOIN (VALUES
    ('INSTALLMENT_RATIO', '분급비율 설정',   '/rules/installment-ratios', 'rule/InstallmentRatioList', 7),
    ('CHARGEBACK_GRADE',  '환수등급 관리',   '/rules/chargeback-grades',  'rule/ChargebackGradeList',  8)
) AS x(code, name, path, cmpt, sort) ON p.menu_code = 'GRP_RULE'
WHERE NOT EXISTS (SELECT 1 FROM menu WHERE menu_code = x.code);

-- ============================================================
-- 2. GRP_SETTLE 하위 신규 PAGE 6개
-- ============================================================
INSERT INTO menu (parent_menu_id, menu_code, menu_name, menu_type, menu_path, component_path, menu_level, sort_order, is_visible, is_active)
SELECT p.menu_id, x.code, x.name, 'PAGE', x.path, x.cmpt, 2, x.sort, 'Y', 'Y'
FROM menu p
JOIN (VALUES
    ('DEDUCTION',         '공제차감 관리',   '/settle/deductions',   'settle/DeductionList',     10),
    ('PERIOD_CLOSE',      '정산 마감',       '/settle/period-close', 'settle/PeriodClose',        20),
    ('SETTLE_CORRECTION', '정산 정정',       '/settle/corrections',  'settle/SettleCorrection',   30),
    ('TAX_INVOICE',       '세금계산서',      '/settle/tax-invoices', 'settle/TaxInvoiceList',     40),
    ('DISPUTE',           '이의신청',        '/settle/disputes',     'settle/CommissionDispute',  50),
    ('INCENTIVE_PROG',    '시책 프로그램',   '/settle/incentives',   'settle/IncentiveProgram',   60)
) AS x(code, name, path, cmpt, sort) ON p.menu_code = 'GRP_SETTLE'
WHERE NOT EXISTS (SELECT 1 FROM menu WHERE menu_code = x.code);

-- ============================================================
-- 3. 권한 항목 등록 — 규정 도메인 (READ/CREATE/UPDATE)
-- ============================================================
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)) AS p(code,name,sort)
WHERE m.menu_code IN ('INSTALLMENT_RATIO','CHARGEBACK_GRADE')
ON CONFLICT (menu_id, perm_code) DO NOTHING;

-- ============================================================
-- 4. 권한 항목 등록 — 정산 도메인 (READ/CREATE/UPDATE/APPROVE)
-- ============================================================
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)) AS p(code,name,sort)
WHERE m.menu_code IN ('DEDUCTION','PERIOD_CLOSE','SETTLE_CORRECTION','TAX_INVOICE','INCENTIVE_PROG')
ON CONFLICT (menu_id, perm_code) DO NOTHING;

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),('APPROVE','승인',50)) AS p(code,name,sort)
WHERE m.menu_code IN ('DISPUTE','SETTLE_CORRECTION')
ON CONFLICT (menu_id, perm_code) DO NOTHING;

-- ============================================================
-- 5. 역할별 권한 부여
--    SUPER_ADMIN / ADMIN: 전체
--    MANAGER: READ + CREATE + UPDATE + APPROVE
--    AGENT: DEDUCTION/DISPUTE READ 만
-- ============================================================
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 ('INSTALLMENT_RATIO','CHARGEBACK_GRADE',
                      'DEDUCTION','PERIOD_CLOSE','SETTLE_CORRECTION',
                      'TAX_INVOICE','DISPUTE','INCENTIVE_PROG')
ON CONFLICT (role_id, menu_id, perm_code) DO NOTHING;

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','APPROVE')
  AND m.menu_code IN ('INSTALLMENT_RATIO','CHARGEBACK_GRADE',
                      'DEDUCTION','PERIOD_CLOSE','SETTLE_CORRECTION',
                      'TAX_INVOICE','DISPUTE','INCENTIVE_PROG')
ON CONFLICT (role_id, menu_id, perm_code) DO NOTHING;

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 ('DEDUCTION','DISPUTE')
ON CONFLICT (role_id, menu_id, perm_code) DO NOTHING;
