"use client";

import { useEffect, useState } from "react";
import { Search, X } from "lucide-react";

import { SearchBox } from "@/components/layout/search-box";

/**
 * Full-screen search overlay.
 *
 * Extracted from the floating nav so the homepage header can offer the same
 * search affordance without duplicating the open/close state machine, the
 * Escape handling or the `/` shortcut.
 */
export function useSearchSheet() {
  const [open, setOpen] = useState(false);

  useEffect(() => {
    const onKey = (event: KeyboardEvent) => {
      const target = event.target as HTMLElement | null;
      const typing = target && /^(INPUT|TEXTAREA|SELECT)$/.test(target.tagName);
      if (event.key === "/" && !typing) {
        event.preventDefault();
        setOpen(true);
      }
      if (event.key === "Escape") setOpen(false);
    };
    window.addEventListener("keydown", onKey);
    return () => window.removeEventListener("keydown", onKey);
  }, []);

  return { open, openSheet: () => setOpen(true), closeSheet: () => setOpen(false) };
}

export function SearchSheet({ onClose }: { onClose: () => void }) {
  return (
    <div className="fixed inset-0 z-[600] bg-bg/96 p-4 backdrop-blur-xl">
      <div className="mx-auto w-full max-w-2xl">
        <div className="mb-6 flex items-center justify-between">
          <span className="text-[15px] font-bold text-ink">Search FlixTV</span>
          <button
            type="button"
            onClick={onClose}
            aria-label="Close search"
            className="grid size-10 place-items-center rounded-full text-ink-soft transition-colors hover:bg-white/8 hover:text-ink"
          >
            <X className="size-5" />
          </button>
        </div>
        <SearchBox autoFocus size="lg" onNavigate={onClose} />
      </div>
    </div>
  );
}

/** The circular trigger used in both the header and the floating nav. */
export function SearchTrigger({
  onClick,
  className,
  variant = "icon",
  label = "Search",
}: {
  onClick: () => void;
  className?: string;
  variant?: "icon" | "pill";
  label?: string;
}) {
  if (variant === "pill") {
    return (
      <button
        type="button"
        onClick={onClick}
        className={
          className ??
          "inline-flex h-9 items-center gap-2 rounded-pill border border-white/15 bg-black/35 px-3.5 text-[13px] font-semibold text-white/85 backdrop-blur-md transition-colors duration-300 hover:bg-black/55 hover:text-white"
        }
      >
        <Search className="size-4 shrink-0" />
        Search
        <kbd className="ml-1 hidden rounded border border-white/20 px-1 text-[10px] font-bold text-white/60 sm:inline">
          /
        </kbd>
      </button>
    );
  }

  return (
    <button
      type="button"
      onClick={onClick}
      aria-label={label}
      className={
        className ??
        "grid size-10 shrink-0 place-items-center rounded-full text-ink-soft transition-all duration-300 hover:bg-white/10 hover:text-ink active:scale-90"
      }
    >
      <Search className="size-[18px]" />
    </button>
  );
}
