import Link from "next/link";
import Image from "next/image";
import { Heart, ShoppingBag } from "lucide-react";
import type { Locale } from "@/lib/i18n/config";
import type { Dictionary } from "@/lib/i18n";
import { categoryTitle } from "@/lib/catalog";
import type { CategoryWithMeta } from "@/lib/catalog";
import { LocaleSwitcher } from "./locale-switcher";
import { SearchDialog } from "./search-dialog";
import { AccountMenu } from "./account-menu";
import { MobileMenu } from "./mobile-menu";

export function SiteHeader({
  locale,
  dict,
  categories,
  user,
  cartCount,
}: {
  locale: Locale;
  dict: Dictionary;
  categories: CategoryWithMeta[];
  user: { name: string; email: string; role: string } | null;
  cartCount: number;
}) {
  const nav = [
    { key: "shop", href: `/${locale}/shop` },
    { key: "collections", href: `/${locale}/collections` },
    { key: "story", href: `/${locale}/story` },
    { key: "journal", href: `/${locale}/journal` },
    { key: "contact", href: `/${locale}/contact` },
  ] as const;

  const categoryLinks = categories.map((category) => ({
    name: categoryTitle(category, locale),
    slug: category.slug,
    image: category.image,
  }));

  return (
    <header className="sticky top-0 z-50 border-b border-border/80 bg-ivory/95 backdrop-blur-md">
      <div className="shell flex h-[68px] items-center justify-between gap-4 md:h-[78px]">
        <div className="flex items-center gap-1 lg:flex-1">
          <MobileMenu
            locale={locale}
            labels={{
              menu: dict.common.menu,
              home: dict.nav.home,
              shop: dict.nav.shop,
              collections: dict.nav.collections,
              story: dict.nav.story,
              journal: dict.nav.journal,
              contact: dict.nav.contact,
              account: dict.nav.account,
              orders: dict.account.orders,
              wishlist: dict.nav.wishlist,
              login: dict.nav.login,
              register: dict.nav.register,
              logout: dict.nav.logout,
              admin: dict.nav.admin,
              categories: dict.shop.allCategories,
            }}
            categories={categoryLinks}
            user={user}
          />
          <Link href={`/${locale}`} className="group flex flex-col items-start" aria-label="DAR ZELLIGE">
            <span className="font-serif text-[18px] leading-none tracking-[0.26em] text-charcoal sm:text-[22px] sm:tracking-[0.3em]">
              DAR ZELLIGE
            </span>
            <span className="mt-1 hidden text-[10px] uppercase tracking-[0.32em] text-charcoal/45 sm:block">
              {dict.common.madeInMorocco}
            </span>
          </Link>
        </div>

        <nav className="hidden items-center gap-8 lg:flex" aria-label={dict.common.menu}>
          {nav.map((item) =>
            item.key === "shop" ? (
              <div key={item.key} className="group relative">
                <Link
                  href={item.href}
                  className="inline-flex items-center gap-1 py-7 text-[12px] font-medium uppercase tracking-[0.18em] text-charcoal/75 transition-colors hover:text-charcoal"
                >
                  {dict.nav.shop}
                  <span className="mt-0.5 block h-px w-3 bg-current transition-all group-hover:w-4" />
                </Link>
                <div className="invisible absolute left-1/2 top-full z-50 w-[680px] -translate-x-1/2 border border-border bg-cream opacity-0 shadow-soft transition-all duration-300 group-focus-within:visible group-focus-within:opacity-100 group-hover:visible group-hover:opacity-100">
                  <div className="grid grid-cols-[1.2fr_0.8fr]">
                    <div className="grid grid-cols-2 gap-x-6 gap-y-1 p-7">
                      <p className="col-span-2 mb-3 text-[12px] uppercase tracking-luxe text-gold">
                        {dict.shop.allCategories}
                      </p>
                      {categoryLinks.map((category) => (
                        <Link
                          key={category.slug}
                          href={`/${locale}/shop?category=${category.slug}`}
                          className="py-2 text-[14px] text-charcoal/70 transition-colors hover:text-charcoal"
                        >
                          {category.name}
                        </Link>
                      ))}
                      <Link
                        href={`/${locale}/shop`}
                        className="col-span-2 mt-4 border-t border-border pt-4 text-[12px] uppercase tracking-[0.18em] text-charcoal"
                      >
                        {dict.common.viewAll} →
                      </Link>
                    </div>
                    <div className="relative min-h-[280px] overflow-hidden bg-sand/30">
                      <Image
                        src="/images/cat-rugs.jpg"
                        alt={categoryLinks[0]?.name ?? "DAR ZELLIGE"}
                        fill
                        sizes="300px"
                        className="object-cover transition-transform duration-700 hover:scale-105"
                      />
                      <div className="absolute inset-x-0 bottom-0 bg-gradient-to-t from-charcoal/60 to-transparent p-5">
                        <p className="text-[12px] uppercase tracking-luxe text-ivory/80">{dict.home.categoriesEyebrow}</p>
                        <p className="mt-1 font-serif text-lg text-ivory">{dict.home.collectionTitle}</p>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            ) : (
              <Link
                key={item.key}
                href={item.href}
                className="py-7 text-[12px] font-medium uppercase tracking-[0.18em] text-charcoal/75 transition-colors hover:text-charcoal"
              >
                {dict.nav[item.key]}
              </Link>
            ),
          )}
        </nav>

        <div className="flex flex-1 items-center justify-end gap-1 sm:gap-2">
          <SearchDialog
            locale={locale}
            labels={{
              search: dict.nav.search,
              placeholder: dict.shop.searchPlaceholder,
              cta: dict.common.search,
              popular: dict.shop.allCategories,
            }}
            categories={categoryLinks}
          />
          <div className="hidden sm:block">
            <LocaleSwitcher locale={locale} label={dict.language.label} />
          </div>
          <AccountMenu
            locale={locale}
            user={user}
            labels={{
              account: dict.nav.account,
              login: dict.nav.login,
              orders: dict.account.orders,
              wishlist: dict.nav.wishlist,
              admin: dict.nav.admin,
              logout: dict.nav.logout,
              greeting: dict.account.greeting,
            }}
          />
          <Link
            href={`/${locale}/account/wishlist`}
            className="hidden p-2 text-charcoal/75 transition-colors hover:text-charcoal sm:block"
            aria-label={dict.nav.wishlist}
          >
            <Heart className="h-[18px] w-[18px]" strokeWidth={1.4} />
          </Link>
          <Link
            href={`/${locale}/cart`}
            className="relative p-2 text-charcoal/80 transition-colors hover:text-charcoal"
            aria-label={dict.nav.cart}
          >
            <ShoppingBag className="h-[19px] w-[19px]" strokeWidth={1.4} />
            {cartCount > 0 && (
              <span className="absolute -right-0.5 -top-0.5 flex h-[15px] min-w-[15px] items-center justify-center bg-charcoal px-1 text-[11px] font-semibold text-ivory">
                {cartCount > 99 ? "99" : cartCount}
              </span>
            )}
          </Link>
        </div>
      </div>
    </header>
  );
}
