"use client";

import { useState } from "react";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { ChevronRight, Globe, Heart, Menu, Package, User } from "lucide-react";
import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetTrigger } from "@/components/ui/sheet";
import { logoutAction } from "@/app/actions/auth";
import { locales, localeMeta, type Locale } from "@/lib/i18n/config";

type Labels = {
  menu: string;
  home: string;
  shop: string;
  collections: string;
  story: string;
  journal: string;
  contact: string;
  account: string;
  orders: string;
  wishlist: string;
  login: string;
  register: string;
  logout: string;
  admin: string;
  categories: string;
};

export function MobileMenu({
  locale,
  labels,
  categories,
  user,
}: {
  locale: Locale;
  labels: Labels;
  categories: { name: string; slug: string }[];
  user: { name: string; role: string } | null;
}) {
  const [open, setOpen] = useState(false);
  const pathname = usePathname() || `/${locale}`;
  const close = () => setOpen(false);

  const localeHref = (code: Locale) => {
    const parts = pathname.split("/").filter(Boolean);
    if (parts.length && (locales as readonly string[]).includes(parts[0])) {
      parts[0] = code;
      return `/${parts.join("/")}`;
    }
    return `/${code}`;
  };

  const mainLinks = [
    { href: `/${locale}`, label: labels.home },
    { href: `/${locale}/shop`, label: labels.shop },
    { href: `/${locale}/collections`, label: labels.collections },
    { href: `/${locale}/story`, label: labels.story },
    { href: `/${locale}/journal`, label: labels.journal },
    { href: `/${locale}/contact`, label: labels.contact },
  ];

  return (
    <Sheet open={open} onOpenChange={setOpen}>
      <SheetTrigger className="p-2 text-charcoal/75 lg:hidden" aria-label={labels.menu}>
        <Menu className="h-5 w-5" strokeWidth={1.4} />
      </SheetTrigger>
      <SheetContent side="left" className="max-w-sm overflow-y-auto bg-ivory p-0">
        <SheetHeader className="border-b border-border px-6 py-5">
          <SheetTitle className="text-lg tracking-[0.28em]">DAR ZELLIGE</SheetTitle>
        </SheetHeader>

        <div className="flex flex-col px-6 py-5">
          <nav className="flex flex-col">
            {mainLinks.map((link) => (
              <Link
                key={link.href}
                href={link.href}
                onClick={close}
                className="flex items-center justify-between border-b border-border/70 py-3.5 text-[14px] uppercase tracking-[0.14em] text-charcoal/80"
              >
                {link.label}
                <ChevronRight className="h-3.5 w-3.5 text-charcoal/30 rtl:rotate-180" />
              </Link>
            ))}
          </nav>

          <p className="mt-7 text-[12px] uppercase tracking-luxe text-charcoal/40">{labels.categories}</p>
          <div className="mt-3 grid grid-cols-2 gap-x-4 gap-y-2">
            {categories.map((category) => (
              <Link
                key={category.slug}
                href={`/${locale}/shop?category=${category.slug}`}
                onClick={close}
                className="py-1.5 text-[14px] text-charcoal/70"
              >
                {category.name}
              </Link>
            ))}
          </div>

          <div className="mt-8 border-t border-border pt-5">
            {user ? (
              <div className="space-y-2">
                <Link href={`/${locale}/account`} onClick={close} className="flex items-center gap-3 py-2 text-[14px]">
                  <User className="h-4 w-4" /> {labels.account}
                </Link>
                <Link href={`/${locale}/account/orders`} onClick={close} className="flex items-center gap-3 py-2 text-[14px]">
                  <Package className="h-4 w-4" /> {labels.orders}
                </Link>
                <Link href={`/${locale}/account/wishlist`} onClick={close} className="flex items-center gap-3 py-2 text-[14px]">
                  <Heart className="h-4 w-4" /> {labels.wishlist}
                </Link>
                {(user.role === "ADMIN" || user.role === "SUPERADMIN") && (
                  <Link href="/admin" onClick={close} className="block py-2 text-[14px]">
                    {labels.admin}
                  </Link>
                )}
                <form action={logoutAction}>
                  <input type="hidden" name="locale" value={locale} />
                  <button type="submit" className="py-2 text-left text-[14px] text-charcoal/60">
                    {labels.logout}
                  </button>
                </form>
              </div>
            ) : (
              <div className="flex flex-col gap-3">
                <Link
                  href={`/${locale}/login`}
                  onClick={close}
                  className="border border-charcoal px-4 py-3 text-center text-[12px] uppercase tracking-luxe"
                >
                  {labels.login}
                </Link>
                <Link
                  href={`/${locale}/register`}
                  onClick={close}
                  className="bg-charcoal px-4 py-3 text-center text-[12px] uppercase tracking-luxe text-ivory"
                >
                  {labels.register}
                </Link>
              </div>
            )}
          </div>

          <div className="mt-8 flex items-center gap-4 border-t border-border pt-5 text-[12px] uppercase tracking-[0.14em] text-charcoal/50">
            <Globe className="h-3.5 w-3.5" />
            {locales.map((code) => (
              <Link
                key={code}
                href={localeHref(code)}
                onClick={close}
                className={code === locale ? "text-charcoal" : "hover:text-charcoal"}
              >
                {localeMeta[code].label}
              </Link>
            ))}
          </div>
        </div>
      </SheetContent>
    </Sheet>
  );
}
