import { NavLink } from 'react-router-dom'
import {
  LayoutDashboard, Store, FileText, RefreshCw,
  Bell, Cpu, ClipboardList, Zap, Users, Package, Building2, Brain,
  Link2, ListOrdered, NotebookPen, CalendarDays, Mail, BarChart3,
  ScrollText, Settings, Smartphone, Sparkles,
  ShieldCheck, KeyRound, ListTree, Network, FolderTree, Briefcase
} from 'lucide-react'

const nav = [
  { to: '/dashboard',   icon: LayoutDashboard, label: '대시보드' },
  { to: '/stores',      icon: Store,            label: '매장 관리' },
  { to: '/templates',   icon: FileText,         label: 'ESL 템플릿' },
  { to: '/pos-cvt',     icon: RefreshCw,        label: 'POS 가격변환' },
  { to: '/tag-binding', icon: Link2,            label: '태그 바인딩' },
  { to: '/update-queue',icon: ListOrdered,      label: '업데이트 큐' },
  { to: '/alarms',      icon: Bell,             label: '알람 관리' },
  { to: '/hcore',       icon: Cpu,              label: 'HCore 장치' },
  { to: '/works',       icon: ClipboardList,    label: '작업 이력' },
  { to: '/firmware',    icon: Zap,              label: '펌웨어' },
  { to: '/products',    icon: Package,          label: '상품/가격' },
  { to: '/users',       icon: Users,            label: '사용자' },
  { to: '/tenants',     icon: Building2,        label: '테넌트 관리' },
  { to: '/audit',       icon: ScrollText,       label: '감사 로그' },
  { to: '/settings',    icon: Settings,         label: '시스템 설정' },
  { to: '/wise',        icon: Sparkles,         label: 'WISE AI' },
  { to: '/ai',          icon: Brain,            label: 'AI 분석' },
  { to: '/ai-techniques', icon: Sparkles,       label: 'AI 기법 설정' },
  { to: '/ai-platform',   icon: Cpu,             label: 'AI 플랫폼 설정' },
  { to: '/worklogs',    icon: NotebookPen,      label: '업무일지' },
  { to: '/schedules',   icon: CalendarDays,     label: '일정 관리' },
  { to: '/messages',    icon: Mail,             label: '쪽지' },
  { to: '/work-stats',  icon: BarChart3,        label: '업무 통계' },
  { to: '/mobile-app',  icon: Smartphone,       label: '모바일 앱 설치' },
]

// 시스템관리(권한) — UIWS system 이식. 관리자(ADMIN) 전용 노출. 백엔드 RBAC 403 이 1차 방어.
const systemNav = [
  { to: '/system/roles',      icon: ShieldCheck, label: '권한 관리' },
  { to: '/system/role-menus', icon: KeyRound,    label: '역할-메뉴 권한' },
  { to: '/system/menus',      icon: ListTree,    label: '메뉴 관리' },
  { to: '/system/codes',      icon: Network,     label: '공통코드 관리' },
  { to: '/system/depts',      icon: FolderTree,  label: '부서 관리' },
  { to: '/system/companies',  icon: Briefcase,   label: '거래처 관리' },
]

export default function Sidebar() {
  const isAdmin = (localStorage.getItem('esn_role') || '') === 'ADMIN'
  return (
    <aside className="w-56 bg-panel flex flex-col border-r border-edge flex-shrink-0">
      <div className="h-14 flex items-center px-4 border-b border-edge">
        <span className="text-brand font-bold text-sm tracking-wide">ESN</span>
        <span className="ml-2 text-xs text-gray-400">ESL 통합 관리</span>
      </div>
      <nav className="flex-1 overflow-y-auto py-3">
        {nav.map(({ to, icon: Icon, label }) => (
          <NavLink
            key={to}
            to={to}
            className={({ isActive }) =>
              `flex items-center gap-3 px-4 py-2.5 text-sm transition-colors ${
                isActive
                  ? 'bg-brand/15 text-brand border-r-2 border-brand'
                  : 'text-gray-400 hover:text-white hover:bg-edge/50'
              }`
            }
          >
            <Icon size={16} />
            {label}
          </NavLink>
        ))}

        {isAdmin && (
          <>
            <div className="mt-3 px-4 pt-3 pb-1 border-t border-edge text-[10px] font-semibold tracking-wide text-gray-500 uppercase">
              시스템관리 (권한)
            </div>
            {systemNav.map(({ to, icon: Icon, label }) => (
              <NavLink
                key={to}
                to={to}
                className={({ isActive }) =>
                  `flex items-center gap-3 px-4 py-2.5 text-sm transition-colors ${
                    isActive
                      ? 'bg-brand/15 text-brand border-r-2 border-brand'
                      : 'text-gray-400 hover:text-white hover:bg-edge/50'
                  }`
                }
              >
                <Icon size={16} />
                {label}
              </NavLink>
            ))}
          </>
        )}
      </nav>
      <div className="p-4 border-t border-edge text-xs text-gray-500">
        zioinfo-esn v1.0
      </div>
    </aside>
  )
}
