-- V73: P5-W2 WITHHOLDING_TAX / VAT_REPORT 메뉴 EXECUTE 권한 누락 보강
-- V69에서 READ/CREATE/UPDATE/EXPORT만 등록되어 regenerate/complete 엔드포인트 403 발생
-- (P5-W1 ANNUAL_INCOME 의 V72 와 동일 패턴)
-- SUPER_ADMIN / ADMIN 역할에 EXECUTE 권한 부여

-- 1. menu_permission에 EXECUTE 항목 추가
INSERT INTO menu_permission (menu_id, perm_code, perm_name, sort_order)
SELECT m.menu_id, 'EXECUTE', '실행', 70
FROM menu m
WHERE m.menu_code IN ('WITHHOLDING_TAX', 'VAT_REPORT')
  AND NOT EXISTS (
      SELECT 1 FROM menu_permission mp2
      WHERE mp2.menu_id = m.menu_id AND mp2.perm_code = 'EXECUTE'
  );

-- 2. SUPER_ADMIN / ADMIN에 EXECUTE 권한 부여
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 mp.perm_code = 'EXECUTE'
  AND m.menu_code IN ('WITHHOLDING_TAX', 'VAT_REPORT')
  AND NOT EXISTS (
      SELECT 1 FROM role_menu_permission rmp2
      WHERE rmp2.role_id = r.role_id
        AND rmp2.menu_id = mp.menu_id
        AND rmp2.perm_code = 'EXECUTE'
  );
