/* Hero — video background, logo top-left, editorial headline + glass buttons bottom-left.
   Parallax: video drifts up, logo drifts down, content fades out — all driven by scroll. */

function Hero({ tweaks }) {
  const tintSpeed  = tweaks.tintSpeed || 30;
  const bgRef      = React.useRef(null);
  const brandRef   = React.useRef(null);  // logo
  const contentRef = React.useRef(null);  // headline + copy + buttons
  const hazeRef    = React.useRef(null);
  const exitRef    = React.useRef(null);

  React.useEffect(() => {
    const hero = document.getElementById('home');
    if (!hero) return;
    let raf = 0;
    let lastP = -1;

    const update = () => {
      const rect = hero.getBoundingClientRect();
      const vh   = window.innerHeight;
      const scrollable = Math.max(1, rect.height - vh);
      const p = Math.max(0, Math.min(1, -rect.top / scrollable));
      if (Math.abs(p - lastP) < 0.002) return;
      lastP = p;

      // Video: drifts up + gentle zoom
      if (bgRef.current) {
        const y = -p * vh * 0.24;
        const s = 1 + p * 0.06;
        bgRef.current.style.transform = `translate3d(0,${y.toFixed(2)}px,0) scale(${s.toFixed(4)})`;
      }

      // Logo: drifts down, fades from p≈0.28 to p≈0.78
      if (brandRef.current) {
        const drift = p * vh * 0.13;
        const fp    = Math.max(0, Math.min(1, (p - 0.28) / 0.50));
        const op    = Math.max(0, 1 - Math.pow(fp, 1.3));
        brandRef.current.style.transform = `translate3d(0,${drift.toFixed(2)}px,0)`;
        brandRef.current.style.opacity   = op.toFixed(3);
      }

      // Content: fades slightly earlier than logo
      if (contentRef.current) {
        const fp    = Math.max(0, Math.min(1, (p - 0.22) / 0.46));
        const op    = Math.max(0, 1 - Math.pow(fp, 1.2));
        const drift = p * vh * 0.09;
        contentRef.current.style.opacity   = op.toFixed(3);
        contentRef.current.style.transform = `translate3d(0,${drift.toFixed(2)}px,0)`;
      }

      // Warm haze swells as buildings approach
      if (hazeRef.current) {
        hazeRef.current.style.opacity = Math.min(1, p * 1.2).toFixed(3);
      }

      // Exit fade into next section
      if (exitRef.current) {
        const op = Math.min(1, Math.max(0, (p - 0.62) / 0.34));
        exitRef.current.style.opacity = op.toFixed(3);
      }
    };

    const onScroll = () => {
      if (raf) return;
      raf = requestAnimationFrame(() => { update(); raf = 0; });
    };

    update();
    window.addEventListener('scroll', onScroll, { passive: true });
    window.addEventListener('resize', update);
    return () => {
      window.removeEventListener('scroll', onScroll);
      window.removeEventListener('resize', update);
      if (raf) cancelAnimationFrame(raf);
    };
  }, []);

  return (
    <section
      id="home"
      data-screen-label="Hero"
      className="relative w-full bg-charcoal"
      style={{ height: '170vh' }}
    >
      {/* Pinned viewport */}
      <div className="sticky top-0 w-full h-screen overflow-hidden">

        {/* Video background */}
        <video
          ref={bgRef}
          className="absolute inset-0 w-full h-full object-cover"
          poster="assets/beigman/my.jpg"
          autoPlay muted loop playsInline preload="auto"
          disablePictureInPicture
          aria-label="Beigman Engineering — hero"
          style={{
            zIndex: 1,
            transformOrigin: 'center center',
            willChange: 'transform',
            objectPosition: 'center center',
            backgroundColor: '#0F1B2A',
          }}
        >
          <source src="assets/beigman/final-hero-opt.webm" type="video/webm" />
          <source src="assets/beigman/final-hero-opt.mp4"  type="video/mp4" />
        </video>

        {/* Time-of-day tint */}
        <div
          className="absolute inset-0 pointer-events-none time-tint"
          style={{ animationDuration: `${tintSpeed}s`, mixBlendMode: 'soft-light', zIndex: 2 }}
        />

        {/* Logo — top-left, FLIP target for loader animation */}
        <div
          ref={brandRef}
          className="absolute left-0 top-0 pointer-events-none"
          style={{
            paddingTop:  'clamp(2.5rem, 5vh, 4.5rem)',
            paddingLeft: 'clamp(2.5rem, 4vw, 4rem)',
            zIndex: 6,
            willChange: 'opacity, transform',
          }}
        >
          <div className="rise rise-2" style={{ marginLeft: '-6px' }}>
            <img
              data-hero-brand-logo
              src="assets/logo-transparent.png"
              alt="Beigman Engineering"
              decoding="async"
              style={{
                height: 'clamp(120px, 16vw, 200px)',
                width: 'auto',
                display: 'block',
                filter: 'drop-shadow(0 2px 20px rgba(255,255,255,0.45))',
              }}
              draggable="false"
            />
          </div>
        </div>

        {/* Hero content — bottom-left editorial composition */}
        <div
          ref={contentRef}
          className="absolute left-0 bottom-0"
          style={{
            paddingLeft:   'clamp(2.5rem, 4vw, 4rem)',
            paddingRight:  'clamp(2.5rem, 4vw, 4rem)',
            paddingBottom: 'clamp(2rem, 5vh, 3.5rem)',
            zIndex: 3,
            maxWidth: '620px',
            willChange: 'opacity, transform',
          }}
        >
          <div className="hero-copy-card">
            {/* Headline */}
            <div className="rise rise-3">
              <h1
                style={{
                  fontFamily: "'Cormorant Garamond', Georgia, serif",
                  lineHeight: 1.02,
                  fontWeight: 600,
                  margin: 0,
                }}
              >
                <span
                  style={{
                    display: 'block',
                    fontSize: 'var(--hero-headline-size, clamp(2.6rem, 5vw, 5.5rem))',
                    color: '#101820',
                    letterSpacing: 0,
                    textShadow:
                      '0 1px 18px rgba(255,255,255,0.58), 0 2px 34px rgba(255,255,255,0.28)',
                  }}
                >
                  {tweaks.headline1 || 'Engineering Confidence.'}
                </span>
                <span
                  style={{
                    display: 'block',
                    fontSize: 'var(--hero-headline-size, clamp(2.6rem, 5vw, 5.5rem))',
                    color: '#8F552E',
                    letterSpacing: 0,
                    fontStyle: 'italic',
                    textShadow: '0 1px 16px rgba(255,255,255,0.38)',
                  }}
                >
                  {tweaks.headline2 || 'Delivering Certainty.'}
                </span>
              </h1>
            </div>

            {/* Supporting copy */}
            <div className="rise rise-4" style={{ marginTop: 'var(--hero-copy-gap, clamp(1rem, 2vh, 1.625rem))' }}>
              <p
                style={{
                  fontFamily: "'Inter', system-ui, sans-serif",
                  fontSize: 'clamp(0.875rem, 1.1vw, 1rem)',
                  lineHeight: 1.72,
                  color: '#101820',
                  maxWidth: '420px',
                  textShadow: '0 1px 10px rgba(255,255,255,0.42)',
                  margin: 0,
                }}
              >
                Engineering solutions shaped around your project vision,
                approval pathway and commercial priorities.
              </p>
            </div>

            {/* Authority mantra — copper badge row */}
            <div className="rise rise-4" style={{ marginTop: 'clamp(0.85rem, 1.8vh, 1.25rem)' }}>
              <span
                className="font-heading"
                style={{
                  display: 'inline-block',
                  fontSize: 'clamp(0.5rem, 0.72vw, 0.625rem)',
                  fontWeight: 600,
                  letterSpacing: '0.2em',
                  textTransform: 'uppercase',
                  color: '#8F552E',
                  background: 'rgba(178,124,77,0.12)',
                  border: '1px solid rgba(178,124,77,0.34)',
                  borderRadius: '9999px',
                  padding: '0.5rem 0.95rem',
                  backdropFilter: 'blur(4px)',
                  WebkitBackdropFilter: 'blur(4px)',
                }}
              >
                Fire Engineering: Engineering Accuracy · Practical · Holistic Approach
              </span>
            </div>

            {/* Frosted glass CTA button */}
            <div
              className="rise rise-5"
              style={{
                marginTop: 'var(--hero-cta-gap, clamp(1.75rem, 3vh, 2.5rem))',
                display: 'flex',
                flexWrap: 'wrap',
                gap: '0.75rem',
                pointerEvents: 'auto',
              }}
            >
              <a href="#contact" className="glass-btn glass-btn-copper">
                <span className="glass-btn-label" data-label="GET IN TOUCH">
                  <span>GET IN TOUCH</span>
                </span>
                <svg className="glass-btn-arrow" width="18" height="8" viewBox="0 0 18 8" fill="none" aria-hidden="true">
                  <path d="M0 4 H16 M12 1 L16 4 L12 7" stroke="currentColor" strokeWidth="1.4"
                        strokeLinecap="round" strokeLinejoin="round" />
                </svg>
              </a>
            </div>
          </div>
        </div>

        {/* Warm atmospheric haze — grows over brand to sell depth */}
        <div
          ref={hazeRef}
          className="absolute inset-0 pointer-events-none"
          style={{
            zIndex: 4,
            opacity: 0,
            background:
              'radial-gradient(120% 80% at 20% 25%, rgba(236,225,210,0.55) 0%, rgba(236,225,210,0.18) 35%, rgba(15,27,42,0) 65%)',
            willChange: 'opacity',
          }}
        />

        {/* Exit fade — bottom bleeds into next section tray colour */}
        <div
          ref={exitRef}
          className="absolute inset-0 pointer-events-none"
          style={{
            zIndex: 5,
            opacity: 0,
            background:
              'linear-gradient(to bottom,' +
              ' rgba(15,27,42,0) 0%,' +
              ' rgba(15,27,42,0) 72%,' +
              ' rgba(15,27,42,0.65) 88%,' +
              ' rgba(15,27,42,0.97) 96%,' +
              ' #0F1B2A 100%)',
            willChange: 'opacity',
          }}
        />
      </div>
    </section>
  );
}

Object.assign(window, { Hero });
