// WhatsApp — floating contact widget pinned bottom-right.
// Tap to open a small card with quick-start message options that deep-link
// into WhatsApp with the text prefilled.

const WA_PHONE = "601170068801"; // no '+' for wa.me
const WA_DISPLAY = "+60 11-7006 8801";

const WA_QUICK = [
  {
    label: "Schedule a 30-min intro call",
    kind: "INTRO",
    msg: "Hi Promefy — I'd like to book a 30-minute intro call to discuss a project.",
  },
  {
    label: "I have a project to scope",
    kind: "SCOPE",
    msg: "Hi Promefy — I have a project in mind and would like to discuss scoping options.",
  },
  {
    label: "Question about your services",
    kind: "Q&A",
    msg: "Hi Promefy — I have a question about your services.",
  },
];

function WhatsAppIcon({ size = 22, color = "currentColor" }) {
  return (
    <svg viewBox="0 0 24 24" width={size} height={size} fill={color} xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
      <path d="M.057 24l1.687-6.163a11.867 11.867 0 01-1.587-5.946C.16 5.335 5.495 0 12.05 0a11.817 11.817 0 018.413 3.488 11.824 11.824 0 013.48 8.414c-.003 6.557-5.338 11.892-11.893 11.892a11.9 11.9 0 01-5.688-1.448L.057 24zm6.597-3.807c1.676.995 3.276 1.591 5.392 1.592 5.448 0 9.886-4.434 9.889-9.885.002-5.462-4.415-9.89-9.881-9.892-5.452 0-9.887 4.434-9.889 9.884-.001 2.225.651 3.891 1.746 5.634l-.999 3.648 3.742-.981zm11.387-5.464c-.074-.124-.272-.198-.57-.347-.297-.149-1.758-.868-2.031-.967-.272-.099-.47-.149-.669.149-.198.297-.768.967-.941 1.165-.173.198-.347.223-.644.074-.297-.149-1.255-.462-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.297-.347.446-.521.151-.172.2-.296.3-.495.099-.198.05-.372-.025-.521-.075-.148-.669-1.611-.916-2.206-.242-.579-.487-.501-.669-.51l-.57-.01c-.198 0-.52.074-.792.372s-1.04 1.016-1.04 2.479 1.065 2.876 1.213 3.074c.149.198 2.095 3.2 5.076 4.487.709.306 1.263.489 1.694.626.712.226 1.36.194 1.872.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413z"/>
    </svg>
  );
}

function Whatsapp() {
  const [open, setOpen] = React.useState(false);
  const [hint, setHint] = React.useState(true); // initial nudge bubble
  const panelRef = React.useRef(null);

  // Dismiss the initial hint after a few seconds
  React.useEffect(() => {
    const t = setTimeout(() => setHint(false), 4500);
    return () => clearTimeout(t);
  }, []);

  // Close on outside click + Esc
  React.useEffect(() => {
    if (!open) return;
    const onDown = (e) => {
      if (panelRef.current && !panelRef.current.contains(e.target)) setOpen(false);
    };
    const onKey = (e) => { if (e.key === "Escape") setOpen(false); };
    setTimeout(() => document.addEventListener("mousedown", onDown), 0);
    document.addEventListener("keydown", onKey);
    return () => {
      document.removeEventListener("mousedown", onDown);
      document.removeEventListener("keydown", onKey);
    };
  }, [open]);

  const openWA = (msg) => {
    const url = `https://wa.me/${WA_PHONE}?text=${encodeURIComponent(msg)}`;
    window.open(url, "_blank", "noopener,noreferrer");
    setOpen(false);
  };

  return (
    <>
      <style>{`
        @keyframes waPulse {
          0%, 100% { box-shadow: 0 0 0 0 rgba(37, 211, 102, 0.55), 0 18px 36px -10px rgba(0,0,0,0.30); }
          50%      { box-shadow: 0 0 0 14px rgba(37, 211, 102, 0.0),  0 18px 36px -10px rgba(0,0,0,0.30); }
        }
        @keyframes waPanelIn {
          from { opacity: 0; transform: translateY(8px) scale(0.96); }
          to   { opacity: 1; transform: translateY(0)   scale(1); }
        }
        @keyframes waHintIn {
          from { opacity: 0; transform: translateX(8px); }
          to   { opacity: 1; transform: translateX(0); }
        }
        .wa-quick:hover {
          background: rgba(37, 211, 102, 0.08) !important;
          border-color: rgba(37, 211, 102, 0.40) !important;
        }
      `}</style>

      {/* Initial nudge speech-bubble, shown once */}
      {!open && hint && (
        <div style={{
          position: "fixed",
          right: 92, bottom: 32,
          zIndex: 9998,
          background: "var(--paper, #f5f3ee)",
          color: "var(--fg-1, #0a0a0a)",
          padding: "10px 14px",
          borderRadius: 10,
          fontFamily: "var(--font-sans, system-ui)",
          fontSize: 13.5, lineHeight: 1.35,
          boxShadow: "0 18px 36px -10px rgba(0,0,0,0.30), 0 0 0 1px rgba(10,10,10,0.06)",
          animation: "waHintIn 380ms cubic-bezier(0.16,1,0.3,1) both",
          maxWidth: 220,
          cursor: "pointer",
        }}
          onClick={() => { setHint(false); setOpen(true); }}
        >
          <div style={{ fontWeight: 600, marginBottom: 2 }}>Need answers fast?</div>
          <div style={{ color: "var(--fg-2, #444)" }}>Chat with us on WhatsApp.</div>
          {/* arrow */}
          <span aria-hidden="true" style={{
            position: "absolute",
            right: -6, bottom: 18,
            width: 12, height: 12,
            background: "var(--paper, #f5f3ee)",
            transform: "rotate(45deg)",
            boxShadow: "1px -1px 0 0 rgba(10,10,10,0.06)",
          }}/>
        </div>
      )}

      {/* Panel */}
      {open && (
        <div ref={panelRef} role="dialog" aria-label="Chat with Promefy on WhatsApp" style={{
          position: "fixed",
          right: 24, bottom: 92,
          zIndex: 10000,
          width: 320,
          background: "var(--paper, #f5f3ee)",
          color: "var(--fg-1, #0a0a0a)",
          borderRadius: 14,
          boxShadow: "0 30px 60px -12px rgba(0,0,0,0.35), 0 0 0 1px rgba(10,10,10,0.06)",
          overflow: "hidden",
          animation: "waPanelIn 280ms cubic-bezier(0.16,1,0.3,1) both",
          fontFamily: "var(--font-sans, system-ui)",
        }}>
          {/* Header — WhatsApp green */}
          <div style={{
            background: "#075E54",
            color: "#fff",
            padding: "16px 18px",
            display: "flex", alignItems: "center", gap: 12,
            position: "relative",
          }}>
            <div style={{
              width: 38, height: 38, borderRadius: 38,
              background: "rgba(255,255,255,0.14)",
              display: "flex", alignItems: "center", justifyContent: "center",
            }}>
              <WhatsAppIcon size={22} color="#25D366" />
            </div>
            <div style={{ flex: 1, minWidth: 0 }}>
              <div style={{ fontSize: 14.5, fontWeight: 600 }}>Promefy</div>
              <div style={{ fontSize: 11.5, opacity: 0.85, display: "flex", alignItems: "center", gap: 5 }}>
                <span style={{
                  width: 6, height: 6, borderRadius: 6,
                  background: "#6BE39A", display: "inline-block",
                }}/>
                Typically replies within hours
              </div>
            </div>
            <button
              type="button"
              onClick={() => setOpen(false)}
              aria-label="Close WhatsApp chat panel"
              style={{
                background: "transparent",
                border: "none",
                color: "rgba(255,255,255,0.7)",
                cursor: "pointer",
                fontSize: 18, lineHeight: 1,
                padding: 4,
              }}>×</button>
          </div>

          {/* Body */}
          <div style={{
            padding: "16px 18px 18px",
            background: "var(--bg-1, #fff)",
            display: "flex", flexDirection: "column", gap: 12,
          }}>
            {/* Greeting bubble */}
            <div style={{
              alignSelf: "flex-start",
              maxWidth: "85%",
              padding: "10px 12px",
              background: "var(--bg-2, #f5f3ee)",
              borderRadius: "4px 14px 14px 14px",
              fontSize: 13.5, lineHeight: 1.5,
              color: "var(--fg-1)",
              border: "1px solid var(--border-1, rgba(10,10,10,0.06))",
            }}>
              👋 Hi! Pick a quick-start or send a custom message.
            </div>

            {/* Quick-start options */}
            <div style={{ display: "flex", flexDirection: "column", gap: 8 }}>
              {WA_QUICK.map((q) => (
                <button
                  key={q.kind}
                  type="button"
                  className="wa-quick"
                  onClick={() => openWA(q.msg)}
                  style={{
                    textAlign: "left",
                    padding: "10px 12px",
                    background: "transparent",
                    border: "1px solid var(--border-2, rgba(10,10,10,0.12))",
                    borderRadius: 8,
                    cursor: "pointer",
                    fontFamily: "inherit",
                    fontSize: 13,
                    color: "var(--fg-1)",
                    display: "flex", justifyContent: "space-between", alignItems: "center", gap: 10,
                    transition: "background 140ms ease-out, border-color 140ms ease-out",
                  }}>
                  <span>{q.label}</span>
                  <span aria-hidden="true" style={{
                    fontFamily: "var(--font-mono, ui-monospace, monospace)",
                    fontSize: 10, letterSpacing: "0.12em",
                    color: "#25D366", whiteSpace: "nowrap",
                  }}>{q.kind} ›</span>
                </button>
              ))}
            </div>

            {/* Open WhatsApp directly */}
            <a
              href={`https://wa.me/${WA_PHONE}`}
              target="_blank" rel="noopener noreferrer"
              onClick={() => setOpen(false)}
              style={{
                display: "flex", alignItems: "center", justifyContent: "center", gap: 8,
                padding: "12px",
                background: "#25D366",
                color: "#fff",
                borderRadius: 8,
                textDecoration: "none",
                fontWeight: 600,
                fontSize: 13.5,
                marginTop: 4,
              }}>
              <WhatsAppIcon size={16} color="#fff" />
              Start a new chat
            </a>

            <div style={{
              fontFamily: "var(--font-mono, ui-monospace, monospace)",
              fontSize: 10, letterSpacing: "0.14em",
              color: "var(--fg-3, #888)",
              textAlign: "center",
              textTransform: "uppercase",
            }}>
              {WA_DISPLAY}
            </div>
          </div>
        </div>
      )}

      {/* Floating button */}
      <button
        type="button"
        onClick={() => { setHint(false); setOpen((v) => !v); }}
        aria-label={open ? "Close WhatsApp chat" : "Chat with Promefy on WhatsApp"}
        aria-expanded={open}
        style={{
          position: "fixed",
          right: 24, bottom: 24,
          zIndex: 10001,
          width: 60, height: 60,
          borderRadius: 60,
          background: "#25D366",
          color: "#fff",
          border: "none",
          cursor: "pointer",
          display: "flex", alignItems: "center", justifyContent: "center",
          animation: !open ? "waPulse 2.4s ease-out infinite" : "none",
          boxShadow: "0 18px 36px -10px rgba(0,0,0,0.30)",
          transition: "transform 200ms cubic-bezier(0.16,1,0.3,1)",
          transform: open ? "rotate(45deg)" : "rotate(0)",
        }}
      >
        {open ? (
          <span style={{ fontSize: 30, lineHeight: 1, fontWeight: 300 }}>+</span>
        ) : (
          <WhatsAppIcon size={28} color="#fff" />
        )}
      </button>
    </>
  );
}

Object.assign(window, { Whatsapp });
