import Link from "next/link";
import Image from "next/image";
import type { Locale, Product, Solution } from "@/types/content";
import { getProductInitial, sortProductsByShowroomOrder } from "@/lib/product-order";
import { getImageAlt, getImageUrl } from "@/lib/images";
import { localizedPath } from "@/lib/routing";
import { LanguageSwitcher } from "@/components/layout/LanguageSwitcher";
import { HeaderBehavior } from "@/components/layout/HeaderBehavior";
import { NavMega } from "@/components/layout/NavMega";

const navLabels = {
  en: {
    solutions: "Solutions",
    products: "Product",
    successStories: "Success Stories",
    roomDesigner: "Room Designer",
    insights: "Insight",
    about: "About"
  },
  id: {
    solutions: "Solusi",
    products: "Produk",
    successStories: "Success Stories",
    roomDesigner: "Room Designer",
    insights: "Insight",
    about: "Tentang"
  }
} as const;

type NavbarProps = {
  locale: Locale;
  path: string;
  solutions: Solution[];
  products: Product[];
};

export function Navbar({ locale, path, solutions, products }: NavbarProps) {
  const labels = navLabels[locale];
  const solutionItems = solutions.map((item, index) => {
    const image = item.showcaseImage ?? item.heroImage;
    return {
      label: item.title[locale],
      href: localizedPath(`/solutions/${item.slug}`, locale),
      description: item.shortDescription[locale],
      category: item.eyebrow?.[locale] || "Solution",
      badge: String(index + 1).padStart(2, "0"),
      imageUrl: getImageUrl(image),
      imageAlt: getImageAlt(image, item.title[locale])
    };
  });
  const productItems = sortProductsByShowroomOrder(products).map((item, index) => {
    const image = item.heroImage;
    return {
      label: item.title[locale],
      href: localizedPath(`/products/${item.slug}`, locale),
      description: item.shortDescription[locale],
      category: item.category?.title?.[locale] || (locale === "id" ? "Produk" : "Product"),
      badge: getProductInitial(item.title[locale], index),
      imageUrl: getImageUrl(image),
      imageAlt: getImageAlt(image, item.title[locale])
    };
  });

  return (
    <HeaderBehavior isHome>
      <nav className="container nav" aria-label="Primary navigation">
        <Link href={localizedPath("/", locale)} className="brand" aria-label="Akarya home">
          <Image src="/logos/akarya/akarya-logo-full-white-blue.png" alt="Akarya" width={190} height={58} priority />
        </Link>
        <div className="nav-links">
          <div className="nav-menu nav-menu-wide">
            <Link className="nav-menu-trigger" href={localizedPath("/solutions", locale)}>
              {labels.solutions}
            </Link>
            <NavMega
              variant="solutions"
              ariaLabel={labels.solutions}
              rail={{
                eyebrow: labels.solutions,
                title: locale === "id" ? "Pilih setup ruang" : "Choose a room setup",
                linkLabel: locale === "id" ? "Semua solusi" : "All solutions",
                linkHref: localizedPath("/solutions", locale)
              }}
              items={solutionItems}
              fallback={{
                eyebrow: "Sales ready",
                title:
                  locale === "id"
                    ? "AV & IT system planning yang mudah dipahami."
                    : "Clear AV & IT system planning for every room.",
                description:
                  locale === "id"
                    ? "Mulai dari classroom, meeting room, LED display, signage, audio, sampai command center."
                    : "From classrooms, meeting rooms, LED displays, signage, audio, to command centers.",
                imageAlt: ""
              }}
            />
          </div>
          <div className="nav-menu nav-menu-wide">
            <Link className="nav-menu-trigger" href={localizedPath("/products", locale)}>
              {labels.products}
            </Link>
            <NavMega
              variant="products"
              ariaLabel={labels.products}
              rail={{
                eyebrow: labels.products,
                title: locale === "id" ? "Kategori produk" : "Product categories",
                linkLabel: locale === "id" ? "Semua produk" : "All products",
                linkHref: localizedPath("/products", locale)
              }}
              items={productItems}
              fallback={{
                eyebrow: "Product ecosystem",
                title:
                  locale === "id"
                    ? "Perangkat untuk display, audio, network, compute, dan software."
                    : "Devices for display, audio, network, compute, and software.",
                description:
                  locale === "id"
                    ? "Dipilih untuk kebutuhan project, kompatibilitas sistem, dan support jangka panjang."
                    : "Selected around project needs, system compatibility, and long-term support.",
                imageAlt: ""
              }}
            />
          </div>
          <Link href={localizedPath("/success-stories", locale)}>{labels.successStories}</Link>
          <Link href={localizedPath("/room-designer", locale)}>{labels.roomDesigner}</Link>
          <Link href={localizedPath("/insights", locale)}>{labels.insights}</Link>
          <Link href={localizedPath("/about", locale)}>{labels.about}</Link>
        </div>
        <div className="nav-actions">
          <LanguageSwitcher locale={locale} path={path} />
          <Link className="btn btn-secondary" href={localizedPath("/talk-to-akarya", locale)}>
            Talk to Akarya
          </Link>
          <details className="mobile-nav">
            <summary aria-label="Open navigation menu">
              <span aria-hidden="true"></span>
              <span aria-hidden="true"></span>
              <span aria-hidden="true"></span>
            </summary>
            <div className="mobile-nav-panel">
              <Link href={localizedPath("/solutions", locale)}>{labels.solutions}</Link>
              {solutionItems.map((item) => (
                <Link className="mobile-sub-link" key={item.href} href={item.href}>
                  {item.label}
                </Link>
              ))}
              <Link href={localizedPath("/products", locale)}>{labels.products}</Link>
              {productItems.map((item) => (
                <Link className="mobile-sub-link" key={item.href} href={item.href}>
                  {item.label}
                </Link>
              ))}
              <Link href={localizedPath("/success-stories", locale)}>{labels.successStories}</Link>
              <Link href={localizedPath("/room-designer", locale)}>{labels.roomDesigner}</Link>
              <Link href={localizedPath("/insights", locale)}>{labels.insights}</Link>
              <Link href={localizedPath("/about", locale)}>{labels.about}</Link>
              <Link className="mobile-primary-link" href={localizedPath("/talk-to-akarya", locale)}>
                Talk to Akarya
              </Link>
            </div>
          </details>
        </div>
      </nav>
    </HeaderBehavior>
  );
}
