-- V015: AI 메뉴 슈퍼관리자 권한 그룹 자동 매핑
-- AUTHORITY_MASTER 에서 COMPANY_CODE='*' & AUTH_CODE='SUPER_ADMIN' 인 권한 그룹을 찾아
-- V014 에서 등록된 AI 메뉴 8개(루트 + 하위 7개)를 AUTHORITY_SUB_MENU 에 삽입합니다.
-- 멱등성: ON CONFLICT (MASTER_OBJID, MENU_OBJID) DO NOTHING

INSERT INTO AUTHORITY_SUB_MENU (
    OBJID
    , MASTER_OBJID
    , MENU_OBJID
    , CREATE_YN
    , READ_YN
    , UPDATE_YN
    , DELETE_YN
    , WRITER
    , CREATED_DATE
)
SELECT
    AM.OBJID || '_' || MI.OBJID
    , AM.OBJID
    , MI.OBJID
    , 'Y'
    , 'Y'
    , 'Y'
    , 'Y'
    , 'system'
    , NOW()
FROM AUTHORITY_MASTER AM
CROSS JOIN MENU_INFO MI
WHERE AM.AUTH_CODE   = 'SUPER_ADMIN'
  AND AM.COMPANY_CODE = '*'
  AND MI.OBJID IN (
      'AI_ASSISTANT_ROOT'
    , 'AI_MENU_ASSISTANT'
    , 'AI_MENU_AGENTS'
    , 'AI_MENU_PROVIDERS'
    , 'AI_MENU_WORKSPACE'
    , 'AI_MENU_CONVERSATIONS'
    , 'AI_MENU_API_KEYS'
    , 'AI_MENU_KNOWLEDGE'
  )
ON CONFLICT (MASTER_OBJID, MENU_OBJID) DO NOTHING;
