import { useState } from "react";
import { useWeb } from "../WebContext";
import { cn } from "../WebUI";
import { BrandLogo } from "@/components/base/BrandLogo";
import type { WebScreen } from "../types";

const NAV: { key: string; screen: WebScreen }[] = [
  { key: "navHome", screen: "home" },
  { key: "navBusinesses", screen: "businesses" },
  { key: "navProducts", screen: "products" },
  { key: "navOpportunities", screen: "opportunities" },
];

export default function WebHeader() {
  const { screen, jump, t, lang, setLang, role, openAuth, logout } = useWeb();
  const [accountOpen, setAccountOpen] = useState(false);
  const [mobileOpen, setMobileOpen] = useState(false);

  return (
    <header className="sticky top-0 inset-x-0 z-40 bg-background-50/95 backdrop-blur border-b border-background-200">
      <div className="max-w-[1200px] mx-auto px-4 md:px-8 h-[72px] flex items-center gap-4">
        {/* Logo */}
        <button
          type="button"
          onClick={() => jump("home")}
          className="flex items-center gap-2.5 cursor-pointer shrink-0"
        >
          <BrandLogo className="w-[46px] h-[33px] text-foreground-950" />
          <span className="font-heading font-bold text-[17px] text-foreground-950 hidden sm:block">
            {t("brand")}
          </span>
        </button>

        {/* Nav */}
        <nav className="hidden lg:flex items-center gap-1 mx-auto">
          {NAV.map((n) => (
            <button
              key={n.key}
              type="button"
              onClick={() => jump(n.screen)}
              className={cn(
                "px-3.5 h-10 rounded-full text-[14px] font-medium transition-colors cursor-pointer whitespace-nowrap",
                screen === n.screen
                  ? "bg-primary-500 text-background-50"
                  : "text-foreground-600 hover:text-foreground-950",
              )}
            >
              {t(n.key)}
            </button>
          ))}
        </nav>

        {/* Right */}
        <div className="flex items-center gap-1.5 mr-auto lg:mr-0">
          <button
            type="button"
            aria-label="بحث"
            onClick={() => jump("products")}
            className="w-10 h-10 rounded-full flex items-center justify-center text-foreground-950 hover:bg-background-200 transition-colors cursor-pointer"
          >
            <i className="ri-search-line text-[20px] leading-none" />
          </button>

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

          {!role ? (
            <button
              type="button"
              onClick={() => openAuth()}
              className="ms-1 h-10 px-5 rounded-full bg-primary-500 text-background-50 text-[13.5px] font-semibold inline-flex items-center justify-center hover:bg-primary-600 transition-colors cursor-pointer whitespace-nowrap"
            >
              {t("login")}
            </button>
          ) : (
            <div className="relative ms-1">
              <button
                type="button"
                onClick={() => setAccountOpen((o) => !o)}
                className="h-10 px-3.5 rounded-full flex items-center gap-2 text-[13.5px] font-semibold cursor-pointer whitespace-nowrap bg-background-200 text-foreground-950"
              >
                <i className="ri-user-3-line text-[16px] leading-none" />
                {t("account")}
                <i className="ri-arrow-down-s-line text-[16px] leading-none" />
              </button>
              {accountOpen && (
                <div className="absolute end-0 top-[calc(100%+8px)] w-52 bg-background-100 border border-background-200 rounded-[16px] p-1.5">
                  <MenuItem icon="ri-user-line" label={t("account")} onClick={() => { setAccountOpen(false); jump("profile"); }} />
                  <MenuItem icon="ri-file-list-3-line" label={t("myOrders")} onClick={() => { setAccountOpen(false); jump("orders"); }} />
                  {role === "business" && (
                    <MenuItem icon="ri-dashboard-line" label={t("boDashboard")} onClick={() => { setAccountOpen(false); jump("bo"); }} />
                  )}
                  {role === "investor" && (
                    <MenuItem icon="ri-line-chart-line" label={t("invFollowing")} onClick={() => { setAccountOpen(false); jump("invFollowing"); }} />
                  )}
                  <div className="h-px bg-background-200 my-1" />
                  <MenuItem icon="ri-logout-box-r-line" label={t("logout")} danger onClick={() => { setAccountOpen(false); logout(); jump("home"); }} />
                </div>
              )}
            </div>
          )}

          {/* Mobile menu */}
          <button
            type="button"
            onClick={() => setMobileOpen((o) => !o)}
            className="lg:hidden w-10 h-10 rounded-full flex items-center justify-center cursor-pointer text-foreground-950"
          >
            <i className="ri-menu-line text-[22px] leading-none" />
          </button>
        </div>
      </div>

      {mobileOpen && (
        <div className="lg:hidden bg-background-50 border-b border-background-200">
          <div className="max-w-[1200px] mx-auto px-4 py-3 flex flex-col gap-1">
            {NAV.map((n) => (
              <button
                key={n.key}
                type="button"
                onClick={() => { setMobileOpen(false); jump(n.screen); }}
                className="h-11 px-4 rounded-[12px] text-start text-[14.5px] font-medium text-foreground-800 hover:bg-background-200 cursor-pointer"
              >
                {t(n.key)}
              </button>
            ))}
          </div>
        </div>
      )}
    </header>
  );
}

function MenuItem({
  icon,
  label,
  onClick,
  danger,
}: {
  icon: string;
  label: string;
  onClick: () => void;
  danger?: boolean;
}) {
  return (
    <button
      type="button"
      onClick={onClick}
      className={cn(
        "w-full h-10 px-3 rounded-[12px] flex items-center gap-2.5 text-[13.5px] font-medium cursor-pointer transition-colors",
        danger ? "text-danger-600 hover:bg-danger-50" : "text-foreground-800 hover:bg-background-200",
      )}
    >
      <i className={cn(icon, "text-[17px] leading-none")} />
      {label}
    </button>
  );
}