/* global React */
// Shared building blocks for the Ajaia Enterprise Training redesign.

const { useState, useEffect, useRef, useMemo, useCallback } = React;

// ─── Hook: scroll reveal (IntersectionObserver) ──────────────────────
function useReveal() {
  useEffect(() => {
    const io = new IntersectionObserver((entries) => {
      for (const e of entries) {
        if (e.isIntersecting) {
          e.target.classList.add("in");
          io.unobserve(e.target);
        }
      }
    }, { threshold: 0.12, rootMargin: "0px 0px -40px 0px" });
    document.querySelectorAll(".reveal:not(.in)").forEach((el) => io.observe(el));
    return () => io.disconnect();
  }, []);
}

// ─── Hook: count-up tickers ──────────────────────────────────────────
function useCountUp(target, durationMs = 1400, deps = []) {
  const ref = useRef(null);
  const [value, setValue] = useState(0);
  useEffect(() => {
    let raf,started = false,startedAt = 0;
    const io = new IntersectionObserver(([entry]) => {
      if (entry.isIntersecting && !started) {
        started = true;
        const step = (t) => {
          if (!startedAt) startedAt = t;
          const p = Math.min(1, (t - startedAt) / durationMs);
          const eased = 1 - Math.pow(1 - p, 3);
          setValue(Math.round(target * eased));
          if (p < 1) raf = requestAnimationFrame(step);
        };
        raf = requestAnimationFrame(step);
      }
    }, { threshold: 0.4 });
    if (ref.current) io.observe(ref.current);
    return () => {io.disconnect();cancelAnimationFrame(raf);};
  }, [target, durationMs, ...deps]);
  return [ref, value];
}

// ─── Icons (Lucide-style, 1.5px strokes) ─────────────────────────────
const AjIcon = {
  ArrowRight: ({ size = 16, stroke = 1.6 }) =>
  <svg width={size} height={size * 14 / 16} viewBox="0 0 16 14" fill="none"
  stroke="currentColor" strokeWidth={stroke} strokeLinecap="square" strokeLinejoin="miter">
      <path d="M1 7H14M9 2l5 5-5 5" />
    </svg>,

  ArrowUpRight: ({ size = 14 }) =>
  <svg width={size} height={size} viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.6">
      <path d="M5 11L11 5M5 5h6v6" />
    </svg>,

  Plus: ({ size = 14 }) =>
  <svg width={size} height={size} viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.6">
      <path d="M8 3v10M3 8h10" />
    </svg>,

  Check: ({ size = 14 }) =>
  <svg width={size} height={size} viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round">
      <path d="M3 8l3.5 3.5L13 5" />
    </svg>,

  Spark: ({ size = 18 }) =>
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5">
      <path d="M12 3l1.6 5.4L19 10l-5.4 1.6L12 17l-1.6-5.4L5 10l5.4-1.6L12 3z" />
    </svg>,

  Layers: ({ size = 20 }) =>
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5">
      <path d="M12 3l9 5-9 5-9-5 9-5z" /><path d="M3 13l9 5 9-5" /><path d="M3 18l9 5 9-5" />
    </svg>,

  Shield: ({ size = 20 }) =>
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5">
      <path d="M12 2l8 4v6c0 4-3.5 8-8 10-4.5-2-8-6-8-10V6l8-4z" /><path d="M9 12l2 2 4-4" />
    </svg>,

  Workflow: ({ size = 20 }) =>
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5">
      <rect x="3" y="3" width="6" height="6" /><rect x="15" y="3" width="6" height="6" />
      <rect x="9" y="15" width="6" height="6" /><path d="M6 9v3h12V9M12 12v3" />
    </svg>,

  Users: ({ size = 20 }) =>
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5">
      <circle cx="9" cy="8" r="3.5" /><path d="M3 21c0-3.3 2.7-6 6-6s6 2.7 6 6" />
      <circle cx="17" cy="9" r="2.5" /><path d="M14 21c0-2.5 1.6-4.6 3.6-5.4" />
    </svg>,

  Bolt: ({ size = 20 }) =>
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinejoin="round">
      <path d="M13 2L4 14h7l-1 8 9-12h-7l1-8z" />
    </svg>,

  Code: ({ size = 20 }) =>
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round">
      <path d="M8 6l-5 6 5 6M16 6l5 6-5 6M14 4l-4 16" />
    </svg>,

  Compass: ({ size = 20 }) =>
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5">
      <circle cx="12" cy="12" r="9" /><path d="M15.5 8.5l-2 5-5 2 2-5 5-2z" />
    </svg>,

  Chart: ({ size = 20 }) =>
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round">
      <path d="M3 3v18h18" /><path d="M7 14l4-4 3 3 5-7" />
    </svg>,

  Book: ({ size = 20 }) =>
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5">
      <path d="M4 4h7a3 3 0 0 1 3 3v13a2 2 0 0 0-2-2H4V4z" /><path d="M20 4h-7a3 3 0 0 0-3 3v13a2 2 0 0 1 2-2h8V4z" />
    </svg>,

  Quote: ({ size = 28 }) =>
  <svg width={size} height={size} viewBox="0 0 24 24" fill="currentColor">
      <path d="M9 6H4v6h3c0 2-1 4-3 4v2c4 0 6-3 6-7V6zm11 0h-5v6h3c0 2-1 4-3 4v2c4 0 6-3 6-7V6z" />
    </svg>,

  Star: ({ size = 14 }) =>
  <svg width={size} height={size} viewBox="0 0 24 24" fill="currentColor">
      <path d="M12 2l3.1 6.6 7.2 1-5.2 5 1.2 7.2L12 18.4 5.7 21.8l1.2-7.2L1.7 9.6l7.2-1L12 2z" />
    </svg>,

  Briefcase: ({ size = 18 }) =>
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5">
      <rect x="3" y="7" width="18" height="13" /><path d="M8 7V5a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2" />
    </svg>

};

// ─── Decorative paired squares ───────────────────────────────────────
function Squares({ color = "var(--ink-900)", corner = "tr", size = 16 }) {
  const base = { position: "absolute", width: size, height: size, background: color, display: "block" };
  let p1, p2;
  if (corner === "tr") {p1 = { top: size, right: 0 };p2 = { top: 0, right: size };}
  if (corner === "tl") {p1 = { top: size, left: 0 };p2 = { top: 0, left: size };}
  if (corner === "br") {p1 = { bottom: size, right: 0 };p2 = { bottom: 0, right: size };}
  if (corner === "bl") {p1 = { bottom: size, left: 0 };p2 = { bottom: 0, left: size };}
  return <><span style={{ ...base, ...p1 }} /><span style={{ ...base, ...p2 }} /></>;
}

// ─── Button (split CTA) ─────────────────────────────────────────────
function AjButton({ children, variant = "primary", size = "md", href = "#", onClick, style = {} }) {
  const heights = { sm: 38, md: 48, lg: 54 };
  const padding = { sm: "0 16px", md: "0 22px", lg: "0 26px" };
  const arrowW = { sm: 40, md: 50, lg: 56 };
  const h = heights[size];

  const palettes = {
    primary: { bg: "var(--sky-400)", fg: "var(--ink-900)", line: "rgba(0,29,107,0.18)" },
    dark: { bg: "var(--ink-900)", fg: "var(--paper)", line: "rgba(255,255,255,0.18)" },
    ghost: { bg: "transparent", fg: "var(--ink-900)", border: "1px solid rgba(0,29,107,0.18)", line: "rgba(0,29,107,0.18)" },
    outline: { bg: "rgba(255,255,255,0.04)", fg: "var(--paper)", border: "1px solid rgba(255,255,255,0.18)", line: "rgba(255,255,255,0.18)" },
    glow: { bg: "var(--sky-400)", fg: "var(--ink-900)", line: "rgba(0,29,107,0.18)", glow: true }
  };
  const p = palettes[variant];
  const fontSize = size === "lg" ? 16 : size === "sm" ? 13 : 14.5;

  return (
    <a href={href} onClick={onClick} className="aj-cta" style={{
      display: "inline-flex", alignItems: "stretch", textDecoration: "none",
      position: "relative", overflow: "hidden", borderRadius: 0,
      boxShadow: p.glow ? "0 0 0 1px rgba(151,194,247,0.5), 0 0 30px -8px rgba(151,194,247,0.55)" : "none",
      ...style
    }}>
      <span style={{
        display: "inline-flex", alignItems: "center", justifyContent: "center",
        height: h, padding: padding[size],
        background: p.bg, color: p.fg,
        fontFamily: "var(--font-sans)", fontWeight: 600, fontSize, lineHeight: 1,
        letterSpacing: "-0.01em", whiteSpace: "nowrap",
        border: p.border || "none", borderRight: "none"
      }}>{children}</span>
      <span style={{
        display: "inline-flex", alignItems: "center", justifyContent: "center",
        height: h, width: arrowW[size],
        background: p.bg, color: p.fg,
        borderLeft: `1px solid ${p.line}`,
        border: p.border || "none",
        borderLeftWidth: 1, borderLeftStyle: "solid", borderLeftColor: p.line
      }}><AjIcon.ArrowRight size={size === "sm" ? 13 : 15} /></span>
    </a>);

}

// ─── Eyebrow (small tracked tag) ─────────────────────────────────────
function Eyebrow({ children, tone = "light", icon = true }) {
  const dark = tone === "dark";
  return (
    <span style={{
      display: "inline-flex", alignItems: "center", gap: 10,
      padding: "6px 12px",
      background: dark ? "rgba(151,194,247,0.08)" : "rgba(0,29,107,0.06)",
      border: dark ? "1px solid rgba(151,194,247,0.18)" : "1px solid rgba(0,29,107,0.10)",
      backdropFilter: dark ? "blur(10px)" : undefined,
      color: dark ? "var(--sky-400)" : "var(--ink-900)",
      fontFamily: "var(--font-sans)", fontWeight: 700,
      fontSize: 11.5, letterSpacing: "0.14em", textTransform: "uppercase"
    }}>
      {icon && <span style={{ width: 6, height: 6, background: dark ? "var(--sky-400)" : "var(--ink-900)" }} />}
      {children}
    </span>);

}

// ─── Top Nav ─────────────────────────────────────────────────────────
function AjNav() {
  const [scrolled, setScrolled] = useState(false);
  useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 24);
    onScroll();
    window.addEventListener("scroll", onScroll, { passive: true });
    return () => window.removeEventListener("scroll", onScroll);
  }, []);
  const links = ["Services", "Who We Serve", "Insights", "About", "Products"];
  return (
    <header style={{
      position: "fixed", top: 0, left: 0, right: 0, height: 68, zIndex: 100,
      background: scrolled ? "rgba(8,18,60,0.78)" : "rgba(8,18,60,0.40)",
      backdropFilter: "blur(18px)", WebkitBackdropFilter: "blur(18px)",
      borderBottom: `1px solid rgba(255,255,255,${scrolled ? 0.08 : 0.04})`,
      transition: "background 300ms var(--ease), border-color 300ms var(--ease)"
    }}>
      <div className="aj-nav-inner" style={{
        maxWidth: 1440, height: "100%", margin: "0 auto",
        padding: "0 48px", display: "flex", alignItems: "center", gap: 40
      }}>
        <a href="#" style={{ display: "inline-flex" }}>
          <img src={window.__resources.ajaiaLogoWhite} alt="Ajaia" style={{ height: 28, width: "auto", display: "block" }} />
        </a>
        <nav className="hide-md-down" style={{ display: "flex", gap: 28, marginLeft: 16 }}>
          {links.map((l) =>
          <a key={l} href="#" style={{
            color: "rgba(255,255,255,0.82)", textDecoration: "none",
            fontFamily: "var(--font-sans)", fontWeight: 500, fontSize: 13.5,
            letterSpacing: "-0.005em",
            transition: "color var(--dur) var(--ease)"
          }}
          onMouseEnter={(e) => e.currentTarget.style.color = "var(--sky-400)"}
          onMouseLeave={(e) => e.currentTarget.style.color = "rgba(255,255,255,0.82)"}>
              {l}
            </a>
          )}
        </nav>
        <div style={{ marginLeft: "auto", display: "flex", alignItems: "center", gap: 18 }}>
          <AjButton size="sm" variant="primary" href="#contact">Book training</AjButton>
        </div>
      </div>
    </header>);

}

// ─── Footer ──────────────────────────────────────────────────────────
function AjFooter() {
  return (
    <footer style={{ background: "var(--ink-900)", color: "var(--paper)", position: "relative", overflow: "hidden" }}>
      <div className="grid-bg" style={{ opacity: 0.5 }} />
      <div className="container" style={{ position: "relative", paddingTop: 96, paddingBottom: 48 }}>
        {/* Top CTA band */}
        <div className="reveal aj-footer-cta" style={{
          position: "relative", overflow: "hidden",
          background: "linear-gradient(135deg, var(--sky-400) 0%, #B6D4FA 100%)",
          color: "var(--ink-900)",
          padding: "44px 56px",
          display: "grid", gridTemplateColumns: "1fr auto", alignItems: "center", gap: 32
        }}>
          <div>
            <div className="h3" style={{ color: "var(--ink-900)" }}>AI moves quickly—and so should you.</div>
            <div style={{ marginTop: 10, fontFamily: "var(--font-sans)", fontWeight: 500, fontSize: 16, color: "var(--ink-muted)", maxWidth: 640 }}>
              We'll help you turn uncertainty into an actionable plan built for measurable impact.
            </div>
          </div>
          <AjButton variant="dark">Let's Connect</AjButton>
          <Squares color="var(--ink-900)" corner="tr" />
        </div>

        {/* Columns */}
        <div className="aj-footer-cols" style={{ marginTop: 72, display: "grid", gridTemplateColumns: "1.4fr 1fr 1fr 1fr", gap: 56 }}>
          <div>
            <img src={window.__resources.ajaiaLogoWhite} alt="Ajaia" style={{ height: 28, width: "auto", display: "block" }} />
            <div style={{ marginTop: 18, fontFamily: "var(--font-sans)", fontSize: 14, color: "rgba(255,255,255,0.70)", lineHeight: 1.6, maxWidth: 320 }}>
              Enterprise AI strategy, training, and implementation. Headquartered in New York.
            </div>
            <div style={{ marginTop: 20, fontFamily: "var(--font-mono)", fontSize: 12.5, color: "rgba(255,255,255,0.55)" }}>
              450 Park Avenue South<br />New York, NY 10016<br /><br />contact@ajaia.ai
            </div>
          </div>
          {[
          { tag: "Pages", links: ["Home", "Services", "Who We Serve", "Insights", "About", "Products"] },
          { tag: "Services", links: ["AI Strategy & Advisory", "AI Engineering & Automation", "AI Training & Enablement", "Custom AI Development", "AI Agents & Workflow Automation", "Full-Stack AI Implementation"] },
          { tag: "Products", links: ["AI Scout Tool", "Multi-Model AI Platform", "AI Government Proposal Writer", "AI Clinical Training", "AI Translator for Healthcare"] }].
          map((col, i) =>
          <div key={i}>
              <Eyebrow tone="dark">{col.tag}</Eyebrow>
              <ul style={{ listStyle: "none", padding: 0, margin: "20px 0 0", display: "flex", flexDirection: "column", gap: 12 }}>
                {col.links.map((l) =>
              <li key={l}><a href="#" style={{
                  color: "rgba(255,255,255,0.75)", textDecoration: "none",
                  fontFamily: "var(--font-sans)", fontWeight: 500, fontSize: 13.5,
                  transition: "color var(--dur) var(--ease)"
                }}
                onMouseEnter={(e) => e.currentTarget.style.color = "var(--sky-400)"}
                onMouseLeave={(e) => e.currentTarget.style.color = "rgba(255,255,255,0.75)"}>{l}</a></li>
              )}
              </ul>
            </div>
          )}
        </div>

        {/* Bottom */}
        <div style={{ borderTop: "1px solid rgba(255,255,255,0.08)", marginTop: 72, paddingTop: 28, display: "flex", justifyContent: "space-between", alignItems: "center", gap: 24, flexWrap: "wrap" }}>
          <div style={{ display: "flex", gap: 24 }}>
            {["Privacy Policy", "Terms and Conditions", "Careers"].map((l) =>
            <a key={l} href="#" style={{
              color: "rgba(255,255,255,0.5)", textDecoration: "none",
              fontFamily: "var(--font-sans)", fontSize: 12.5
            }}>{l}</a>
            )}
          </div>
          <div style={{ fontFamily: "var(--font-mono)", fontSize: 12, color: "rgba(255,255,255,0.4)" }}>
            © 2026 Ajaia. All rights reserved.
          </div>
        </div>
      </div>
    </footer>);

}

Object.assign(window, { useReveal, useCountUp, AjIcon, Squares, AjButton, Eyebrow, AjNav, AjFooter });