"use client";

import Link from "next/link";
import { usePathname } from "next/navigation";
import { Heart, LayoutDashboard, LockKeyhole, LogOut, MapPin, Package, User } from "lucide-react";
import { logoutAction } from "@/app/actions/auth";
import { cn } from "@/lib/utils";

const icons = {
  overview: LayoutDashboard,
  orders: Package,
  addresses: MapPin,
  wishlist: Heart,
  profile: User,
  security: LockKeyhole,
};

export function AccountNav({
  locale,
  labels,
  logoutLabel,
}: {
  locale: string;
  labels: Record<keyof typeof icons, string>;
  logoutLabel: string;
}) {
  const pathname = usePathname();

  const items: { key: keyof typeof icons; href: string }[] = [
    { key: "overview", href: `/${locale}/account` },
    { key: "orders", href: `/${locale}/account/orders` },
    { key: "addresses", href: `/${locale}/account/addresses` },
    { key: "wishlist", href: `/${locale}/account/wishlist` },
    { key: "profile", href: `/${locale}/account/profile` },
    { key: "security", href: `/${locale}/account/security` },
  ];

  return (
    <nav className="grid grid-cols-2 gap-1 border border-line bg-cream p-2 shadow-panel lg:flex lg:flex-col lg:gap-0.5 lg:p-4">
      {items.map((item) => {
        const Icon = icons[item.key];
        const active =
          item.key === "overview" ? pathname === item.href : pathname.startsWith(item.href);
        return (
          <Link
            key={item.key}
            href={item.href}
            className={cn(
              "flex w-full items-center gap-2.5 rounded-sm px-3 py-2.5 text-[13px] uppercase tracking-[0.1em] transition-colors lg:px-3 lg:py-2.5",
              active ? "bg-sand/20 font-medium text-charcoal" : "text-walnut hover:bg-parchment hover:text-charcoal",
            )}
          >
            <Icon className={cn("h-3.5 w-3.5", active ? "text-gold-ink" : "text-pebble")} strokeWidth={1.5} />
            {labels[item.key]}
          </Link>
        );
      })}
      <form action={logoutAction} className="col-span-2 lg:mt-4 lg:border-t lg:border-line lg:pt-3">
        <input type="hidden" name="locale" value={locale} />
        <button
          type="submit"
          className="flex w-full items-center gap-2.5 rounded-sm px-3 py-2.5 text-[13px] uppercase tracking-[0.1em] text-walnut transition-colors hover:bg-parchment hover:text-terracotta lg:px-3 lg:py-2.5"
        >
          <LogOut className="h-3.5 w-3.5" strokeWidth={1.5} />
          {logoutLabel}
        </button>
      </form>
    </nav>
  );
}
