"use client";

import Link from "next/link";
import { usePathname } from "next/navigation";
import { motion } from "framer-motion";
import {
  Video,
  Image,
  Music,
  Sparkles,
  LayoutDashboard,
  CreditCard,
  Settings,
} from "lucide-react";
import { cn } from "@/lib/utils";

const sidebarNavItems = [
  {
    title: "Dashboard",
    items: [
      { name: "Overview", href: "/dashboard", icon: LayoutDashboard },
    ],
  },
  {
    title: "Tools",
    items: [
      { name: "Video to Frames", href: "/tools/video-frames", icon: Video },
      { name: "Image Compression", href: "/tools/image-compress", icon: Image },
      { name: "Audio Compression", href: "/tools/audio-compress", icon: Music },
    ],
  },
  {
    title: "AI Tools",
    items: [
      { name: "AI Image", href: "/tools/ai-image", icon: Sparkles },
      { name: "AI Audio", href: "/tools/ai-audio", icon: Sparkles },
    ],
  },
  {
    title: "Account",
    items: [
      { name: "Pricing", href: "/pricing", icon: CreditCard },
      { name: "Settings", href: "/settings", icon: Settings },
    ],
  },
];

interface SidebarProps {
  className?: string;
}

export function Sidebar({ className }: SidebarProps) {
  const pathname = usePathname();

  return (
    <aside
      className={cn(
        "fixed left-0 top-16 z-40 h-[calc(100vh-4rem)] w-64 border-r border-border/40 bg-background/95 backdrop-blur",
        className
      )}
    >
      <div className="h-full overflow-y-auto py-6 pr-4">
        <nav className="space-y-8 px-4">
          {sidebarNavItems.map((section) => (
            <div key={section.title}>
              <h3 className="mb-4 px-2 text-xs font-semibold uppercase tracking-wider text-muted-foreground">
                {section.title}
              </h3>
              <ul className="space-y-1">
                {section.items.map((item) => {
                  const Icon = item.icon;
                  const isActive = pathname === item.href || pathname?.startsWith(item.href + "/");

                  return (
                    <li key={item.name}>
                      <Link
                        href={item.href}
                        className={cn(
                          "group flex items-center rounded-lg px-3 py-2 text-sm font-medium transition-all",
                          isActive
                            ? "bg-primary/10 text-primary"
                            : "text-muted-foreground hover:bg-accent hover:text-accent-foreground"
                        )}
                      >
                        <Icon className={cn("mr-3 h-4 w-4", isActive ? "text-primary" : "")} />
                        <span className="flex-1">{item.name}</span>
                        {isActive && (
                          <motion.div
                            layoutId="activeIndicator"
                            className="h-4 w-1 rounded-full bg-primary"
                            transition={{ type: "spring", stiffness: 300, damping: 30 }}
                          />
                        )}
                      </Link>
                    </li>
                  );
                })}
              </ul>
            </div>
          ))}
        </nav>
      </div>
    </aside>
  );
}
