"use client";

import Link from "next/link";
import { usePathname, useSearchParams } from "next/navigation";

import { NAV_ITEMS, isNavItemActive, type NavItem } from "@/components/layout/nav-items";
import { cn } from "@/lib/utils";

/**
 * Floating bottom navigation — icons only.
 *
 * FlixTV's only persistent chrome. Identity, search and the account menu live in
 * the top header (`site-header.tsx`), so this bar is purely a row of
 * destinations and nothing is duplicated between the two.
 *
 * Shape: a shrink-to-fit pill centred above the bottom edge of the viewport at
 * every breakpoint, with a heavy glass surface (translucent base +
 * `backdrop-blur-2xl` + hairline border + deep drop shadow) so the artwork behind
 * it stays visible through the bar.
 *
 * Icons only. With no text the bar is roughly a third of the width it was, so
 * seven cells sit comfortably on a 360px phone. The full name is never rendered
 * as visible text — it is the `aria-label`, the `title` (which browsers show as a
 * native tooltip) and the CSS tooltip on hover/focus. The active destination is a
 * white pill with a dark icon, which is self-styling and therefore correct during
 * SSR rather than needing a measurement that would shift the bar on hydration.
 */
export function FloatingNav() {
  const pathname = usePathname();
  // Read for the active-state match only: "Trending" is a view of `/search`, so
  // it can only be the active item when that exact query is on screen.
  const search = useSearchParams().toString();

  return (
    <div className="pointer-events-none fixed inset-x-0 bottom-0 z-[500] flex justify-center px-3 pb-[max(0.75rem,env(safe-area-inset-bottom))] sm:px-4 sm:pb-[max(1rem,env(safe-area-inset-bottom))]">
      <nav
        aria-label="Primary"
        className={cn(
          "pointer-events-auto flex w-max max-w-[calc(100vw-1.5rem)] items-center gap-0.5 p-1.5 sm:gap-1",
          "rounded-full border border-white/12 bg-bg/78",
          "shadow-[0_18px_45px_-12px_rgba(0,0,0,0.85)] backdrop-blur-2xl backdrop-saturate-150",
        )}
      >
        {NAV_ITEMS.map((item) => (
          <NavLink
            key={item.href}
            item={item}
            active={isNavItemActive(item, pathname, search)}
          />
        ))}
      </nav>
    </div>
  );
}

/**
 * One destination: icon, and a tooltip that is invisible until hover or focus.
 */
function NavLink({ item, active }: { item: NavItem; active: boolean }) {
  const Icon = item.icon;

  return (
    <Link
      href={item.href}
      data-active={active}
      aria-current={active ? "page" : undefined}
      aria-label={item.label}
      title={item.label}
      className={cn(
        // `group` scopes the tooltip to this cell so it appears on hover and on
        // keyboard focus, and never persists.
        "group relative grid size-10 shrink-0 place-items-center rounded-full sm:size-11",
        "transition-colors duration-300 motion-reduce:transition-none",
        active
          ? "bg-white text-black shadow-[0_4px_14px_-4px_rgba(0,0,0,0.9)]"
          : "text-white/55 hover:bg-white/12 hover:text-white",
      )}
    >
      <Icon
        className={cn(
          "size-5 shrink-0 transition-transform duration-300 sm:size-[22px]",
          "motion-reduce:transition-none",
          !active && "group-hover:scale-110",
          active && "scale-105",
        )}
      />

      {/*
        Hover / focus tooltip. `aria-hidden` because the accessible name is
        already the link's `aria-label`; announcing it twice would be noise.
        `invisible` + `opacity-0` keeps it out of the tab order and out of the
        accessibility tree, and the group hover/focus variants reveal it.
      */}
      <span
        aria-hidden
        className={cn(
          "pointer-events-none absolute bottom-full left-1/2 z-10 mb-2 -translate-x-1/2 translate-y-1",
          "whitespace-nowrap rounded-md bg-black/85 px-2 py-1 text-[11px] font-semibold text-white/90",
          "opacity-0 shadow-lg backdrop-blur-md transition-all duration-200",
          "invisible group-hover:visible group-hover:translate-y-0 group-hover:opacity-100",
          "group-focus-visible:visible group-focus-visible:translate-y-0 group-focus-visible:opacity-100",
        )}
      >
        {item.label}
      </span>
    </Link>
  );
}
