-- V104: incar 갭 운영 도메인 7개 PAGE 메뉴 등록 (V95 패턴 동일)
--       GRP_SETTLE 하위: STEP_CLOSE, HQ_SETTLE
--       GRP_ORG    하위: GRADE_EVAL, GUARANTEE_INS
--       GRP_PRD    하위: JOINT_CONTRACT
--       GRP_COMMISSION 하위: RESERVE_FUND, COMM_SIMULATOR
--
--       권한:
--         STEP_CLOSE        → READ/UPDATE/APPROVE
--         나머지 6개         → READ/CREATE/UPDATE/DELETE/EXPORT
--       역할:
--         SUPER_ADMIN/ADMIN → 전체 권한
--         MANAGER           → READ/CREATE/UPDATE/EXPORT (+ APPROVE for STEP_CLOSE, DELETE 제외)
--         AGENT             → 본인 관련 도메인 READ (RESERVE_FUND, GUARANTEE_INS, GRADE_EVAL)

-- ============================================================
-- 1. GRP_SETTLE 하위: STEP_CLOSE, HQ_SETTLE
-- ============================================================

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
    ('STEP_CLOSE', '단계마감',  '/settle/step-close', 'settle/StepClose',  90),
    ('HQ_SETTLE',  '본사정산',  '/settle/hq-settle',  'settle/HqSettle',  100)
) 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);

-- ============================================================
-- 2. GRP_ORG 하위: GRADE_EVAL, GUARANTEE_INS
-- ============================================================

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
    ('GRADE_EVAL',     '등급평가', '/org/grade-eval',  'org/GradeEvaluation',     60),
    ('GUARANTEE_INS',  '보증보험', '/org/guarantee',   'org/GuaranteeInsurance',  70)
) AS x(code, name, path, cmpt, sort)
ON p.menu_code = 'GRP_ORG'
WHERE NOT EXISTS (SELECT 1 FROM menu WHERE menu_code = x.code);

-- ============================================================
-- 3. GRP_PRD 하위: JOINT_CONTRACT
-- ============================================================

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
    ('JOINT_CONTRACT', '공동계약', '/contracts/joint', 'contracts/JointContract', 60)
) AS x(code, name, path, cmpt, sort)
ON p.menu_code = 'GRP_PRD'
WHERE NOT EXISTS (SELECT 1 FROM menu WHERE menu_code = x.code);

-- ============================================================
-- 4. GRP_COMMISSION 하위: RESERVE_FUND, COMM_SIMULATOR
-- ============================================================

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
    ('RESERVE_FUND',    '적립금',            '/commission/reserve',   'commission/ReserveFund',            150),
    ('COMM_SIMULATOR',  '수수료 시뮬레이터', '/commission/simulator', 'commission/CommissionSimulator',    160)
) 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);

-- ============================================================
-- 5. STEP_CLOSE 권한 등록 (READ/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),
    ('UPDATE',  '수정', 30),
    ('APPROVE', '승인', 50)
) AS p(code, name, sort)
WHERE m.menu_code = 'STEP_CLOSE'
ON CONFLICT (menu_id, perm_code) DO NOTHING;

-- ============================================================
-- 6. 나머지 6개 메뉴 권한 등록 (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 ('HQ_SETTLE','GRADE_EVAL','JOINT_CONTRACT','GUARANTEE_INS','RESERVE_FUND','COMM_SIMULATOR')
ON CONFLICT (menu_id, perm_code) DO NOTHING;

-- ============================================================
-- 7. 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 ('STEP_CLOSE','HQ_SETTLE','GRADE_EVAL','JOINT_CONTRACT','GUARANTEE_INS','RESERVE_FUND','COMM_SIMULATOR')
ON CONFLICT (role_id, menu_id, perm_code) DO NOTHING;

-- ============================================================
-- 8. MANAGER — READ/CREATE/UPDATE/EXPORT + APPROVE(STEP_CLOSE)
-- ============================================================

-- STEP_CLOSE: READ/UPDATE/APPROVE
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','UPDATE','APPROVE')
  AND m.menu_code = 'STEP_CLOSE'
ON CONFLICT (role_id, menu_id, perm_code) DO NOTHING;

-- 나머지 6개: 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 ('HQ_SETTLE','GRADE_EVAL','JOINT_CONTRACT','GUARANTEE_INS','RESERVE_FUND','COMM_SIMULATOR')
ON CONFLICT (role_id, menu_id, perm_code) DO NOTHING;

-- ============================================================
-- 9. AGENT — 본인 관련 도메인 READ (RESERVE_FUND, GUARANTEE_INS, GRADE_EVAL)
-- ============================================================

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