import { state } from '../core/state';
import { renderPcList } from './List/PcListView';
import { renderServerList } from './List/ServerListView';
import { renderStorageList } from './List/StorageListView';
import { renderEquipmentList } from './List/EquipmentListView';
import { renderMobileList } from './List/MobileListView';
import { renderSwList } from './List/SwListView';
import { renderCloudList } from './List/CloudListView';
import { renderDomainList } from './List/DomainListView';
import { createIcons, Download, Upload, FileSpreadsheet, Plus, X, LayoutDashboard, Monitor, Server, Database, Laptop, CalendarClock, Key, Cpu, Layers, Users, Paperclip, Edit2, RefreshCcw } from 'lucide';

/**
 * 자산 목록 테이블 렌더링 통합 허브
 */
export function renderSWTable(mainContent: HTMLElement) {
  if (!mainContent) return;
  console.log(`📂 Rendering Table for: ${state.activeCategory} / ${state.activeSubTab}`);
  
  mainContent.innerHTML = ''; 
  const container = document.createElement('div');
  container.className = 'view-container'; 

  try {
    const tab = state.activeSubTab;

    if (state.activeCategory === 'hw') {
      if (tab === '개인PC') renderPcList(container);
      else if (tab === '서버') renderServerList(container);
      else if (tab === '스토리지') renderStorageList(container);
      else if (tab === '전산비품') renderEquipmentList(container);
      else if (tab === '모바일기기') renderMobileList(container);
      else {
        container.innerHTML = `<div style="padding:2rem; color:var(--text-muted);">"${tab}" 탭에 대한 하드웨어 리스트 뷰가 정의되지 않았습니다.</div>`;
      }
    } else if (state.activeCategory === 'sw') {
      if (tab === '구독SW' || tab === '영구SW') {
        renderSwList(container);
      } else if (tab === '클라우드') {
        renderCloudList(container);
      } else {
        container.innerHTML = `<div style="padding:2rem; color:var(--text-muted);">"${tab}" 탭에 대한 소프트웨어 리스트 뷰가 정의되지 않았습니다.</div>`;
      }
    } else if (state.activeCategory === 'ops') {
      if (tab === '도메인') renderDomainList(container);
      else {
        container.innerHTML = `<div style="padding:2rem; color:var(--text-muted); text-align:center; margin-top:3rem;">운영 서비스(${tab}) 관리 기능은 현재 준비 중입니다.</div>`;
      }
    }

    mainContent.appendChild(container);
    
    // 전역 아이콘 초기화 (한 번 더 실행하여 누락 방지)
    createIcons({
      icons: { Download, Upload, FileSpreadsheet, Plus, X, LayoutDashboard, Monitor, Server, Database, Laptop, CalendarClock, Key, Cpu, Layers, Users, Paperclip, Edit2, RefreshCcw }
    });
  } catch (err: any) {
    console.error('❌ Error rendering table view:', err);
    mainContent.innerHTML = `<div style="padding:2rem; color:var(--danger);">목록을 불러오는 중 오류가 발생했습니다: ${err?.message || err}</div>`;
  }
}
