/* Carelon Passport — top navigation */

const Menu = ({ children, style = {} }) => (
  <div className="cp-menu" style={{
    position: "absolute", top: "calc(100% + 10px)", right: 0, zIndex: 60,
    background: "#fff", border: "1px solid var(--line)", borderRadius: 16,
    boxShadow: "0 18px 44px -12px rgba(28,21,48,.32), 0 2px 6px rgba(28,21,48,.06)",
    overflow: "hidden", ...style,
  }}>{children}</div>
);

const TopNav = ({ onOpenChat }) => {
  const toast = useToast();
  const [open, setOpen] = React.useState(null);
  const [q, setQ] = React.useState("");
  const [region, setRegion] = React.useState("Global");
  const rootRef = React.useRef(null);

  React.useEffect(() => {
    const h = (e) => { if (rootRef.current && !rootRef.current.contains(e.target)) setOpen(null); };
    document.addEventListener("mousedown", h);
    return () => document.removeEventListener("mousedown", h);
  }, []);

  const notifs = [
    { icon: "calendar", tone: "violet", title: "Cardiology follow-up confirmed", body: "The London Clinic · Jun 18, 9:30 AM", time: "2h" },
    { icon: "pill", tone: "teal", title: "Prescription ready for travel", body: "Atorvastatin · 90-day supply approved", time: "5h" },
  ];
  const regions = ["Global", "North America", "Europe", "Middle East"];

  return (
    <header ref={rootRef} style={{
      position: "sticky", top: 0, zIndex: 50, height: 72, flexShrink: 0,
      display: "flex", alignItems: "center", gap: 24, padding: "0 32px",
      background: "rgba(255,255,255,.86)", backdropFilter: "blur(14px)",
      borderBottom: "1px solid var(--line)",
    }}>
      {/* lockup */}
      <div style={{ display: "flex", alignItems: "center", gap: 13, flexShrink: 0 }}>
        <img src="assets/carelon-logo.svg" alt="Carelon" style={{ height: 28, display: "block" }} />
        <span style={{ width: 1, height: 26, background: "var(--line-strong)" }} />
        <div style={{ lineHeight: 1.05 }}>
          <div style={{ fontSize: 17.5, fontWeight: 700, color: "var(--purple)", letterSpacing: "-.015em" }}>Passport</div>
          <div style={{ fontSize: 10.5, fontWeight: 600, color: "var(--ink-3)", letterSpacing: ".04em", textTransform: "uppercase", whiteSpace: "nowrap" }}>Global health navigation</div>
        </div>
      </div>

      {/* search */}
      <div style={{ flex: 1, display: "flex", justifyContent: "center", minWidth: 0 }}>
        <div className="cp-search" style={{
          display: "flex", alignItems: "center", gap: 10, width: "100%", maxWidth: 460, height: 44,
          padding: "0 16px", borderRadius: 13, background: "var(--bg-sunk)", border: "1px solid var(--line)",
          color: "var(--ink-3)",
        }}>
          <Icon name="search" size={18} stroke={1.8} />
          <input
            value={q} onChange={(e) => setQ(e.target.value)}
            placeholder="Search providers, destinations, benefits, or support"
            style={{ flex: 1, border: "none", outline: "none", background: "transparent", fontSize: 13.5, color: "var(--ink)", fontFamily: "inherit", minWidth: 0 }}
          />
          <span style={{ display: "flex", alignItems: "center", gap: 2, padding: "3px 7px", borderRadius: 7, background: "#fff", border: "1px solid var(--line)", fontSize: 11, fontWeight: 600, color: "var(--ink-3)" }}>⌘K</span>
        </div>
      </div>

      {/* right cluster */}
      <div style={{ display: "flex", alignItems: "center", gap: 10, flexShrink: 0 }}>
        <Button variant="soft" size="sm" icon="chat" onClick={onOpenChat} style={{ height: 42, borderRadius: 12 }}>Concierge</Button>

        <div style={{ position: "relative" }}>
          <IconButton name="bell" label="Notifications" badge={2} active={open === "notif"} onClick={() => setOpen(open === "notif" ? null : "notif")} />
          {open === "notif" && (
            <Menu style={{ width: 340 }}>
              <div style={{ padding: "14px 16px", borderBottom: "1px solid var(--line)", display: "flex", justifyContent: "space-between", alignItems: "center" }}>
                <span style={{ fontWeight: 700, fontSize: 14, color: "var(--ink)" }}>Notifications</span>
                <StatusPill tone="purple">2 new</StatusPill>
              </div>
              {notifs.map((n, i) => {
                const t = TONES[n.tone];
                return (
                  <div key={i} className="cp-menu-row" style={{ display: "flex", gap: 12, padding: "13px 16px", cursor: "pointer", borderBottom: i < notifs.length - 1 ? "1px solid var(--line)" : "none" }}>
                    <span style={{ width: 34, height: 34, borderRadius: 10, background: t.bg, color: t.fg, display: "flex", alignItems: "center", justifyContent: "center", flexShrink: 0 }}><Icon name={n.icon} size={17} stroke={1.9} /></span>
                    <div style={{ minWidth: 0 }}>
                      <div style={{ fontSize: 13, fontWeight: 650, color: "var(--ink)" }}>{n.title}</div>
                      <div style={{ fontSize: 12, color: "var(--ink-3)", marginTop: 1 }}>{n.body}</div>
                    </div>
                    <span style={{ fontSize: 11, color: "var(--ink-4)", marginLeft: "auto", flexShrink: 0 }}>{n.time}</span>
                  </div>
                );
              })}
              <div style={{ padding: 10 }}><Button variant="ghost" size="sm" full onClick={() => { setOpen(null); toast({ title: "All caught up", body: "You have no other notifications" }); }}>Mark all as read</Button></div>
            </Menu>
          )}
        </div>

        <div style={{ position: "relative" }}>
          <button className="cp-chip" onClick={() => setOpen(open === "region" ? null : "region")} style={{
            display: "flex", alignItems: "center", gap: 7, height: 42, padding: "0 12px", borderRadius: 12,
            background: open === "region" ? "var(--purple-soft)" : "#fff", border: "1px solid var(--line)", cursor: "pointer",
            fontFamily: "inherit", color: "var(--ink)", fontSize: 13.5, fontWeight: 600,
          }}>
            <Icon name="globe" size={17} stroke={1.8} style={{ color: "var(--purple)" }} />
            {region}
            <Icon name="chevronDown" size={14} stroke={2} style={{ color: "var(--ink-3)" }} />
          </button>
          {open === "region" && (
            <Menu style={{ width: 210 }}>
              <div style={{ padding: "10px 14px 6px", fontSize: 11, fontWeight: 700, letterSpacing: ".05em", textTransform: "uppercase", color: "var(--ink-4)" }}>Coverage region</div>
              {regions.map((r) => (
                <button key={r} className="cp-menu-row" onClick={() => { setRegion(r); setOpen(null); }} style={{
                  display: "flex", alignItems: "center", gap: 10, width: "100%", padding: "10px 14px", border: "none", background: "none", cursor: "pointer",
                  fontFamily: "inherit", fontSize: 13.5, fontWeight: 550, color: "var(--ink)", textAlign: "left",
                }}>
                  <Icon name="check" size={16} stroke={2.2} style={{ color: "var(--purple)", opacity: r === region ? 1 : 0 }} />
                  {r}
                </button>
              ))}
            </Menu>
          )}
        </div>

        <div style={{ position: "relative" }}>
          <button onClick={() => setOpen(open === "profile" ? null : "profile")} style={{ display: "flex", alignItems: "center", gap: 9, padding: "3px 4px 3px 3px", border: "none", background: "none", cursor: "pointer", borderRadius: 999 }}>
            <Avatar initials="JM" size={38} online />
            <Icon name="chevronDown" size={15} stroke={2} style={{ color: "var(--ink-3)" }} />
          </button>
          {open === "profile" && (
            <Menu style={{ width: 240 }}>
              <div style={{ padding: 16, display: "flex", gap: 12, alignItems: "center", borderBottom: "1px solid var(--line)" }}>
                <Avatar initials="JM" size={42} online />
                <div>
                  <div style={{ fontSize: 14, fontWeight: 700, color: "var(--ink)" }}>Jordan Mitchell</div>
                  <div style={{ fontSize: 12, color: "var(--ink-3)" }}>Passport · Platinum plan</div>
                </div>
              </div>
              {[["idCard", "Digital member ID"], ["user", "Account & profile"], ["shield", "Privacy & security"]].map(([ic, label]) => (
                <button key={label} className="cp-menu-row" onClick={() => { setOpen(null); toast({ title: label, body: "Opening…" }); }} style={{ display: "flex", alignItems: "center", gap: 11, width: "100%", padding: "11px 16px", border: "none", background: "none", cursor: "pointer", fontFamily: "inherit", fontSize: 13.5, fontWeight: 550, color: "var(--ink)", textAlign: "left" }}>
                  <Icon name={ic} size={17} stroke={1.8} style={{ color: "var(--ink-3)" }} />{label}
                </button>
              ))}
              <div style={{ borderTop: "1px solid var(--line)", padding: 8 }}>
                <button className="cp-menu-row" onClick={() => { setOpen(null); toast({ title: "Signed out", body: "See you soon, Jordan" }); }} style={{ display: "flex", alignItems: "center", gap: 11, width: "100%", padding: "10px 12px", border: "none", background: "none", cursor: "pointer", fontFamily: "inherit", fontSize: 13.5, fontWeight: 600, color: "var(--coral-ink)", textAlign: "left", borderRadius: 9 }}>
                  <Icon name="arrowRight" size={17} stroke={1.9} />Sign out
                </button>
              </div>
            </Menu>
          )}
        </div>
      </div>
    </header>
  );
};

window.TopNav = TopNav;
