"use client";

import Link from "next/link";
import { usePathname } from "next/navigation";
import { useSession } from "next-auth/react";
import { useState } from "react";
import {
  LayoutDashboard,
  Film,
  Smartphone,
  Tag,
  ShieldAlert,
  Megaphone,
  Users,
  Settings,
  ArrowLeft,
  Menu,
  X,
  Sparkles,
  Sliders
} from "lucide-react";

const navItems = [
  { href: "/admin", icon: LayoutDashboard, label: "Dashboard", badge: null },
  { href: "/admin/shorts", icon: Smartphone, label: "Vidéos Shorts", badge: "TikTok", badgeColor: "bg-rose-500/20 text-rose-300 border-rose-500/40" },
  { href: "/admin/media", icon: Film, label: "Catalogue Vidéos", badge: null },
  { href: "/admin/moderation", icon: ShieldAlert, label: "Modération & Reports", badge: "Live", badgeColor: "bg-red-500/20 text-red-300 border-red-500/40" },
  { href: "/admin/categories", icon: Tag, label: "Catégories", badge: null },
  { href: "/admin/ads", icon: Megaphone, label: "Publicités & Bannières", badge: null },
  { href: "/admin/users", icon: Users, label: "Utilisateurs & Bans", badge: null },
  { href: "/admin/builder", icon: Sliders, label: "Site Builder & Thèmes", badge: null },
  { href: "/admin/settings", icon: Settings, label: "Paramètres Globaux", badge: null },
];

export function AdminSidebar() {
  const pathname = usePathname();
  const { data: session } = useSession();
  const [mobileOpen, setMobileOpen] = useState(false);

  const role = (session?.user as any)?.role || "ADMIN";
  const name = session?.user?.name || session?.user?.email?.split("@")[0] || "Admin";

  return (
    <>
      {/* Mobile Top Header */}
      <div className="md:hidden flex items-center justify-between p-4 bg-[#070406] border-b border-[#2e1217] sticky top-0 z-50">
        <Link href="/" className="flex items-center font-black text-xl tracking-tighter">
          <span className="text-white">MT</span>
          <span className="text-[#e05a68] ml-0.5">BR</span>
          <span className="text-[10px] uppercase tracking-wider bg-red-600/30 text-red-400 border border-red-500/40 px-2 py-0.5 rounded-full ml-2 font-bold">
            Admin
          </span>
        </Link>
        <button
          onClick={() => setMobileOpen(!mobileOpen)}
          className="p-2 text-[#aaaaaa] hover:text-white bg-[#12080b] border border-[#2e1217] rounded-xl"
        >
          {mobileOpen ? <X size={20} /> : <Menu size={20} />}
        </button>
      </div>

      {mobileOpen && (
        <div
          onClick={() => setMobileOpen(false)}
          className="fixed inset-0 bg-black/80 backdrop-blur-md z-40 md:hidden"
        />
      )}

      {/* Sidebar Desktop & Drawer */}
      <aside
        className={`fixed md:sticky top-0 left-0 z-50 h-screen w-64 bg-[#070406] border-r border-[#2e1217] flex flex-col justify-between transition-transform duration-300 ${
          mobileOpen ? "translate-x-0" : "-translate-x-full md:translate-x-0"
        }`}
      >
        <div>
          {/* Brand Header */}
          <div className="p-5 border-b border-[#2e1217] flex items-center justify-between">
            <Link href="/admin" className="flex items-center font-black text-2xl tracking-tighter">
              <span className="text-white">MT</span>
              <span className="text-[#e05a68] ml-0.5">BR</span>
            </Link>
            <span className="text-[10px] font-black uppercase tracking-wider bg-gradient-to-r from-[#c93f52] to-[#e05a68] text-white px-2.5 py-0.5 rounded-full shadow-[0_0_12px_rgba(224,90,104,0.4)]">
              ADMIN v2.0
            </span>
          </div>

          {/* Current Admin User Badge */}
          <div className="p-4 border-b border-[#2e1217] bg-[#12080b]/60">
            <div className="flex items-center gap-3">
              <div className="w-10 h-10 rounded-2xl bg-gradient-to-br from-[#5c1922] to-[#e05a68] p-0.5 shadow-md flex items-center justify-center font-black text-white text-base">
                {session?.user?.image ? (
                  <img src={session.user.image} alt="Admin" className="w-full h-full object-cover rounded-[14px]" />
                ) : (
                  <span>{name[0]?.toUpperCase()}</span>
                )}
              </div>
              <div className="min-w-0 flex-1">
                <p className="text-xs font-black text-white truncate">{name}</p>
                <p className="text-[10px] text-rose-300 font-bold flex items-center gap-1">
                  <Sparkles size={10} /> {role}
                </p>
              </div>
            </div>
          </div>

          {/* Navigation Links */}
          <nav className="p-3 space-y-1.5 overflow-y-auto max-h-[calc(100vh-260px)] scrollbar-hide">
            {navItems.map(({ href, icon: Icon, label, badge, badgeColor }) => {
              const isActive = href === "/admin" ? pathname === "/admin" : pathname.startsWith(href);
              return (
                <Link
                  key={href}
                  href={href}
                  onClick={() => setMobileOpen(false)}
                  className={`flex items-center justify-between px-3.5 py-2.5 rounded-xl text-xs font-bold transition-all duration-200 ${
                    isActive
                      ? "bg-gradient-to-r from-[#5c1922] via-[#8c2432] to-[#e05a68] text-white shadow-[0_0_20px_rgba(224,90,104,0.35)] scale-[1.02]"
                      : "text-[#d8cbd0] hover:bg-[#12080b] hover:text-white"
                  }`}
                >
                  <div className="flex items-center gap-3">
                    <Icon size={17} className={isActive ? "text-white" : "#a38890"} />
                    <span>{label}</span>
                  </div>

                  {badge && (
                    <span className={`text-[9px] font-black uppercase px-2 py-0.5 rounded-full border ${badgeColor || "bg-white/10 text-white/80 border-white/20"}`}>
                      {badge}
                    </span>
                  )}
                </Link>
              );
            })}
          </nav>
        </div>

        {/* Bottom Actions: Add Media & Return */}
        <div className="p-3 border-t border-[#2e1217] space-y-2 bg-[#070406]">
          <Link
            href="/shorts/upload"
            onClick={() => setMobileOpen(false)}
            className="flex items-center justify-center gap-2 w-full py-2 rounded-xl text-xs font-black bg-gradient-to-r from-[#c93f52] to-[#e05a68] hover:from-[#b03042] hover:to-[#c93f52] text-white shadow-[0_0_15px_rgba(224,90,104,0.3)] transition-all"
          >
            <Smartphone size={14} /> + Créer un Short
          </Link>

          <Link
            href="/"
            className="flex items-center justify-center gap-2 px-3 py-2 rounded-xl text-xs font-bold text-[#a38890] hover:text-white hover:bg-[#12080b] transition-colors"
          >
            <ArrowLeft size={14} /> Retour au site
          </Link>
        </div>
      </aside>
    </>
  );
}
