// Azlani — shared components used across pages

// Brandmark — uses uploaded Azlani logo asset (house + headset + chimney).
const AzlaniLogo = ({ size = 28, style = {} }) => (
  <img
    src="logo-icon.png"
    alt="Azlani"
    style={{ height: size, width: 'auto', display: 'block', ...style }}
  />
);

const Nav = ({ current }) => (
  <nav className="nav">
    <a href="index.html" className="brand">
      <span className="brand-mark">
        <AzlaniLogo size={32} />
        <span className="brand-word">Azlani</span>
      </span>
    </a>
    <div className="nav-links">
      <a href="index.html#product" style={{ fontWeight: current === 'home' ? 600 : 400 }}>Product</a>
      <a href="index.html#how">How it works</a>
      <a href="pricing.html" style={{ fontWeight: current === 'pricing' ? 600 : 400 }}>Pricing</a>
      <a href="faq.html" style={{ fontWeight: current === 'faq' ? 600 : 400 }}>FAQ</a>
    </div>
    <a href="contact.html" className="cta-mini">Book demo →</a>
  </nav>
);

const Footer = () => (
  <footer className="footer">
    <div className="footer-inner">
      <div className="footer-top">
        <div>
          <div className="eyebrow"><span className="dot"></span> Get started</div>
          <h2 className="serif" style={{ fontSize: 'clamp(40px, 5vw, 72px)', lineHeight: 1, marginTop: 16, maxWidth: 700, letterSpacing: '-0.02em' }}>
            Built around <span className="ital">your</span> intake — <br/>not the other way around.
          </h2>
          <div style={{ display: 'flex', gap: 12, marginTop: 32, flexWrap: 'wrap' }}>
            <a href="contact.html" className="btn btn-primary">Book a 10-minute demo <span className="arrow">↗</span></a>
          </div>
        </div>
      </div>
      <div className="footer-mark"><img src="logo-full.png" alt="Azlani — AI agents for roofer companies" /></div>
      <div className="footer-bottom">
        <span>© 2026 Azlani · AI front office for inbound leads</span>
        <div className="footer-links">
          <a href="contact.html">Contact</a>
          <a href="pricing.html">Pricing</a>
          <a href="faq.html">FAQ</a>
          <a href="mailto:contact@azlani.com">contact@azlani.com</a>
        </div>
      </div>
    </div>
  </footer>
);

// Hook: reveal element when it enters viewport.
// Uses both IntersectionObserver AND a sync in-viewport check + scroll listener
// fallback so it always fires regardless of iframe quirks.
const useReveal = () => {
  const ref = React.useRef(null);
  const [shown, setShown] = React.useState(false);
  React.useEffect(() => {
    if (!ref.current) return;
    let done = false;
    const finish = () => { if (!done) { done = true; setShown(true); } };

    const check = () => {
      if (!ref.current) return;
      const r = ref.current.getBoundingClientRect();
      const vh = window.innerHeight || document.documentElement.clientHeight;
      // Reveal once the element's top has crossed below 92% of the viewport,
      // regardless of whether it's still on-screen (handles fast-scroll past).
      if (r.top < vh * 0.92) finish();
    };

    // Defer the initial check by 2 RAFs so the hidden state has a chance to
    // paint first — guarantees the CSS transition has a "from" frame.
    requestAnimationFrame(() => requestAnimationFrame(check));
    if (done) return;

    // IntersectionObserver primary
    let obs;
    try {
      obs = new IntersectionObserver(([e]) => {
        if (e.isIntersecting) finish();
      }, { threshold: 0.05, rootMargin: '0px 0px -8% 0px' });
      obs.observe(ref.current);
    } catch (_) {}

    // Scroll/resize fallback
    const onScroll = () => { check(); if (done) cleanup(); };
    window.addEventListener('scroll', onScroll, { passive: true });
    window.addEventListener('resize', onScroll);

    const cleanup = () => {
      window.removeEventListener('scroll', onScroll);
      window.removeEventListener('resize', onScroll);
      if (obs) obs.disconnect();
    };
    return cleanup;
  }, []);
  return [ref, shown];
};

// FadeUp/RevealLines: plain pass-throughs. Hidden-tab CSS animation pause was
// causing above-the-fold content to render blank in some iframe contexts; the
// page has plenty of other motion (auroras, ticker, scroll scenes, marquees).
const RevealLines = ({ lines, className = '', style = {} }) => (
  <div className={className} style={style}>
    {lines.map((line, i) => (
      <span key={i} style={{ display: 'block' }}>{line}</span>
    ))}
  </div>
);

const FadeUp = ({ children, className = '', style = {} }) => (
  <div className={className} style={style}>{children}</div>
);

// Tiny mini-phone component used in lots of places
const PhoneFrame = ({ children, scale = 1 }) => (
  <div className="phone" style={{ transform: `scale(${scale})`, transformOrigin: 'top center' }}>
    <div className="phone-screen">
      {children}
    </div>
  </div>
);

Object.assign(window, { Nav, Footer, useReveal, RevealLines, FadeUp, PhoneFrame, AzlaniLogo });
