import { state } from '../core/state';

const MENU_CONFIG = {
  hw: {
    label: '하드웨어',
    tabs: ['대시보드', '개인PC', '서버', '스토리지', '전산비품', '모바일기기']
  },
  sw: {
    label: '소프트웨어',
    tabs: ['대시보드', '구독SW', '영구SW', '클라우드']
  },
  ops: {
    label: '운영 서비스',
    tabs: ['도메인', '메일', '메신저', '청구비용']
  }
};

export function renderNavigation(onTabChange: (tab: string) => void) {
  const navContainer = document.getElementById('main-nav')!;
  const btnAddAsset = document.getElementById('btn-add-asset') as HTMLButtonElement;

  const render = () => {
    navContainer.innerHTML = '';
    
    // 기존 메뉴 렌더링
    (Object.keys(MENU_CONFIG) as Array<keyof typeof MENU_CONFIG>).forEach(catKey => {
      const config = MENU_CONFIG[catKey];
      const isActive = state.activeCategory === catKey;

      const group = document.createElement('div');
      group.className = `nav-group ${isActive ? 'active is-showing-shelf' : ''}`;
      
      const trigger = document.createElement('div');
      trigger.className = 'gnb-trigger';
      trigger.textContent = config.label;
      
      trigger.addEventListener('click', () => {
        if (state.activeCategory !== catKey) {
          state.activeCategory = catKey;
          state.activeSubTab = '대시보드';
          if (btnAddAsset) btnAddAsset.classList.remove('hidden');
          render();
          onTabChange('대시보드');
        }
      });
      group.appendChild(trigger);

      const shelf = document.createElement('div');
      shelf.className = 'lnb-shelf';
      
      config.tabs.forEach(tab => {
        const item = document.createElement('div');
        item.className = `lnb-item ${isActive && state.activeSubTab === tab ? 'active' : ''}`;
        item.textContent = tab;
        
        item.addEventListener('click', (e) => {
          e.stopPropagation();
          state.activeCategory = catKey;
          state.activeSubTab = tab;
          if (btnAddAsset) btnAddAsset.classList.remove('hidden');
          render();
          onTabChange(tab);
        });
        shelf.appendChild(item);
      });
      group.appendChild(shelf);
      navContainer.appendChild(group);
    });

    // ─── '관리자' 메뉴 별도 추가 (GNB 스타일) ───
    const adminGroup = document.createElement('div');
    adminGroup.className = 'nav-group';
    
    const adminTrigger = document.createElement('div');
    adminTrigger.className = 'gnb-trigger';
    adminTrigger.innerHTML = '관리자';
    adminTrigger.style.color = 'var(--text-muted)';
    adminTrigger.style.borderLeft = '1px solid var(--border-color)';
    adminTrigger.style.marginLeft = '1rem';
    adminTrigger.style.paddingLeft = '1.5rem';
    
    adminTrigger.addEventListener('click', () => {
      alert('준비중입니다.');
    });
    
    adminGroup.appendChild(adminTrigger);
    navContainer.appendChild(adminGroup);
  };

  render();
}
