"use client";

import Link from "next/link";
import { usePathname } from "next/navigation";
import { useState } from "react";
import {
  Menu,
  X,
  ShoppingBag,
  CalendarDays,
  UtensilsCrossed,
  Home,
} from "lucide-react";

import { useLanguage } from "@/lib/i18n";
import { useCart } from "@/lib/cart";
import { cn } from "@/lib/utils";

const links = [
  { href: "/", key: "nav.home" },
  { href: "/menu", key: "nav.menu" },
  { href: "/reservation", key: "nav.reserve" },
  { href: "/commander", key: "nav.order" },
  { href: "/evenements", key: "nav.events" },
];

export function SiteHeader() {
  const { t } = useLanguage();
  const { count } = useCart();
  const pathname = usePathname();

  const [open, setOpen] = useState(false);

  function closeMobileMenu() {
    setOpen(false);
  }

  function isActive(href: string) {
    if (href === "/") {
      return pathname === "/";
    }

    return pathname.startsWith(href);
  }

  return (
    <header className="fixed inset-x-0 top-0 z-50 border-b border-[#e8dfd2] bg-[#faf8f2]/95 text-[#2b241c] shadow-sm backdrop-blur-xl">
      {/* HEADER PRINCIPAL */}
      <div className="mx-auto flex h-20 max-w-7xl items-center justify-between gap-4 px-5 md:px-8">
        {/* LOGO */}
        <Link
          href="/"
          onClick={closeMobileMenu}
          aria-label="Happy Hours - Accueil"
          className="group flex shrink-0 flex-col leading-none"
        >
          <span className="font-serif text-2xl font-semibold tracking-wide text-[#b8863b] transition-colors duration-300 group-hover:text-[#9f7332]">
            Happy Hours
          </span>

          <span className="mt-1 text-[9px] uppercase tracking-[0.35em] text-[#7a7064]">
            Un instant de privilège
          </span>
        </Link>

        {/* NAVIGATION DESKTOP */}
        <nav
          aria-label="Navigation principale"
          className="hidden items-center gap-6 lg:flex"
        >
          {links.map((link) => {
            const active = isActive(link.href);

            return (
              <Link
                key={link.href}
                href={link.href}
                className={cn(
                  "group relative py-2 text-sm font-medium transition-colors duration-300",
                  active
                    ? "text-[#b8863b]"
                    : "text-[#5f574d] hover:text-[#b8863b]"
                )}
              >
                {t(link.key)}

                <span
                  className={cn(
                    "absolute bottom-0 left-0 h-px bg-[#b8863b] transition-all duration-300",
                    active ? "w-full" : "w-0 group-hover:w-full"
                  )}
                />
              </Link>
            );
          })}
        </nav>

        {/* ACTIONS */}
        <div className="flex items-center gap-2 md:gap-3">
          {/* PANIER */}
          <Link
            href="/panier"
            aria-label={
              count > 0
                ? `Panier, ${count} article${count > 1 ? "s" : ""}`
                : "Panier"
            }
            className={cn(
              "relative inline-flex h-10 w-10 items-center justify-center rounded-full border transition-all duration-300",
              pathname.startsWith("/panier") ||
                pathname.startsWith("/checkout")
                ? "border-[#b8863b] bg-[#b8863b]/10 text-[#9f7332]"
                : "border-[#d8cfc2] text-[#5f574d] hover:border-[#b8863b] hover:text-[#b8863b]"
            )}
          >
            <ShoppingBag className="h-4 w-4" />

            {count > 0 && (
              <span className="absolute -right-1 -top-1 flex h-5 min-w-5 items-center justify-center rounded-full bg-[#b8863b] px-1 text-[10px] font-bold text-white shadow-md">
                {count > 99 ? "99+" : count}
              </span>
            )}
          </Link>

          {/* COMMANDER */}
          <Link
            href="/commander"
            className={cn(
              "hidden items-center gap-2 rounded-full px-5 py-2.5 text-sm font-semibold transition-all duration-300 sm:flex",
              pathname.startsWith("/commander")
                ? "bg-[#2b241c] text-white"
                : "bg-[#b8863b] text-white hover:bg-[#9f7332]"
            )}
          >
            <UtensilsCrossed className="h-4 w-4" />
            Commander
          </Link>

          {/* RÉSERVER */}
          <Link
            href="/reservation"
            className="hidden items-center gap-2 rounded-full border border-[#d8cfc2] px-5 py-2.5 text-sm font-semibold text-[#4f473e] transition-all duration-300 hover:border-[#b8863b] hover:text-[#b8863b] md:flex"
          >
            <CalendarDays className="h-4 w-4" />
            {t("nav.bookTable")}
          </Link>

          {/* MENU MOBILE */}
          <button
            type="button"
            aria-label={open ? "Fermer le menu" : "Ouvrir le menu"}
            aria-expanded={open}
            onClick={() => setOpen((value) => !value)}
            className="inline-flex h-10 w-10 items-center justify-center rounded-full border border-[#d8cfc2] text-[#4f473e] transition-all duration-300 hover:border-[#b8863b] hover:text-[#b8863b] lg:hidden"
          >
            {open ? (
              <X className="h-5 w-5" />
            ) : (
              <Menu className="h-5 w-5" />
            )}
          </button>
        </div>
      </div>

      {/* MENU MOBILE */}
      <div
        className={cn(
          "overflow-hidden border-t border-[#e8dfd2] bg-[#faf8f2] lg:hidden",
          "transition-all duration-300 ease-in-out",
          open ? "max-h-[700px] opacity-100" : "max-h-0 opacity-0"
        )}
      >
        <nav
          aria-label="Navigation mobile"
          className="mx-auto max-w-7xl px-5 py-4"
        >
          <Link
            href="/"
            onClick={closeMobileMenu}
            className="flex items-center gap-3 border-b border-[#e8dfd2] py-4 text-sm font-medium text-[#4f473e] transition-colors hover:text-[#b8863b]"
          >
            <Home className="h-4 w-4 text-[#b8863b]" />
            Accueil
          </Link>

          <Link
            href="/menu"
            onClick={closeMobileMenu}
            className="flex items-center gap-3 border-b border-[#e8dfd2] py-4 text-sm font-medium text-[#4f473e] transition-colors hover:text-[#b8863b]"
          >
            <UtensilsCrossed className="h-4 w-4 text-[#b8863b]" />
            Découvrir le menu
          </Link>

          <Link
            href="/commander"
            onClick={closeMobileMenu}
            className="mt-4 flex items-center justify-center gap-2 rounded-full bg-[#b8863b] px-5 py-3.5 text-sm font-semibold text-white transition-all hover:bg-[#9f7332]"
          >
            <UtensilsCrossed className="h-4 w-4" />
            Commander
          </Link>

          <Link
            href="/panier"
            onClick={closeMobileMenu}
            className="mt-3 flex items-center justify-center gap-2 rounded-full border border-[#d8cfc2] px-5 py-3.5 text-sm font-semibold text-[#4f473e] transition-all hover:border-[#b8863b] hover:text-[#b8863b]"
          >
            <ShoppingBag className="h-4 w-4" />

            Voir mon panier

            {count > 0 && (
              <span className="flex h-5 min-w-5 items-center justify-center rounded-full bg-[#b8863b] px-1 text-[10px] font-bold text-white">
                {count > 99 ? "99+" : count}
              </span>
            )}
          </Link>

          <Link
            href="/reservation"
            onClick={closeMobileMenu}
            className="mt-3 flex items-center justify-center gap-2 rounded-full border border-[#d8cfc2] px-5 py-3.5 text-sm font-semibold text-[#4f473e] transition-all hover:border-[#b8863b] hover:text-[#b8863b]"
          >
            <CalendarDays className="h-4 w-4" />
            Réserver une table
          </Link>

          <div className="mt-4 border-t border-[#e8dfd2] pt-2">
            {links
              .filter(
                (link) =>
                  link.href !== "/" &&
                  link.href !== "/menu" &&
                  link.href !== "/commander" &&
                  link.href !== "/reservation"
              )
              .map((link) => (
                <Link
                  key={link.href}
                  href={link.href}
                  onClick={closeMobileMenu}
                  className="block border-b border-[#e8dfd2] py-4 text-sm font-medium text-[#5f574d] transition-colors hover:text-[#b8863b] last:border-0"
                >
                  {t(link.key)}
                </Link>
              ))}
          </div>
        </nav>
      </div>
    </header>
  );
}