-- 관리자 권한 트리 확장: 게시판관리, 기준정보, 연계관리 섹션 추가
-- AdminView.tsx의 adminMenuConfig.ts에 정의된 전체 메뉴 구조를 AUTH_PERM_TREE에 반영

-- Level 1 섹션 노드 (3개)
INSERT INTO AUTH_PERM_TREE (RSRC_CD, PARENT_CD, RSRC_NM, RSRC_LEVEL, SORT_ORD) VALUES
  ('admin:board-mgmt', 'admin', '게시판관리', 1, 5),
  ('admin:reference',  'admin', '기준정보',   1, 6),
  ('admin:external',   'admin', '연계관리',   1, 7)
ON CONFLICT (RSRC_CD) DO NOTHING;

-- Level 2 그룹/리프 노드
INSERT INTO AUTH_PERM_TREE (RSRC_CD, PARENT_CD, RSRC_NM, RSRC_LEVEL, SORT_ORD) VALUES
  ('admin:notice',              'admin:board-mgmt', '공지사항',     2, 1),
  ('admin:board',               'admin:board-mgmt', '게시판',       2, 2),
  ('admin:qna',                 'admin:board-mgmt', 'QNA',          2, 3),
  ('admin:map-mgmt',            'admin:reference',  '지도관리',     2, 1),
  ('admin:sensitive-map',       'admin:reference',  '민감자원지도', 2, 2),
  ('admin:coast-guard-assets',  'admin:reference',  '해경자산',     2, 3),
  ('admin:collection',          'admin:external',   '수집자료',     2, 1),
  ('admin:monitoring',          'admin:external',   '연계모니터링', 2, 2)
ON CONFLICT (RSRC_CD) DO NOTHING;

-- Level 3 리프 노드
INSERT INTO AUTH_PERM_TREE (RSRC_CD, PARENT_CD, RSRC_NM, RSRC_LEVEL, SORT_ORD) VALUES
  ('admin:map-base',                'admin:map-mgmt',           '지도백데이터',       3, 1),
  ('admin:map-layer',               'admin:map-mgmt',           '레이어',             3, 2),
  ('admin:env-ecology',             'admin:sensitive-map',      '환경/생태',          3, 1),
  ('admin:social-economy',          'admin:sensitive-map',      '사회/경제',          3, 2),
  ('admin:cleanup-equip',           'admin:coast-guard-assets', '방제장비',           3, 1),
  ('admin:asset-upload',            'admin:coast-guard-assets', '자산현행화',         3, 2),
  ('admin:dispersant-zone',         'admin:coast-guard-assets', '유처리제 제한구역',  3, 3),
  ('admin:vessel-materials',        'admin:coast-guard-assets', '방제선 보유자재',    3, 4),
  ('admin:collect-vessel-signal',   'admin:collection',         '선박신호',           3, 1),
  ('admin:collect-hr',              'admin:collection',         '인사정보',           3, 2),
  ('admin:monitor-realtime',        'admin:monitoring',         '실시간 관측자료',    3, 1),
  ('admin:monitor-forecast',        'admin:monitoring',         '수치예측자료',       3, 2),
  ('admin:monitor-vessel',          'admin:monitoring',         '선박위치정보',       3, 3),
  ('admin:monitor-hr',              'admin:monitoring',         '인사',               3, 4)
ON CONFLICT (RSRC_CD) DO NOTHING;

-- AUTH_PERM: 신규 섹션/그룹 노드에 권한 복사
-- admin 권한이 있는 역할에 동일하게 부여 (permResolver의 parent READ gate 충족)
INSERT INTO AUTH_PERM (ROLE_SN, RSRC_CD, OPER_CD, GRANT_YN)
SELECT ap.ROLE_SN, nc.RSRC_CD, ap.OPER_CD, ap.GRANT_YN
FROM AUTH_PERM ap
CROSS JOIN (VALUES
  ('admin:board-mgmt'),
  ('admin:reference'),
  ('admin:external'),
  ('admin:map-mgmt'),
  ('admin:sensitive-map'),
  ('admin:coast-guard-assets'),
  ('admin:collection'),
  ('admin:monitoring')
) AS nc(RSRC_CD)
WHERE ap.RSRC_CD = 'admin'
ON CONFLICT (ROLE_SN, RSRC_CD, OPER_CD) DO NOTHING;
