import { Outlet, NavLink, useNavigate } from 'react-router-dom'
import { Home, LogOut, Archive, Users } from 'lucide-react'
import { menuItems } from '../config/menu'

const iconMap: Record<string, React.ReactNode> = {
  menuHome: <Home className="w-5 h-5" />,
  menuBackupPlans: <Archive className="w-5 h-5" />,
  menuUsers: <Users className="w-5 h-5" />,
  menuLogout: <LogOut className="w-5 h-5" />,
}

export function Layout() {
  const navigate = useNavigate()

  return (
    <div className="flex h-screen bg-gray-100 dark:bg-gray-900">
      <aside className="w-56 bg-white dark:bg-gray-800 border-r border-gray-200 dark:border-gray-700 flex flex-col">
        <div className="p-4 border-b border-gray-200 dark:border-gray-700">
          <h1 className="text-xl font-bold text-gray-800 dark:text-white">YakPanel</h1>
        </div>
        <nav className="flex-1 overflow-y-auto p-2">
          {menuItems
            .filter((m) => m.id !== 'menuLogout')
            .map((item) => (
              <NavLink
                key={item.id}
                to={item.href}
                className={({ isActive }) =>
                  `flex items-center gap-3 px-3 py-2 rounded-lg mb-1 transition-colors ${
                    isActive
                      ? 'bg-blue-100 dark:bg-blue-900/30 text-blue-700 dark:text-blue-300'
                      : 'text-gray-600 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-700'
                  }`
                }
              >
                {iconMap[item.id] || <span className="w-5 h-5" />}
                <span>{item.title}</span>
              </NavLink>
            ))}
        </nav>
        <div className="p-2 border-t border-gray-200 dark:border-gray-700">
          <button
            onClick={() => navigate('/logout')}
            className="flex items-center gap-3 w-full px-3 py-2 rounded-lg text-gray-600 dark:text-gray-400 hover:bg-red-50 dark:hover:bg-red-900/20 hover:text-red-600"
          >
            <LogOut className="w-5 h-5" />
            <span>Log out</span>
          </button>
        </div>
      </aside>
      <main className="flex-1 overflow-auto p-6">
        <Outlet />
      </main>
    </div>
  )
}
