-- =====================================================
-- SAM ERP 메뉴 추가 쿼리
-- 생성일: 2025-12-01
-- 대상: 인사관리, 전자결재, 기준정보 확장 등
-- 참고문서: SAM_ERP_인사관리전자결재_Storyboard_D0.6_251201.pdf
-- =====================================================

-- 총 추가 메뉴: 23개
-- - 최상위 9개: 인사관리, 전자결재, 게시판, 보고서 및 분석, 계정정보, 회사정보, 구독관리, 결제내역, 고객센터
-- - 인사관리 하위 3개: 근태관리, 휴가관리, 급여관리
-- - 전자결재 하위 3개: 기안함, 결재함, 참조함
-- - 기준정보 관리 하위 8개: 직급관리, 직책관리, 근무관리, 휴가설정, 팝업관리, 게시판관리, 일반설정, 알림설정

-- =====================================================
-- 1. 인사관리 (최상위 메뉴)
-- =====================================================
INSERT INTO menus (tenant_id, parent_id, name, url, icon, sort_order, is_active, hidden, is_external, external_url, created_at, updated_at)
SELECT NULL, NULL, '인사관리', '/hr', 'users', 19, 1, 0, 0, NULL, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM menus WHERE name = '인사관리' AND tenant_id IS NULL AND deleted_at IS NULL);

-- 인사관리 하위 메뉴
INSERT INTO menus (tenant_id, parent_id, name, url, icon, sort_order, is_active, hidden, is_external, external_url, created_at, updated_at)
SELECT NULL, (SELECT id FROM menus WHERE name = '인사관리' AND tenant_id IS NULL AND deleted_at IS NULL LIMIT 1),
       '근태관리', '/hr/attendance', 'clock', 1, 1, 0, 0, NULL, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM menus WHERE name = '근태관리' AND tenant_id IS NULL AND deleted_at IS NULL);

INSERT INTO menus (tenant_id, parent_id, name, url, icon, sort_order, is_active, hidden, is_external, external_url, created_at, updated_at)
SELECT NULL, (SELECT id FROM menus WHERE name = '인사관리' AND tenant_id IS NULL AND deleted_at IS NULL LIMIT 1),
       '휴가관리', '/hr/leave', 'calendar', 2, 1, 0, 0, NULL, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM menus WHERE name = '휴가관리' AND tenant_id IS NULL AND deleted_at IS NULL);

INSERT INTO menus (tenant_id, parent_id, name, url, icon, sort_order, is_active, hidden, is_external, external_url, created_at, updated_at)
SELECT NULL, (SELECT id FROM menus WHERE name = '인사관리' AND tenant_id IS NULL AND deleted_at IS NULL LIMIT 1),
       '급여관리', '/hr/payroll', 'dollar-sign', 3, 1, 0, 0, NULL, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM menus WHERE name = '급여관리' AND tenant_id IS NULL AND deleted_at IS NULL);

-- =====================================================
-- 2. 전자결재 (최상위 메뉴)
-- =====================================================
INSERT INTO menus (tenant_id, parent_id, name, url, icon, sort_order, is_active, hidden, is_external, external_url, created_at, updated_at)
SELECT NULL, NULL, '전자결재', '/approval', 'file-signature', 20, 1, 0, 0, NULL, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM menus WHERE name = '전자결재' AND tenant_id IS NULL AND deleted_at IS NULL);

-- 전자결재 하위 메뉴
INSERT INTO menus (tenant_id, parent_id, name, url, icon, sort_order, is_active, hidden, is_external, external_url, created_at, updated_at)
SELECT NULL, (SELECT id FROM menus WHERE name = '전자결재' AND tenant_id IS NULL AND deleted_at IS NULL LIMIT 1),
       '기안함', '/approval/drafts', 'edit', 1, 1, 0, 0, NULL, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM menus WHERE name = '기안함' AND tenant_id IS NULL AND deleted_at IS NULL);

INSERT INTO menus (tenant_id, parent_id, name, url, icon, sort_order, is_active, hidden, is_external, external_url, created_at, updated_at)
SELECT NULL, (SELECT id FROM menus WHERE name = '전자결재' AND tenant_id IS NULL AND deleted_at IS NULL LIMIT 1),
       '결재함', '/approval/pending', 'check-square', 2, 1, 0, 0, NULL, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM menus WHERE name = '결재함' AND tenant_id IS NULL AND deleted_at IS NULL);

INSERT INTO menus (tenant_id, parent_id, name, url, icon, sort_order, is_active, hidden, is_external, external_url, created_at, updated_at)
SELECT NULL, (SELECT id FROM menus WHERE name = '전자결재' AND tenant_id IS NULL AND deleted_at IS NULL LIMIT 1),
       '참조함', '/approval/references', 'eye', 3, 1, 0, 0, NULL, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM menus WHERE name = '참조함' AND tenant_id IS NULL AND deleted_at IS NULL);

-- =====================================================
-- 3. 게시판 (최상위 메뉴)
-- =====================================================
INSERT INTO menus (tenant_id, parent_id, name, url, icon, sort_order, is_active, hidden, is_external, external_url, created_at, updated_at)
SELECT NULL, NULL, '게시판', '/boards', 'layout', 21, 1, 0, 0, NULL, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM menus WHERE name = '게시판' AND tenant_id IS NULL AND deleted_at IS NULL);

-- =====================================================
-- 4. 보고서 및 분석 (최상위 메뉴)
-- =====================================================
INSERT INTO menus (tenant_id, parent_id, name, url, icon, sort_order, is_active, hidden, is_external, external_url, created_at, updated_at)
SELECT NULL, NULL, '보고서 및 분석', '/reports', 'bar-chart-2', 22, 1, 0, 0, NULL, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM menus WHERE name = '보고서 및 분석' AND tenant_id IS NULL AND deleted_at IS NULL);

-- =====================================================
-- 5. 계정정보 (최상위 메뉴)
-- =====================================================
INSERT INTO menus (tenant_id, parent_id, name, url, icon, sort_order, is_active, hidden, is_external, external_url, created_at, updated_at)
SELECT NULL, NULL, '계정정보', '/account', 'user-circle', 23, 1, 0, 0, NULL, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM menus WHERE name = '계정정보' AND tenant_id IS NULL AND deleted_at IS NULL);

-- =====================================================
-- 6. 회사정보 (최상위 메뉴)
-- =====================================================
INSERT INTO menus (tenant_id, parent_id, name, url, icon, sort_order, is_active, hidden, is_external, external_url, created_at, updated_at)
SELECT NULL, NULL, '회사정보', '/company', 'building', 24, 1, 0, 0, NULL, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM menus WHERE name = '회사정보' AND tenant_id IS NULL AND deleted_at IS NULL);

-- =====================================================
-- 7. 구독관리 (최상위 메뉴)
-- =====================================================
INSERT INTO menus (tenant_id, parent_id, name, url, icon, sort_order, is_active, hidden, is_external, external_url, created_at, updated_at)
SELECT NULL, NULL, '구독관리', '/subscription', 'credit-card', 25, 1, 0, 0, NULL, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM menus WHERE name = '구독관리' AND tenant_id IS NULL AND deleted_at IS NULL);

-- =====================================================
-- 8. 결제내역 (최상위 메뉴)
-- =====================================================
INSERT INTO menus (tenant_id, parent_id, name, url, icon, sort_order, is_active, hidden, is_external, external_url, created_at, updated_at)
SELECT NULL, NULL, '결제내역', '/payments', 'receipt', 26, 1, 0, 0, NULL, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM menus WHERE name = '결제내역' AND tenant_id IS NULL AND deleted_at IS NULL);

-- =====================================================
-- 9. 고객센터 (최상위 메뉴)
-- =====================================================
INSERT INTO menus (tenant_id, parent_id, name, url, icon, sort_order, is_active, hidden, is_external, external_url, created_at, updated_at)
SELECT NULL, NULL, '고객센터', '/support', 'headphones', 27, 1, 0, 0, NULL, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM menus WHERE name = '고객센터' AND tenant_id IS NULL AND deleted_at IS NULL);

-- =====================================================
-- 10. 기준정보 관리 하위 메뉴 추가 (parent_id = 167)
-- 주의: parent_id는 실제 DB의 기준정보 관리 메뉴 ID로 변경 필요
-- =====================================================
INSERT INTO menus (tenant_id, parent_id, name, url, icon, sort_order, is_active, hidden, is_external, external_url, created_at, updated_at)
SELECT NULL, 167, '직급관리', '/master-data/positions', 'award', 12, 1, 0, 0, NULL, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM menus WHERE name = '직급관리' AND tenant_id IS NULL AND deleted_at IS NULL);

INSERT INTO menus (tenant_id, parent_id, name, url, icon, sort_order, is_active, hidden, is_external, external_url, created_at, updated_at)
SELECT NULL, 167, '직책관리', '/master-data/job-titles', 'briefcase', 13, 1, 0, 0, NULL, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM menus WHERE name = '직책관리' AND tenant_id IS NULL AND deleted_at IS NULL);

INSERT INTO menus (tenant_id, parent_id, name, url, icon, sort_order, is_active, hidden, is_external, external_url, created_at, updated_at)
SELECT NULL, 167, '근무관리', '/master-data/work-schedules', 'clock', 14, 1, 0, 0, NULL, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM menus WHERE name = '근무관리' AND tenant_id IS NULL AND deleted_at IS NULL);

INSERT INTO menus (tenant_id, parent_id, name, url, icon, sort_order, is_active, hidden, is_external, external_url, created_at, updated_at)
SELECT NULL, 167, '휴가설정', '/master-data/leave-settings', 'calendar-check', 15, 1, 0, 0, NULL, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM menus WHERE name = '휴가설정' AND tenant_id IS NULL AND deleted_at IS NULL);

INSERT INTO menus (tenant_id, parent_id, name, url, icon, sort_order, is_active, hidden, is_external, external_url, created_at, updated_at)
SELECT NULL, 167, '팝업관리', '/master-data/popups', 'message-square', 16, 1, 0, 0, NULL, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM menus WHERE name = '팝업관리' AND tenant_id IS NULL AND deleted_at IS NULL);

INSERT INTO menus (tenant_id, parent_id, name, url, icon, sort_order, is_active, hidden, is_external, external_url, created_at, updated_at)
SELECT NULL, 167, '게시판관리', '/master-data/board-settings', 'clipboard', 17, 1, 0, 0, NULL, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM menus WHERE name = '게시판관리' AND tenant_id IS NULL AND deleted_at IS NULL);

INSERT INTO menus (tenant_id, parent_id, name, url, icon, sort_order, is_active, hidden, is_external, external_url, created_at, updated_at)
SELECT NULL, 167, '일반설정', '/master-data/general-settings', 'settings', 18, 1, 0, 0, NULL, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM menus WHERE name = '일반설정' AND tenant_id IS NULL AND deleted_at IS NULL);

INSERT INTO menus (tenant_id, parent_id, name, url, icon, sort_order, is_active, hidden, is_external, external_url, created_at, updated_at)
SELECT NULL, 167, '알림설정', '/master-data/notification-settings', 'bell', 19, 1, 0, 0, NULL, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM menus WHERE name = '알림설정' AND tenant_id IS NULL AND deleted_at IS NULL);

-- =====================================================
-- 실행 전 확인 사항
-- =====================================================
-- 1. 기준정보 관리 메뉴의 실제 ID 확인 (위에서 167로 가정)
--    SELECT id, name FROM menus WHERE name = '기준정보 관리' AND tenant_id IS NULL;
--
-- 2. 기존 메뉴 확인
--    SELECT id, name, url, sort_order FROM menus WHERE tenant_id IS NULL ORDER BY sort_order;
--
-- 3. 실행 후 확인
--    SELECT id, parent_id, name, url, sort_order, is_active
--    FROM menus
--    WHERE tenant_id IS NULL
--    ORDER BY COALESCE(parent_id, 0), sort_order;