import { cn } from "@/lib/utils";
import {
  MenuIcon,
  SearchIcon,
  BellIcon,
  MessageSquareIcon,
  CrownIcon,
  ChevronDownIcon,
} from "@/app/components/icons";

type ActionFn = (action: string) => void;

const iconBtn =
  "relative grid h-10 w-10 place-items-center rounded-xl border border-white/10 bg-white/5 text-white/70 transition hover:border-brand/40 hover:text-white";

export function TopHeader({
  name,
  initials,
  unread,
  plan,
  onMenu,
  onAction,
}: {
  name: string;
  initials: string;
  unread: number;
  plan: string;
  onMenu: () => void;
  onAction: ActionFn;
}) {
  return (
    <header className="sticky top-0 z-30 flex h-[72px] items-center gap-3 border-b border-white/8 bg-bg/80 px-4 backdrop-blur-xl sm:px-6">
      <button
        type="button"
        onClick={onMenu}
        aria-label="Open menu"
        className="grid h-10 w-10 shrink-0 place-items-center rounded-lg text-white/70 transition hover:bg-white/5 lg:hidden"
      >
        <MenuIcon className="h-5 w-5" />
      </button>

      {/* Global search */}
      <div className="relative hidden max-w-md flex-1 sm:block">
        <SearchIcon className="pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-white/35" />
        <input
          type="search"
          placeholder="Search vehicles, requests..."
          className="w-full rounded-xl border border-white/10 bg-white/5 py-2.5 pl-9 pr-3 text-sm text-white outline-none transition-colors placeholder:text-white/35 focus:border-brand focus:ring-2 focus:ring-brand/30"
        />
      </div>

      <div className="flex flex-1 items-center justify-end gap-2 sm:flex-none">
        <button
          type="button"
          onClick={() => onAction("view-notifications")}
          aria-label="Notifications"
          className={iconBtn}
        >
          <BellIcon className="h-5 w-5" />
          {unread > 0 && (
            <span className="absolute right-2 top-2 h-2 w-2 rounded-full bg-danger ring-2 ring-bg" />
          )}
        </button>

        <button
          type="button"
          onClick={() => onAction("messages")}
          aria-label="Messages"
          className={cn(iconBtn, "hidden sm:grid")}
        >
          <MessageSquareIcon className="h-5 w-5" />
        </button>

        <span className="hidden items-center gap-1.5 rounded-full border border-brand/30 bg-brand/10 px-3 py-1.5 text-xs font-semibold text-brand md:inline-flex">
          <CrownIcon className="h-3.5 w-3.5" />
          {plan} Plan
        </span>

        <button
          type="button"
          onClick={() => onAction("profile")}
          className="flex items-center gap-2 rounded-full border border-white/10 bg-white/5 py-1 pl-1 pr-2.5 transition hover:border-brand/40"
        >
          <span
            className="grid h-8 w-8 place-items-center rounded-full text-xs font-bold text-white"
            style={{ background: "linear-gradient(135deg,#5c7cfa,#9b6bff)" }}
          >
            {initials}
          </span>
          <span className="hidden max-w-[8rem] truncate text-sm font-medium text-white lg:block">
            {name}
          </span>
          <ChevronDownIcon className="hidden h-4 w-4 text-white/40 lg:block" />
        </button>
      </div>
    </header>
  );
}
