'use client';

import { useEffect, useRef, useState } from 'react';
import Link from 'next/link';
import { Bell, MessageCircle } from 'lucide-react';
import { usePlatformSidebar } from '@/contexts/platform-sidebar-context';
import { ShellThemeToggle } from './shell-theme-toggle';
import { ShellUserMenu } from './shell-user-menu';
import type { AuthUser } from '@/lib/auth';
import { cn } from '@/lib/utils';

export function ShellHeader({
  homeHref,
  pageTitle,
  user,
  onLogout,
  roleSubtitle,
  notificationsHref,
  messagesHref,
}: {
  homeHref: string;
  pageTitle: string;
  user: AuthUser | null;
  onLogout: () => void;
  roleSubtitle?: string;
  notificationsHref?: string;
  messagesHref?: string;
}) {
  const [appMenuOpen, setAppMenuOpen] = useState(false);
  const { isMobileOpen, toggleSidebar, toggleMobileSidebar } = usePlatformSidebar();
  const inputRef = useRef<HTMLInputElement>(null);

  useEffect(() => {
    const onKey = (e: KeyboardEvent) => {
      if ((e.metaKey || e.ctrlKey) && e.key === 'k') {
        e.preventDefault();
        inputRef.current?.focus();
      }
    };
    document.addEventListener('keydown', onKey);
    return () => document.removeEventListener('keydown', onKey);
  }, []);

  const handleToggle = () => {
    if (typeof window !== 'undefined' && window.innerWidth >= 1024) toggleSidebar();
    else toggleMobileSidebar();
  };

  return (
    <header className="sticky top-0 z-[9999] w-full border-b border-slate-200/90 bg-white/95 backdrop-blur-md dark:border-zinc-800 dark:bg-zinc-950/90">
      <div className="flex w-full flex-col justify-between lg:flex-row lg:items-center lg:gap-6 lg:px-8 lg:py-3">
        <div className="flex w-full items-center justify-between gap-2 border-b border-slate-200/80 px-3 py-3 dark:border-zinc-800 lg:border-b-0 lg:px-0 lg:py-0">
          <button
            type="button"
            className="z-[9999] flex h-10 w-10 items-center justify-center rounded-xl border border-slate-200/90 text-slate-600 transition hover:bg-slate-50 dark:border-zinc-700 dark:text-zinc-300 dark:hover:bg-zinc-800 lg:flex"
            onClick={handleToggle}
            aria-label="Menüyü aç / kapat"
          >
            {isMobileOpen ? (
              <svg width="22" height="22" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path
                  fillRule="evenodd"
                  clipRule="evenodd"
                  d="M6.21967 7.28131C5.92678 6.98841 5.92678 6.51354 6.21967 6.22065C6.51256 5.92775 6.98744 5.92775 7.28033 6.22065L11.999 10.9393L16.7176 6.22078C17.0105 5.92789 17.4854 5.92788 17.7782 6.22078C18.0711 6.51367 18.0711 6.98855 17.7782 7.28144L13.0597 12L17.7782 16.7186C18.0711 17.0115 18.0711 17.4863 17.7782 17.7792C17.4854 18.0721 17.0105 18.0721 16.7176 17.7792L11.999 13.0607L7.28033 17.7794C6.98744 18.0722 6.51256 18.0722 6.21967 17.7794C5.92678 17.4865 5.92678 17.0116 6.21967 16.7187L10.9384 12L6.21967 7.28131Z"
                  fill="currentColor"
                />
              </svg>
            ) : (
              <svg width="18" height="14" viewBox="0 0 16 12" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path
                  fillRule="evenodd"
                  clipRule="evenodd"
                  d="M0.583252 1C0.583252 0.585788 0.919038 0.25 1.33325 0.25H14.6666C15.0808 0.25 15.4166 0.585786 15.4166 1C15.4166 1.41421 15.0808 1.75 14.6666 1.75L1.33325 1.75C0.919038 1.75 0.583252 1.41422 0.583252 1ZM0.583252 11C0.583252 10.5858 0.919038 10.25 1.33325 10.25L14.6666 10.25C15.0808 10.25 15.4166 10.5858 15.4166 11C15.4166 11.4142 15.0808 11.75 14.6666 11.75L1.33325 11.75C0.919038 11.75 0.583252 11.4142 0.583252 11ZM1.33325 5.25C0.919038 5.25 0.583252 5.58579 0.583252 6C0.583252 6.41421 0.919038 6.75 1.33325 6.75L7.99992 6.75C8.41413 6.75 8.74992 6.41421 8.74992 6C8.74992 5.58579 8.41413 5.25 7.99992 5.25L1.33325 5.25Z"
                  fill="currentColor"
                />
              </svg>
            )}
          </button>

          <Link href={homeHref} className="lg:hidden">
            <div className="flex items-center gap-2.5">
              <span className="flex h-9 w-9 items-center justify-center rounded-full bg-shell-sidebar text-xs font-bold text-white ring-1 ring-black/10 dark:bg-shell-sidebar-dark">
                V
              </span>
              <div className="leading-tight">
                <span className="block text-[14px] font-semibold tracking-tight text-slate-900 dark:text-white">Vidikid</span>
                {roleSubtitle ? (
                  <span className="block text-[11px] font-medium text-slate-500 dark:text-zinc-400">{roleSubtitle}</span>
                ) : null}
              </div>
            </div>
          </Link>

          <button
            type="button"
            onClick={() => setAppMenuOpen((v) => !v)}
            className="flex h-10 w-10 items-center justify-center rounded-xl text-slate-600 hover:bg-slate-100 dark:text-zinc-400 dark:hover:bg-zinc-800 lg:hidden"
            aria-label="Menü"
          >
            <svg width="22" height="22" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path
                fillRule="evenodd"
                clipRule="evenodd"
                d="M5.99902 10.4951C6.82745 10.4951 7.49902 11.1667 7.49902 11.9951V12.0051C7.49902 12.8335 6.82745 13.5051 5.99902 13.5051C5.1706 13.5051 4.49902 12.8335 4.49902 12.0051V11.9951C4.49902 11.1667 5.1706 10.4951 5.99902 10.4951ZM17.999 10.4951C18.8275 10.4951 19.499 11.1667 19.499 11.9951V12.0051C19.499 12.8335 18.8275 13.5051 17.999 13.5051C17.1706 13.5051 16.499 12.8335 16.499 12.0051V11.9951C16.499 11.1667 17.1706 10.4951 17.999 10.4951ZM13.499 11.9951C13.499 11.1667 12.8275 10.4951 11.999 10.4951C11.1706 10.4951 10.499 11.1667 10.499 11.9951V12.0051C10.499 12.8335 11.1706 13.5051 11.999 13.5051C12.8275 13.5051 13.499 12.8335 13.499 12.0051V11.9951Z"
                fill="currentColor"
              />
            </svg>
          </button>

          <div className="hidden min-w-0 flex-1 justify-center lg:flex">
            <div className="relative w-full max-w-md xl:max-w-lg">
              <span className="pointer-events-none absolute left-4 top-1/2 -translate-y-1/2 text-slate-400 dark:text-zinc-500">
                <svg className="size-[18px]" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
                  <path
                    fillRule="evenodd"
                    clipRule="evenodd"
                    d="M3.04175 9.37363C3.04175 5.87693 5.87711 3.04199 9.37508 3.04199C12.8731 3.04199 15.7084 5.87693 15.7084 9.37363C15.7084 12.8703 12.8731 15.7053 9.37508 15.7053C5.87711 15.7053 3.04175 12.8703 3.04175 9.37363ZM9.37508 1.54199C5.04902 1.54199 1.54175 5.04817 1.54175 9.37363C1.54175 13.6991 5.04902 17.2053 9.37508 17.2053C11.2674 17.2053 13.003 16.5344 14.357 15.4176L17.177 18.238C17.4699 18.5309 17.9448 18.5309 18.2377 18.238C18.5306 17.9451 18.5306 17.4703 18.2377 17.1774L15.418 14.3573C16.5365 13.0033 17.2084 11.2669 17.2084 9.37363C17.2084 5.04817 13.7011 1.54199 9.37508 1.54199Z"
                    fill="currentColor"
                  />
                </svg>
              </span>
              <input
                ref={inputRef}
                type="search"
                placeholder="Ara…"
                className="h-11 w-full rounded-full border border-slate-200/90 bg-slate-100/80 py-2 pl-12 pr-14 text-[13px] text-slate-900 shadow-inner shadow-slate-900/[0.03] placeholder:text-slate-400 transition focus:border-shell-sidebar/25 focus:bg-white focus:outline-none focus:ring-2 focus:ring-shell-sidebar/15 dark:border-zinc-700 dark:bg-zinc-800/60 dark:text-zinc-100 dark:placeholder:text-zinc-500 dark:focus:border-teal-700 dark:focus:ring-teal-900/30"
              />
              <span className="pointer-events-none absolute right-3 top-1/2 inline-flex -translate-y-1/2 items-center gap-0.5 rounded-md border border-slate-200/80 bg-white/90 px-1.5 py-0.5 text-[10px] font-semibold tabular-nums tracking-wide text-slate-400 dark:border-zinc-600 dark:bg-zinc-900 dark:text-zinc-500">
                <span>⌘</span>
                <span>K</span>
              </span>
            </div>
          </div>
        </div>

        <div
          className={cn(
            appMenuOpen ? 'flex' : 'hidden',
            'w-full flex-wrap items-center justify-end gap-2 border-t border-slate-200/70 px-4 py-3 dark:border-zinc-800 lg:flex lg:border-t-0 lg:px-0 lg:py-0',
          )}
        >
          <span className="mr-auto hidden max-w-[220px] truncate text-left text-[13px] font-semibold tracking-tight text-slate-800 dark:text-zinc-100 sm:max-w-xs lg:inline xl:max-w-md">
            {pageTitle}
          </span>

          <div className="flex items-center gap-1 sm:gap-1.5">
            {notificationsHref ? (
              <Link
                href={notificationsHref}
                className="relative flex h-10 w-10 items-center justify-center rounded-full border border-slate-200/90 bg-white text-slate-500 transition hover:border-slate-300 hover:bg-slate-50 hover:text-slate-800 dark:border-zinc-700 dark:bg-zinc-900 dark:text-zinc-400 dark:hover:bg-zinc-800 dark:hover:text-white"
                aria-label="Bildirimler"
              >
                <Bell className="size-[18px]" strokeWidth={1.75} />
              </Link>
            ) : null}
            {messagesHref ? (
              <Link
                href={messagesHref}
                className="relative flex h-10 w-10 items-center justify-center rounded-full border border-slate-200/90 bg-white text-slate-500 transition hover:border-slate-300 hover:bg-slate-50 hover:text-slate-800 dark:border-zinc-700 dark:bg-zinc-900 dark:text-zinc-400 dark:hover:bg-zinc-800 dark:hover:text-white"
                aria-label="Mesajlar"
              >
                <MessageCircle className="size-[18px]" strokeWidth={1.75} />
              </Link>
            ) : null}
            <ShellThemeToggle />
            <ShellUserMenu user={user} onLogout={onLogout} roleSubtitle={roleSubtitle} />
          </div>
        </div>
      </div>
    </header>
  );
}
