/* global React */
// heroes.jsx — лого, иконки, шапка и три hero-композиции

const Icon = {
  play: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="M8 5.5v13l11-6.5-11-6.5Z" fill="currentColor"/></svg>),
  mail: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="M3 6.5h18v11H3v-11Z" stroke="currentColor" strokeWidth="1.6"/><path d="m4 7 8 6 8-6" stroke="currentColor" strokeWidth="1.6"/></svg>),
  arrow: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="M5 12h14M13 6l6 6-6 6" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"/></svg>),
  user: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><circle cx="12" cy="8" r="4" stroke="currentColor" strokeWidth="1.6"/><path d="M4 21c0-4.4 3.6-7 8-7s8 2.6 8 7" stroke="currentColor" strokeWidth="1.6"/></svg>),
  flag: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="M6 3v18M6 4h11l-2.5 4L17 12H6" stroke="currentColor" strokeWidth="1.6" strokeLinejoin="round"/></svg>),
  film: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><rect x="3" y="5" width="18" height="14" rx="2" stroke="currentColor" strokeWidth="1.6"/><path d="M10 9.5v5l4-2.5-4-2.5Z" fill="currentColor"/></svg>),
  doc: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="M6 3h8l4 4v14H6V3Z" stroke="currentColor" strokeWidth="1.6" strokeLinejoin="round"/><path d="M14 3v4h4M9 12h6M9 16h6" stroke="currentColor" strokeWidth="1.6"/></svg>),
  mic: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><rect x="9" y="3" width="6" height="11" rx="3" stroke="currentColor" strokeWidth="1.6"/><path d="M5 11a7 7 0 0 0 14 0M12 18v3" stroke="currentColor" strokeWidth="1.6"/></svg>),
  chart: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><path d="M4 20V4M4 20h16M8 16v-4M12 16V8M16 16v-7" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round"/></svg>),
  target: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><circle cx="12" cy="12" r="8" stroke="currentColor" strokeWidth="1.6"/><circle cx="12" cy="12" r="3.4" stroke="currentColor" strokeWidth="1.6"/></svg>),
  compass: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><circle cx="12" cy="12" r="9" stroke="currentColor" strokeWidth="1.6"/><path d="m15.5 8.5-2 5-5 2 2-5 5-2Z" stroke="currentColor" strokeWidth="1.5" strokeLinejoin="round"/></svg>),
  people: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><circle cx="9" cy="9" r="3" stroke="currentColor" strokeWidth="1.5"/><circle cx="17" cy="10" r="2.4" stroke="currentColor" strokeWidth="1.5"/><path d="M3 19c0-3 2.7-5 6-5s6 2 6 5M16 14c2.8.2 5 1.9 5 5" stroke="currentColor" strokeWidth="1.5"/></svg>),
  yt: (p) => (<svg viewBox="0 0 24 24" fill="none" {...p}><rect x="2.5" y="5.5" width="19" height="13" rx="4" fill="currentColor"/><path d="M10 9.2v5.6l5-2.8-5-2.8Z" fill="#000"/></svg>),
  whatsapp: (p) => (<svg viewBox="0 0 24 24" fill="currentColor" {...p}><path d="M19.05 4.91A10 10 0 0 0 4.1 18.3L3 22l3.8-1A10 10 0 1 0 19.05 4.9Zm-7.03 15.28a8.3 8.3 0 0 1-4.24-1.16l-.3-.18-2.5.65.67-2.43-.2-.31a8.3 8.3 0 1 1 6.57 3.43Zm4.55-6.2c-.25-.13-1.47-.72-1.7-.8-.23-.09-.4-.13-.56.12-.17.25-.64.8-.79.97-.14.17-.29.18-.54.06a6.8 6.8 0 0 1-2-1.24 7.5 7.5 0 0 1-1.38-1.72c-.14-.25 0-.38.11-.5.11-.12.25-.29.37-.43.13-.15.17-.25.25-.42.08-.17.04-.31-.02-.43-.06-.12-.56-1.34-.76-1.84-.2-.48-.4-.41-.56-.42h-.48c-.16 0-.43.06-.66.31-.22.25-.86.85-.86 2.07s.88 2.4 1 2.57c.13.17 1.74 2.65 4.2 3.72.59.25 1.04.4 1.4.52.59.18 1.12.16 1.54.1.47-.07 1.47-.6 1.68-1.18.2-.58.2-1.07.14-1.18-.06-.1-.22-.16-.47-.28Z"/></svg>),
};

// Самодостаточный reveal: React сам владеет классом `in` (через состояние),
// поэтому реконсиляция его не стирает. Скролл-триггер + fallback по таймеру.
function Reveal({ as = "div", className = "", style, children, delay = 0, ...rest }) {
  const El = as;
  const ref = React.useRef(null);
  const [vis, setVis] = React.useState(false);
  React.useEffect(() => {
    if (window.__noMotion) { setVis(true); return; }
    const el = ref.current;
    if (!el) return;
    const check = () => {
      const r = el.getBoundingClientRect();
      if (r.top < window.innerHeight * 0.92 && r.bottom > -40) { setVis(true); return true; }
      return false;
    };
    if (check()) return;
    const onScroll = () => { if (check()) cleanup(); };
    const onMotion = () => { if (window.__noMotion) { setVis(true); cleanup(); } };
    function cleanup() {
      window.removeEventListener("scroll", onScroll);
      window.removeEventListener("resize", onScroll);
      window.removeEventListener("gm:motion", onMotion);
    }
    window.addEventListener("scroll", onScroll, { passive: true });
    window.addEventListener("resize", onScroll);
    window.addEventListener("gm:motion", onMotion);
    const t = setTimeout(check, 450);
    return () => { cleanup(); clearTimeout(t); };
  }, []);
  return (
    <El ref={ref} className={`reveal ${vis ? "in" : ""} ${className}`.trim()}
        style={{ transitionDelay: delay + "ms", ...style }} {...rest}>
      {children}
    </El>
  );
}

function Logo({ size = 40 }) {
  return (
    <a href="index.html" className="logo" aria-label="Олег Гурари — на главную">
      <span className="logo-badge" style={{ width: size, height: size }}>
        <img src="assets/lion.png" alt="" />
      </span>
      <span className="logo-word">OLEG&nbsp;GUR<b>ARI</b></span>
    </a>
  );
}

function currentPage() {
  const f = (window.location.pathname.split("/").pop() || "index.html");
  return f === "" ? "index.html" : f;
}

const NAV = [
  ["Обо мне", "about.html"],
  ["Манифест", "manifesto.html"],
  ["Видео", "videos.html"],
  ["Статьи", "blog.html"],
  ["Галерея", "gallery.html"],
  ["Выступления", "media.html"],
  ["Контакты", "contact.html"],
];

const YT = "https://www.youtube.com/@oleg.gurari/videos";
const WA_MSG = "Привет, посмотрел сайт о вас. Давайте созвонимся";
const WA = "https://wa.me/972559235215?text=" + encodeURIComponent(WA_MSG);

function Header() {
  const [open, setOpen] = React.useState(false);
  const [solid, setSolid] = React.useState(false);
  React.useEffect(() => {
    const onScroll = () => setSolid(window.scrollY > 24);
    onScroll();
    window.addEventListener("scroll", onScroll, { passive: true });
    return () => window.removeEventListener("scroll", onScroll);
  }, []);
  const cur = currentPage();
  return (
    <header className={"site-header" + (solid ? " solid" : "")}>
      <div className="container hdr-inner">
        <Logo />
        <nav className="nav-desktop">
          {NAV.map(([t, h]) => (<a key={t} href={h} className={h === cur ? "active" : undefined} aria-current={h === cur ? "page" : undefined}>{t}</a>))}
        </nav>
        <div className="hdr-actions">
          <button className="lang" aria-label="Сменить язык / שנה שפה" type="button">
            <span data-lang="ru" className="on">RU</span><span className="sep">/</span><span data-lang="he">עב</span>
          </button>
          <a className="btn btn-gold btn-sm" href={YT} target="_blank" rel="noopener" aria-label="YouTube">
            {Icon.yt({ style: { width: 18, height: 18 } })} <span>YouTube</span>
          </a>
          <button className={"burger" + (open ? " x" : "")} onClick={() => setOpen(o => !o)} aria-label="Меню">
            <span></span><span></span><span></span>
          </button>
        </div>
      </div>
      <div className={"mobile-menu" + (open ? " show" : "")}>
        {NAV.map(([t, h]) => (<a key={t} href={h} className={h === cur ? "active" : undefined} onClick={() => setOpen(false)}>{t}</a>))}
        <a className="btn btn-gold" href={YT} target="_blank" rel="noopener" style={{ marginTop: 8 }}>
          {Icon.yt({ style: { width: 18, height: 18 } })} Смотреть на YouTube
        </a>
      </div>
    </header>
  );
}

const HERO_EYEBROW = "GURARI MARKETING · ИЗРАИЛЬ";
const HERO_H1 = "Владелец бизнеса — это профессия";
const HERO_LEAD = "38 лет в предпринимательстве. Помогаю владельцам управлять компанией на основе данных, а не мнений — и чувствовать себя счастливыми в своём деле.";

function HeroButtons() {
  return (
    <div className="hero-btns">
      <a className="btn btn-gold" href={YT} target="_blank" rel="noopener">
        {Icon.play({ style: { width: 18, height: 18 } })} Смотреть на YouTube
      </a>
      <a className="btn btn-ghost" href={WA} target="_blank" rel="noopener">
        {Icon.whatsapp({ style: { width: 18, height: 18 } })} Написать мне
      </a>
    </div>
  );
}

const MINI_STATS = [
  ["38", "лет в бизнесе"],
  ["200+", "выступлений"],
  ["×50", "рост оборота"],
];

function MiniStats() {
  return (
    <div className="hero-stats">
      {MINI_STATS.map(([n, l]) => (
        <div className="hero-stat" key={l}>
          <div className="num">{n}</div>
          <div className="lbl">{l}</div>
        </div>
      ))}
    </div>
  );
}

// ---- Вариант A: «Раскладка» ----
function HeroSplit({ pRef }) {
  return (
    <section className="hero hero-split" id="top">
      <div className="hero-glow g1"></div>
      <div className="hero-glow g2"></div>
      <div className="container two-col hero-inner">
        <Reveal className="hero-copy">
          <span className="eyebrow">{HERO_EYEBROW}</span>
          <h1 className="hero-title">{HERO_H1}</h1>
          <p className="lead hero-lead">{HERO_LEAD}</p>
          <HeroButtons />
          <MiniStats />
        </Reveal>
        <Reveal className="hero-media" delay={120}>
          <div className="portrait-card">
            <img ref={pRef} src="assets/portrait-chair.jpg" alt="Олег Гурари" className="parallax" data-speed="0.12" />
            <div className="name-plate">
              <div className="np-name">Олег Гурари</div>
              <div className="np-role">Основатель GURARI Marketing</div>
            </div>
          </div>
        </Reveal>
      </div>
    </section>
  );
}

// ---- Вариант B: «Силуэт» (вырезанная фигура) ----
function HeroCutout({ pRef }) {
  return (
    <section className="hero hero-cutout" id="top">
      <div className="hero-glow g1"></div>
      <div className="hero-glow g2"></div>
      <div className="container hero-cutout-inner">
        <Reveal className="hero-copy">
          <span className="eyebrow">{HERO_EYEBROW}</span>
          <h1 className="hero-title">{HERO_H1}</h1>
          <p className="lead hero-lead">{HERO_LEAD}</p>
          <HeroButtons />
          <MiniStats />
        </Reveal>
      </div>
      <div className="cutout-wrap">
        <img ref={pRef} src="assets/portrait-cutout.png" alt="Олег Гурари" className="cutout-img parallax" data-speed="0.06" />
      </div>
    </section>
  );
}

// ---- Вариант C: «Кинокадр» (full-bleed фото) ----
function HeroCinematic({ pRef }) {
  return (
    <section className="hero hero-cinematic" id="top">
      <div className="cine-bg">
        <img ref={pRef} src="assets/portrait-smoke.jpg" alt="Олег Гурари" className="parallax" data-speed="0.08" />
        <div className="cine-scrim"></div>
      </div>
      <div className="container cine-inner">
        <Reveal className="hero-copy">
          <span className="eyebrow">{HERO_EYEBROW}</span>
          <h1 className="hero-title">{HERO_H1}</h1>
          <p className="lead hero-lead">{HERO_LEAD}</p>
          <HeroButtons />
          <MiniStats />
        </Reveal>
      </div>
    </section>
  );
}

function Hero({ variant, pRef }) {
  if (variant === "cutout") return <HeroCutout pRef={pRef} />;
  if (variant === "cinematic") return <HeroCinematic pRef={pRef} />;
  return <HeroSplit pRef={pRef} />;
}

Object.assign(window, { Icon, Reveal, Logo, Header, Hero, NAV, YT, WA, MINI_STATS });
