"use client";

import Link from "next/link";
import { usePathname } from "next/navigation";
import { cn } from "@/lib/utils";
import {
    LayoutDashboard, Briefcase, Users, FileText,
    MessageSquare, Calendar, Settings, Bookmark, User,
    HelpCircle, Kanban, BarChart3
} from "lucide-react";
import { LogoutButton } from "@/components/auth/LogoutButton";
import { SidebarBody, SidebarLink, Sidebar as AceternitySidebar, useSidebar } from "@/components/ui/sidebar";
import { useState } from "react";
import { motion, AnimatePresence } from "framer-motion";

interface SidebarProps {
    role?: "candidate" | "employer" | "admin";
    user?: any;
}

export function Sidebar({
    role = "employer",
    user,
}: SidebarProps) {
    const pathname = usePathname();
    const [open, setOpen] = useState(false);

    const employerNav = [
        { label: "Dashboard", href: "/dashboard", icon: LayoutDashboard },
        { label: "My Jobs", href: "/dashboard/jobs", icon: Briefcase, badge: "3 Active" },
        { label: "Analytics", href: "/dashboard/employer/analytics", icon: BarChart3 },
        { label: "ATS", href: "/dashboard/employer/applicants", icon: Kanban },
        { label: "Candidates", href: "/dashboard/candidates", icon: Users },
        { label: "Messages", href: "/dashboard/messages", icon: MessageSquare, badge: "2" },
        { label: "Calendar", href: "/dashboard/calendar", icon: Calendar },
    ];

    const candidateNav = [
        { label: "Overview", href: "/dashboard", icon: LayoutDashboard },
        { label: "Job Matches", href: "/dashboard/matches", icon: Briefcase, badge: "12 New" },
        { label: "Applications", href: "/dashboard/applications", icon: FileText, badge: "5" },
        { label: "Saved Jobs", href: "/dashboard/saved", icon: Bookmark, badge: "8" },
        { label: "Messages", href: "/dashboard/messages", icon: MessageSquare, badge: "2" },
        { label: "Calendar", href: "/dashboard/calendar", icon: Calendar },
        { label: "My Profile", href: "/dashboard/profile/view", icon: User },
    ];

    const adminNav = [
        { label: "Overview", href: "/admin", icon: LayoutDashboard },
        { label: "Analytics", href: "/admin/analytics", icon: BarChart3 },
        { label: "Users", href: "/admin/users", icon: Users },
        { label: "Jobs", href: "/admin/jobs", icon: Briefcase },
        { label: "System", href: "/admin/system", icon: Settings },
    ];

    let navItems = employerNav;
    if (role === "candidate") navItems = candidateNav;
    if (role === "admin") navItems = adminNav;

    return (
        <AceternitySidebar open={open} setOpen={setOpen}>
            <SidebarBody className="justify-between gap-10">
                <div className="flex flex-col flex-1 overflow-y-auto overflow-x-hidden">
                    <Link href="/" className="flex items-center gap-2 mb-8 px-2 py-1 overflow-hidden">
                        <div className="flex h-8 w-8 shrink-0 items-center justify-center rounded-lg bg-primary text-white font-bold">
                            J
                        </div>
                        <motion.span
                            initial={{ opacity: 0 }}
                            animate={{ opacity: 1 }}
                            className="text-xl font-bold bg-gradient-to-r from-slate-900 to-slate-700 bg-clip-text text-transparent whitespace-nowrap"
                        >
                            Jobzio
                        </motion.span>
                    </Link>
                    <div className="mt-8 flex flex-col gap-2">
                        {navItems.map((item, idx) => {
                            const active = pathname === item.href || pathname.startsWith(item.href + '/');
                            return (
                                <SidebarLink
                                    key={idx}
                                    link={{
                                        label: (
                                            <div className="flex items-center justify-between min-w-[150px] w-full pr-2">
                                                <span>{item.label}</span>
                                                {item.badge && (
                                                    <span className={cn(
                                                        "px-2 py-0.5 text-[10px] font-bold uppercase tracking-wider rounded-full ml-2",
                                                        active ? "bg-primary text-white" : "bg-slate-200 text-slate-600 group-hover:bg-slate-300"
                                                    )}>
                                                        {item.badge}
                                                    </span>
                                                )}
                                            </div>
                                        ),
                                        href: item.href,
                                        icon: (
                                            <item.icon className={cn(
                                                "h-5 w-5 shrink-0 transition-colors duration-200",
                                                active ? "text-primary fill-primary/20" : "text-slate-400 group-hover:text-slate-600"
                                            )} />
                                        )
                                    }}
                                    className={cn("rounded-lg transition-all", active ? "bg-primary/10" : "hover:bg-slate-100")}
                                />
                            );
                        })}
                        <div className="h-px bg-slate-200 w-full my-4" />
                        <SidebarLink
                            link={{
                                label: "Settings",
                                href: "/dashboard/settings",
                                icon: <Settings className="h-5 w-5 shrink-0 text-slate-400 group-hover:text-slate-600" />
                            }}
                            className="hover:bg-slate-100 rounded-lg transition-all"
                        />
                    </div>
                </div>

                <div className="flex flex-col gap-4">
                    {/* Help Center Widget - Hide when collapsed */}
                    <AnimatePresence>
                        {open && (
                            <motion.div
                                initial={{ opacity: 0, y: 20 }}
                                animate={{ opacity: 1, y: 0 }}
                                exit={{ opacity: 0, height: 0, overflow: "hidden", margin: 0, padding: 0 }}
                                className="rounded-2xl bg-gradient-to-br from-slate-900 to-slate-800 p-5 text-white shadow-lg relative overflow-hidden"
                            >
                                <div className="absolute -right-4 -top-4 opacity-10">
                                    <HelpCircle className="h-24 w-24" />
                                </div>
                                <div className="relative z-10 w-48">
                                    <p className="text-xs font-bold uppercase tracking-wider text-blue-300 mb-1">Need Help?</p>
                                    <p className="text-sm leading-tight text-slate-300 mb-4 whitespace-normal">Check our docs or contact support.</p>
                                    <button className="w-full rounded-xl bg-white/10 hover:bg-white/20 transition-colors py-2 text-sm font-semibold backdrop-blur-sm">
                                        Help Center
                                    </button>
                                </div>
                            </motion.div>
                        )}
                    </AnimatePresence>

                    {/* User Profile Summary (Job Seeker specific) */}
                    <AnimatePresence>
                        {open && role === 'candidate' && (
                            <motion.div
                                initial={{ opacity: 0 }}
                                animate={{ opacity: 1 }}
                                exit={{ opacity: 0, height: 0, padding: 0, margin: 0 }}
                                className="p-4 border border-slate-200 rounded-xl bg-slate-50 shrink-0"
                            >
                                <div className="flex items-center justify-between mb-2 w-48">
                                    <span className="text-xs font-semibold text-slate-500 uppercase tracking-wider">Profile</span>
                                    <span className="text-xs font-bold text-primary">75%</span>
                                </div>
                                <div className="h-1.5 w-full bg-slate-200 rounded-full overflow-hidden">
                                    <div className="h-full bg-primary rounded-full w-3/4" />
                                </div>
                                <Link href="/dashboard/profile/edit" className="mt-2 block text-xs text-slate-500 hover:text-primary transition-colors">
                                    Complete profile to stand out →
                                </Link>
                            </motion.div>
                        )}
                    </AnimatePresence>

                    {/* Logout Button */}
                    <div className="px-2 mt-4">
                        <LogoutButton
                            variant="sidebar"
                            className="w-full justify-start rounded-xl text-slate-600 hover:bg-rose-50 hover:text-rose-600 font-medium transition-colors p-3"
                            iconOnly={!open}
                        />
                    </div>
                </div>
            </SidebarBody>
        </AceternitySidebar>
    );
}
