import { useState } from "react";
import { useWeb } from "@/pages/web/WebContext";
import { cn } from "@/pages/web/WebUI";
import { BrandLogo } from "@/components/base/BrandLogo";
import { investorProfile } from "@/mocks/dshnInvestor";
import type { WebScreen } from "@/pages/web/types";

const NAV: { label: string; screen: WebScreen }[] = [
  { label: "الرئيسية", screen: "invHome" },
  { label: "المنشآت", screen: "businesses" },
  { label: "الفرص الاستثمارية", screen: "invOpps" },
  { label: "المتابعة", screen: "invFollowing" },
  { label: "المحادثات", screen: "invChats" },
];

const NOTIF_COUNT = 3;

export default function InvHeader() {
  const { screen, jump, lang, setLang, logout } = useWeb();
  const [menuOpen, setMenuOpen] = useState(false);
  const [searchOpen, setSearchOpen] = useState(false);
  const [query, setQuery] = useState("");

  const runSearch = () => {
    setSearchOpen(false);
    setQuery("");
    jump("invOpps");
  };

  return (
    <header className="sticky top-0 inset-x-0 z-50 bg-primary-950 border-b border-background-50/10">
      <div className="w-full max-w-[1280px] mx-auto px-4 md:px-8 h-[72px] flex items-center gap-4">
        {/* Logo */}
        <button
          type="button"
          onClick={() => jump("invHome")}
          className="flex items-center gap-2.5 cursor-pointer shrink-0"
        >
          <BrandLogo className="w-[46px] h-[33px] text-background-50" />
          <span className="font-heading font-bold text-[18px] text-background-50">دشن</span>
        </button>

        {/* Center nav */}
        <nav className="hidden lg:flex items-center gap-1 mx-auto">
          {NAV.map((n) => {
            const active = screen === n.screen;
            return (
              <button
                key={n.label}
                type="button"
                onClick={() => jump(n.screen)}
                className={cn(
                  "px-4 h-10 rounded-full text-[14px] font-medium transition-colors cursor-pointer whitespace-nowrap",
                  active
                    ? "bg-background-50/15 text-background-50"
                    : "text-background-50/80 hover:text-background-50 hover:bg-background-50/10",
                )}
              >
                {n.label}
              </button>
            );
          })}
        </nav>

        {/* End actions */}
        <div className="flex items-center gap-1.5 ms-auto lg:ms-0">
          <IconBtn
            icon="ri-search-line"
            label="بحث"
            onClick={() => {
              setMenuOpen(false);
              setSearchOpen((o) => !o);
            }}
          />
          <IconBtn
            icon="ri-notification-3-line"
            label="الإشعارات"
            badge={NOTIF_COUNT}
            onClick={() => jump("invNotifications")}
          />

          {/* Language */}
          <div className="flex items-center rounded-full p-0.5 text-[12.5px] font-semibold bg-background-50/15">
            <button
              type="button"
              onClick={() => setLang("ar")}
              className={cn(
                "px-2.5 h-7 rounded-full cursor-pointer transition-colors",
                lang === "ar" ? "bg-background-50 text-primary-500" : "text-background-50/80",
              )}
            >
              ع
            </button>
            <button
              type="button"
              onClick={() => setLang("en")}
              className={cn(
                "px-2.5 h-7 rounded-full cursor-pointer transition-colors",
                lang === "en" ? "bg-background-50 text-primary-500" : "text-background-50/80",
              )}
            >
              EN
            </button>
          </div>

          {/* Avatar + dropdown */}
          <div className="relative ms-1">
            <button
              type="button"
              onClick={() => {
                setSearchOpen(false);
                setMenuOpen((o) => !o);
              }}
              className="flex items-center gap-2 h-10 ps-1 pe-2.5 rounded-full bg-background-50/15 hover:bg-background-50/20 transition-colors cursor-pointer"
            >
              <span className="w-8 h-8 rounded-full overflow-hidden bg-background-200 shrink-0">
                <img
                  src={investorProfile.avatar}
                  alt={investorProfile.name}
                  className="w-full h-full object-cover object-top"
                />
              </span>
              <i
                className={cn(
                  "ri-arrow-down-s-line text-[18px] leading-none text-background-50 transition-transform",
                  menuOpen && "rotate-180",
                )}
              />
            </button>

            {menuOpen && (
              <>
                <button
                  type="button"
                  aria-label="إغلاق"
                  onClick={() => setMenuOpen(false)}
                  className="fixed inset-0 z-40 cursor-default"
                />
                <div className="absolute end-0 top-[calc(100%+10px)] w-52 z-50 bg-background-100 border border-background-200 rounded-[16px] p-1.5 shadow-sm">
                  <button
                    type="button"
                    onClick={() => {
                      setMenuOpen(false);
                      jump("invProfile");
                    }}
                    className="w-full h-11 px-3 rounded-[12px] flex items-center gap-2.5 text-[13.5px] font-medium text-foreground-800 hover:bg-background-200 cursor-pointer transition-colors"
                  >
                    <i className="ri-user-line text-[17px] leading-none" />
                    حسابي
                  </button>
                  <div className="h-px bg-background-200 my-1" />
                  <button
                    type="button"
                    onClick={() => {
                      setMenuOpen(false);
                      logout();
                      jump("home");
                    }}
                    className="w-full h-11 px-3 rounded-[12px] flex items-center gap-2.5 text-[13.5px] font-medium text-danger-600 hover:bg-danger-50 cursor-pointer transition-colors"
                  >
                    <i className="ri-logout-box-r-line text-[17px] leading-none" />
                    تسجيل الخروج
                  </button>
                </div>
              </>
            )}
          </div>
        </div>
      </div>

      {/* Search panel */}
      {searchOpen && (
        <div className="border-t border-background-50/10">
          <div className="w-full max-w-[1280px] mx-auto px-4 md:px-8 py-3.5 flex items-center gap-2">
            <div className="flex-1 flex items-center gap-2.5 h-11 px-4 rounded-full bg-background-50/10 border border-background-50/20 focus-within:border-background-50/50 transition-colors">
              <i className="ri-search-line text-[18px] text-background-50/70 leading-none" />
              <input
                autoFocus
                value={query}
                onChange={(e) => setQuery(e.target.value)}
                onKeyDown={(e) => {
                  if (e.key === "Enter") runSearch();
                }}
                placeholder="ابحث عن منشأة أو فرصة استثمارية..."
                className="flex-1 bg-transparent text-[14px] text-background-50 placeholder:text-background-50/50 outline-none font-body"
              />
            </div>
            <button
              type="button"
              onClick={runSearch}
              className="h-11 px-6 rounded-full bg-background-50 text-primary-500 text-[13.5px] font-semibold hover:bg-background-100 transition-colors cursor-pointer whitespace-nowrap"
            >
              ابحث
            </button>
          </div>
        </div>
      )}
    </header>
  );
}

function IconBtn({
  icon,
  label,
  badge,
  onClick,
}: {
  icon: string;
  label: string;
  badge?: number;
  onClick: () => void;
}) {
  return (
    <button
      type="button"
      aria-label={label}
      onClick={onClick}
      className="relative w-10 h-10 rounded-full hidden sm:flex items-center justify-center text-background-50 hover:bg-background-50/15 transition-colors cursor-pointer"
    >
      <i className={cn(icon, "text-[20px] leading-none")} />
      {badge !== undefined && badge > 0 && (
        <span className="absolute top-1 end-1 min-w-[17px] h-[17px] px-1 rounded-full bg-secondary-500 text-secondary-950 text-[10px] font-bold flex items-center justify-center">
          {badge}
        </span>
      )}
    </button>
  );
}