-- ============================================================
-- 020: 본청방제과 역할 추가
-- ============================================================

-- 역할 추가 (이미 존재하면 무시)
INSERT INTO AUTH_ROLE (ROLE_CD, ROLE_NM, ROLE_DC, DFLT_YN)
SELECT 'HQ_CLEANUP', '본청방제과', '본청 방제 업무 관리 권한', 'N'
WHERE NOT EXISTS (SELECT 1 FROM AUTH_ROLE WHERE ROLE_CD = 'HQ_CLEANUP');

-- 본청방제과 권한 설정: 방제 관련 탭 RCUD + 기타 탭 READ/CREATE, admin 제외
DO $$
DECLARE
  v_role_sn INT;
BEGIN
  SELECT ROLE_SN INTO v_role_sn FROM AUTH_ROLE WHERE ROLE_CD = 'HQ_CLEANUP';

  -- 기존 권한 초기화 (재실행 안전)
  DELETE FROM AUTH_PERM WHERE ROLE_SN = v_role_sn;

  INSERT INTO AUTH_PERM (ROLE_SN, RSRC_CD, OPER_CD, GRANT_YN) VALUES
  (v_role_sn, 'prediction', 'READ', 'Y'), (v_role_sn, 'prediction', 'CREATE', 'Y'), (v_role_sn, 'prediction', 'UPDATE', 'Y'), (v_role_sn, 'prediction', 'DELETE', 'Y'),
  (v_role_sn, 'hns',        'READ', 'Y'), (v_role_sn, 'hns',        'CREATE', 'Y'), (v_role_sn, 'hns',        'UPDATE', 'Y'), (v_role_sn, 'hns',        'DELETE', 'Y'),
  (v_role_sn, 'rescue',     'READ', 'Y'), (v_role_sn, 'rescue',     'CREATE', 'Y'), (v_role_sn, 'rescue',     'UPDATE', 'Y'), (v_role_sn, 'rescue',     'DELETE', 'Y'),
  (v_role_sn, 'reports',    'READ', 'Y'), (v_role_sn, 'reports',    'CREATE', 'Y'), (v_role_sn, 'reports',    'UPDATE', 'Y'), (v_role_sn, 'reports',    'DELETE', 'Y'),
  (v_role_sn, 'aerial',     'READ', 'Y'), (v_role_sn, 'aerial',     'CREATE', 'Y'), (v_role_sn, 'aerial',     'UPDATE', 'Y'), (v_role_sn, 'aerial',     'DELETE', 'Y'),
  (v_role_sn, 'assets',     'READ', 'Y'), (v_role_sn, 'assets',     'CREATE', 'Y'), (v_role_sn, 'assets',     'UPDATE', 'Y'), (v_role_sn, 'assets',     'DELETE', 'Y'),
  (v_role_sn, 'scat',       'READ', 'Y'), (v_role_sn, 'scat',       'CREATE', 'Y'), (v_role_sn, 'scat',       'UPDATE', 'Y'), (v_role_sn, 'scat',       'DELETE', 'Y'),
  (v_role_sn, 'incidents',  'READ', 'Y'), (v_role_sn, 'incidents',  'CREATE', 'Y'), (v_role_sn, 'incidents',  'UPDATE', 'Y'), (v_role_sn, 'incidents',  'DELETE', 'Y'),
  (v_role_sn, 'board',      'READ', 'Y'), (v_role_sn, 'board',      'CREATE', 'Y'), (v_role_sn, 'board',      'UPDATE', 'Y'),
  (v_role_sn, 'weather',    'READ', 'Y'), (v_role_sn, 'weather',    'CREATE', 'Y'),
  (v_role_sn, 'admin',      'READ', 'N');
END $$;
