"use client";

import type { ComponentType, SVGProps } from "react";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { cn } from "@/lib/utils";
import {
  VehCallLogo,
  LayoutGridIcon,
  CarIcon,
  InboxIcon,
  ActivityIcon,
  ShieldIcon,
  BellIcon,
  CreditCardIcon,
  SettingsIcon,
  CloseIcon,
} from "@/app/components/icons";

type IconType = ComponentType<SVGProps<SVGSVGElement>>;

// Each section is now its own top-level route (see app/(app)/<segment>).
export const NAV_ITEMS: { href: string; label: string; icon: IconType }[] = [
  { href: "/dashboard", label: "Dashboard", icon: LayoutGridIcon },
  { href: "/vehicles", label: "Vehicles", icon: CarIcon },
  { href: "/requests", label: "Requests", icon: InboxIcon },
  { href: "/activity", label: "Activity", icon: ActivityIcon },
  { href: "/emergency", label: "Emergency Contacts", icon: ShieldIcon },
  { href: "/notifications", label: "Notifications", icon: BellIcon },
  { href: "/billing", label: "Billing", icon: CreditCardIcon },
  { href: "/settings", label: "Settings", icon: SettingsIcon },
];

export function Sidebar({
  open,
  onClose,
  onNavigate,
  plan,
  vehiclesUsed,
  vehiclesTotal,
}: {
  open: boolean;
  onClose: () => void;
  onNavigate: () => void;
  plan: string;
  vehiclesUsed: number;
  vehiclesTotal: number;
}) {
  const pathname = usePathname();

  return (
    <>
      {open && (
        <button
          type="button"
          aria-label="Close menu"
          onClick={onClose}
          className="fixed inset-0 z-40 bg-black/50 backdrop-blur-sm lg:hidden"
        />
      )}

      <aside
        className={cn(
          "fixed inset-y-0 left-0 z-50 flex w-64 flex-col border-r border-white/8 bg-surface/95 backdrop-blur-xl transition-transform duration-300 lg:translate-x-0",
          open ? "translate-x-0" : "-translate-x-full",
        )}
      >
        <div className="flex h-[72px] items-center justify-between border-b border-white/8 px-5">
          <Link href="/dashboard" onClick={onNavigate} className="flex items-center gap-2.5">
            <VehCallLogo />
            <span className="text-lg font-bold tracking-tight text-white">
              Veh<span className="text-brand">Call</span>
            </span>
          </Link>
          <button
            type="button"
            onClick={onClose}
            aria-label="Close menu"
            className="grid h-9 w-9 place-items-center rounded-lg text-white/60 hover:bg-white/5 lg:hidden"
          >
            <CloseIcon className="h-5 w-5" />
          </button>
        </div>

        <nav className="flex-1 space-y-1 overflow-y-auto px-3 py-4">
          {NAV_ITEMS.map((it) => {
            const isActive = pathname === it.href;
            const Icon = it.icon;
            return (
              <Link
                key={it.href}
                href={it.href}
                onClick={onNavigate}
                aria-current={isActive ? "page" : undefined}
                className={cn(
                  "flex w-full items-center gap-3 rounded-xl px-3 py-2.5 text-sm font-medium transition",
                  isActive
                    ? "bg-brand/15 text-white"
                    : "text-white/55 hover:bg-white/5 hover:text-white",
                )}
              >
                <Icon className={cn("h-5 w-5 shrink-0", isActive ? "text-brand" : "text-white/45")} />
                <span className="truncate">{it.label}</span>
                {isActive && <span className="ml-auto h-1.5 w-1.5 rounded-full bg-brand" />}
              </Link>
            );
          })}
        </nav>

        <div className="border-t border-white/8 p-3">
          <div className="rounded-xl bg-gradient-to-br from-brand/20 to-electric/10 p-4">
            <p className="text-sm font-semibold text-white">{plan} Plan</p>
            <p className="mt-0.5 text-xs text-white/55">
              {vehiclesUsed} of {vehiclesTotal} vehicles used
            </p>
          </div>
        </div>
      </aside>
    </>
  );
}
