"use client";

import Link from "next/link";
import { usePathname } from "next/navigation";
import {
  FiCreditCard,
  FiFileText,
  FiGrid,
  FiSettings,
  FiUsers,
  FiLogOut,
  FiInbox,
} from "react-icons/fi";
import CustomLink from "../ui/CustomLink";

interface AdminSidebarProps {
  isOpen?: boolean;
  onClose?: () => void;
  onLogout?: () => void;
}

const menuItems = [
  {
    title: "Dashboard",
    href: "/admin",
    icon: FiGrid,
  },
  {
    title: "Users",
    href: "/admin/users",
    icon: FiUsers,
  },
  {
    title: "Packages",
    href: "/admin/packages",
    icon: FiCreditCard,
  },
  {
    title: "Transactions",
    href: "/admin/transactions",
    icon: FiFileText,
  },
  {
    title: "Contacts",
    href: "/admin/contacts",
    icon: FiInbox,
  },
  {
    title: "System Settings",
    href: "/admin/settings",
    icon: FiSettings,
  },
];

export default function AdminSidebar({
  isOpen = false,
  onClose,
  onLogout,
}: AdminSidebarProps) {
  const pathname = usePathname();

  return (
    <>
      {/* Mobile Overlay */}
      

      {/* Sidebar */}
      <aside
        className={`fixed left-0 z-40 top-16 h-[calc(100vh-64px)] lg:w-56 2xl:w-70 bg-white/95 lg:admin-sidebar-bg lg:adminsidebar-shadow backdrop-blur-xl transition-transform duration-300 lg:top-20 lg:h-[calc(100vh-80px)] lg:translate-x-0 ${
          isOpen ? "translate-x-0" : "-translate-x-full lg:translate-x-0"
        }`}
      >
        <div className="flex h-full flex-col">
          {/* Scrollable Menu */}
          <div className="flex-1 overflow-y-auto px-2 2xl:px-4 py-6 custom-scrollbar">
            <nav className="space-y-1">
              {menuItems.map((item) => {
                const Icon = item.icon;

                const isActive =
                  pathname === item.href ||
                  (item.href !== "/admin" &&
                    pathname.startsWith(`${item.href}/`));

                return (
                  <CustomLink
                    key={item.href}
                    href={item.href}
                    onClick={onClose}
                    className={`flex items-center gap-2 2xl:gap-3 rounded-full px-4 py-2.5 2xl:py-3 text-sm font-medium transition-all ${
                      isActive
                        ? "gradient1 text-white border border-white"
                        : "text-text hover:bg-white hover:text-link-1 border border-transparent"
                    }`}
                  >
                    <Icon size={18} />
                    <span>{item.title}</span>
                  </CustomLink>
                );
              })}
            </nav>
          </div>

          {/* Bottom Logout */}
          <div className="border-t border-gray-100 bg-gray-50 p-4">
            <button
              onClick={onLogout}
              className="
                flex w-full items-center justify-center gap-3
                rounded-xl border border-danger/20 cursor-pointer
                bg-danger/10 px-4 py-3
                text-sm font-semibold text-danger
                transition-all duration-300
                hover:bg-danger hover:text-white
              "
            >
              <FiLogOut size={18} />
              <span>Sign Out</span>
            </button>
          </div>
        </div>
      </aside>
      <div
        onClick={onClose}
        className={`fixed inset-0 z-30 bg-black/40 top-16 h-[calc(100vh-64px)] transition-all duration-300 lg:hidden ${
          isOpen ? "visible opacity-100" : "invisible opacity-0"
        }`}
      />
    </>
  );
}