// components/dashboard/Sidebar.tsx
"use client";

import Link from "next/link";
import { usePathname } from "next/navigation";
import { 
  LayoutDashboard, 
  Briefcase, 
  Bookmark, 
  FileText, 
  User, 
  Settings,
  LogOut,
  Users,
  BarChart3,
  MessageSquare,
  PlusCircle,
  Sparkles,
  ChevronRight
} from "lucide-react";

interface NavItem {
  name: string;
  href: string;
  icon: React.ElementType;
  badge?: number;
}

interface SidebarProps {
  isOpen: boolean;
  onClose: () => void;
  userType: "job-seeker" | "client";
  userData?: {
    name: string;
    email: string;
    initials: string;
  };
}

export function Sidebar({ isOpen, onClose, userType, userData }: SidebarProps) {
  const pathname = usePathname();

  const jobSeekerNavItems: NavItem[] = [
    { name: "Dashboard", href: "/job-seeker", icon: LayoutDashboard },
    { name: "Find Jobs", href: "/job-seeker/job", icon: Briefcase },
    { name: "Saved Jobs", href: "/job-seeker/saved-jobs", icon: Bookmark },
    { name: "Applications", href: "/job-seeker/applications", icon: FileText, badge: 12 },
    { name: "Profile", href: "/job-seeker/profile", icon: User },
    { name: "Settings", href: "/job-seeker/setting", icon: Settings },
  ];

  const clientNavItems: NavItem[] = [
    { name: "Dashboard", href: "/client", icon: LayoutDashboard },
    { name: "Post Job", href: "/client/post-job", icon: PlusCircle },
    { name: "Manage Jobs", href: "/client/jobs", icon: Briefcase },
    { name: "Applications", href: "/client/applications", icon: FileText, badge: 8 },
    { name: "Candidates", href: "/client/candidates", icon: Users },
    { name: "Analytics", href: "/client/analytics", icon: BarChart3 },
    { name: "Messages", href: "/client/messages", icon: MessageSquare, badge: 3 },
    { name: "Profile", href: "/client/profile", icon: User },
    { name: "Settings", href: "/client/settings", icon: Settings },
  ];

  const navItems = userType === "job-seeker" ? jobSeekerNavItems : clientNavItems;
  
  const isActive = (href: string) => {
    if (href === "/job-seeker" && pathname === "/job-seeker") return true;
    if (href === "/client" && pathname === "/client") return true;
    return pathname === href;
  };

  const defaultUser = {
    name: userData?.name || (userType === "job-seeker" ? "Alex Morgan" : "Tech Solutions"),
    email: userData?.email || (userType === "job-seeker" ? "alex@example.com" : "hello@techsolutions.com"),
    initials: userData?.initials || (userType === "job-seeker" ? "AM" : "TS"),
    role: userType === "job-seeker" ? "Senior Developer" : "Company Admin"
  };

  return (
    <>
      {/* Mobile Overlay */}
      {isOpen && (
        <div className="fixed inset-0 bg-black/20 backdrop-blur-sm z-40 lg:hidden" onClick={onClose} />
      )}

      {/* Sidebar */}
      <aside
        className={`fixed top-0 left-0 z-50 h-full w-64 bg-white border-r border-slate-100 shadow-lg flex flex-col transform transition-transform duration-300 ease-out lg:translate-x-0 ${
          isOpen ? "translate-x-0" : "-translate-x-full"
        }`}
      >
        {/* Logo Section - Top */}
        <div className="flex items-center justify-between px-4 py-4 border-b border-slate-100">
          <Link href={`/${userType}`} className="flex items-center gap-2">
            <div className="w-7 h-7 rounded-lg bg-gradient-to-r from-sky-500 to-blue-600 flex items-center justify-center">
              <Sparkles className="w-3.5 h-3.5 text-white" />
            </div>
            <span className="text-lg font-bold text-slate-800">
              PKI<span className="text-sky-500">.</span>
            </span>
          </Link>
          <button
            onClick={onClose}
            className="lg:hidden p-1 text-slate-400 hover:text-slate-600 rounded-lg"
          >
            <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
            </svg>
          </button>
        </div>

        {/* Navigation - Middle (grows to fill space) */}
        <nav className="flex-1 px-3 py-4 space-y-1 overflow-y-auto">
          {navItems.map((item) => {
            const Icon = item.icon;
            const active = isActive(item.href);
            return (
              <Link
                key={item.name}
                href={item.href}
                onClick={onClose}
                className={`flex items-center justify-between px-3 py-2 rounded-full text-sm transition-all duration-200 group ${
                  active
                    ? "bg-sky-900 text-white shadow-sm"
                    : "text-slate-600 hover:bg-slate-100"
                }`}
              >
                <div className="flex items-center gap-2.5">
                  <Icon className={`w-4 h-4 ${active ? "text-white" : "text-slate-400 group-hover:text-slate-600"}`} />
                  <span className="text-[13px] font-medium">{item.name}</span>
                </div>
                {item.badge && (
                  <span className={`text-[10px] font-bold px-1.5 py-0.5 rounded-full ${
                    active 
                      ? "bg-white/20 text-white" 
                      : "bg-slate-100 text-slate-500"
                  }`}>
                    {item.badge}
                  </span>
                )}
              </Link>
            );
          })}
        </nav>

        {/* User Info & Logout - Bottom */}
        <div className="border-t border-slate-100 bg-white">
          {/* User Profile Card */}
          <div className="p-4">
            <div className="flex items-center gap-3 mb-3 p-2 rounded-xl bg-slate-50 hover:bg-slate-100 transition-colors cursor-pointer group">
              <div className="w-10 h-10 rounded-full bg-gradient-to-br from-sky-100 to-blue-100 flex items-center justify-center text-sky-700 font-bold text-sm">
                {defaultUser.initials}
              </div>
              <div className="flex-1 min-w-0">
                <p className="text-sm font-semibold text-slate-800 truncate">{defaultUser.name}</p>
                <p className="text-[10px] text-slate-500 truncate">{defaultUser.role}</p>
              </div>
              <ChevronRight className="w-4 h-4 text-slate-400 group-hover:text-sky-500 transition-colors" />
            </div>
            
            {/* Logout Button */}
            <button className="flex items-center gap-2.5 px-3 py-2 w-full rounded-full text-sm text-red-600 hover:bg-red-50 transition-all duration-200">
              <LogOut className="w-4 h-4" />
              <span className="text-[13px] font-medium">Sign Out</span>
            </button>
          </div>
          
          {/* Version */}
          <p className="text-[9px] text-slate-400 text-center pb-4">PKI Platform v2.0</p>
        </div>
      </aside>
    </>
  );
}