/* Shared components for Viktor Zaharov site */
const { useState, useEffect, useRef } = React;

/* ---- Logo ---- */
// Geometric Z + V interlocking mark — same idea as the user's hand-drawn logo,
// rebuilt as clean SVG so it scales and stays sharp on dark backgrounds.
function LogoMark({ size = 32 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 40 40" fill="none" aria-hidden style={{ display: 'block' }}>
      {/* Z — solid, filled in orange */}
      <path
        d="M10 6 H30 L14 28 H30 V34 H8 L24 12 H10 Z"
        fill="var(--orange)" />
      
      {/* V — outlined triangle, white stroke, sits on top */}
      <path
        d="M6 10 L20 36 L34 10"
        stroke="var(--text)"
        strokeWidth="2.5"
        strokeLinecap="square"
        strokeLinejoin="miter"
        fill="none" />
      
    </svg>);

}

function Logo({ size = 32, stacked = true }) {
  return (
    <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
      <LogoMark size={size} />
      {stacked ?
      <div style={{ display: 'flex', flexDirection: 'column', lineHeight: 1 }}>
          <span style={{ fontWeight: 800, fontSize: 14, letterSpacing: '-0.02em' }}>VIKTOR ZAHAROV</span>
          <span className="mono" style={{ fontSize: 9, letterSpacing: '0.22em', color: 'var(--text-mute)', marginTop: 4 }}>COACHING · EST. 2024</span>
        </div> :

      <span style={{ fontWeight: 800, fontSize: 16, letterSpacing: '-0.02em' }}>VIKTOR ZAHAROV</span>
      }
    </div>);

}

/* ---- Top Nav ---- */
function Nav({ page, setPage, mobile }) {
  const links = [
  { id: 'home', label: 'Home' },
  { id: 'programs', label: 'Programs' },
  { id: 'about', label: 'About' },
  { id: 'testimonials', label: 'Testimonials' },
  { id: 'contact', label: 'Contact' }];

  if (mobile) {
    return (
      <header style={{ position: 'sticky', top: 0, zIndex: 20, background: 'rgba(10,9,8,0.85)', backdropFilter: 'blur(12px)', borderBottom: '1px solid var(--line)' }}>
        <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '14px 20px' }}>
          <Logo size={22} />
          <button className="btn btn-primary btn-sm" onClick={() => setPage('contact')}>Book call</button>
        </div>
        <div style={{ display: 'flex', gap: 16, padding: '0 20px 12px', overflowX: 'auto' }}>
          {links.map((l) =>
          <button key={l.id} className={"nav-link " + (page === l.id ? 'active' : '')} onClick={() => setPage(l.id)} style={{ whiteSpace: 'nowrap', padding: '6px 0', borderBottom: page === l.id ? '2px solid var(--orange)' : '2px solid transparent' }}>{l.label}</button>
          )}
        </div>
      </header>);

  }
  return (
    <header style={{ position: 'sticky', top: 0, zIndex: 20, background: 'rgba(10,9,8,0.78)', backdropFilter: 'blur(14px)', borderBottom: '1px solid var(--line)' }}>
      <div className="wrap" style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', height: 72 }}>
        <button onClick={() => setPage('home')} style={{ cursor: 'pointer' }}><Logo /></button>
        <nav style={{ display: 'flex', gap: 32 }}>
          {links.map((l) =>
          <button key={l.id} className={"nav-link " + (page === l.id ? 'active' : '')} onClick={() => setPage(l.id)}>{l.label}</button>
          )}
        </nav>
        <button className="btn btn-primary btn-sm" onClick={() => setPage('contact')}>
          Book discovery call <span className="arrow">→</span>
        </button>
      </div>
    </header>);

}

/* ---- Footer ---- */
function Footer({ setPage, page, mobile }) {
  const social = [
    { label: 'Instagram', href: 'https://www.instagram.com/viktor_timewolf/' },
    { label: 'YouTube', href: 'https://youtube.com/@viktortimewolf' },
    { label: 'LinkedIn', href: 'https://www.linkedin.com/in/viktor-zaharov-62152610b/' },
    { label: 'X', href: 'https://x.com/viktortimewolf' },
    { label: 'TikTok', href: 'https://www.tiktok.com/@viktortimewolf' },
    { label: 'Threads', href: 'https://www.threads.com/@viktor_timewolf' },
  ];

  // Scroll to top of page (handles both phone-frame and desktop modes)
  const scrollTop = () => {
    const scroller = document.querySelector('.phone-screen') || window;
    scroller.scrollTo({ top: 0, behavior: 'smooth' });
  };

  // Navigate to a page — scroll to top if already there, otherwise switch page
  const navigate = (target) => {
    if (target === page) { scrollTop(); } else { setPage(target); }
  };

  // Programs column — scroll to program anchor, handling same-page case
  const goProgram = (anchor) => {
    const scrollToAnchor = () => {
      const el = document.getElementById(anchor);
      if (el) {
        const scroller = document.querySelector('.phone-screen') || window;
        const rect = el.getBoundingClientRect();
        const currentTop = scroller === window ? window.scrollY : scroller.scrollTop;
        scroller.scrollTo({ top: currentTop + rect.top - 80, behavior: 'smooth' });
      }
    };
    if (page === 'programs') {
      scrollToAnchor();
    } else {
      setPage('programs');
      setTimeout(scrollToAnchor, 80);
    }
  };

  return (
    <footer>
      <div className="wrap" style={{ padding: mobile ? '40px 0' : '80px 0 32px' }}>
        <div style={{ display: 'grid', gridTemplateColumns: mobile ? '1fr' : '1.4fr 1fr 1fr 1fr', gap: 40, marginBottom: 48 }}>
          <div>
            <Logo />
            <p style={{ color: 'var(--text-mute)', marginTop: 20, maxWidth: 340, lineHeight: 1.55, fontSize: 14 }}>
              I help people stop drifting and start building — through trucking, content, and the work nobody else will do for them.
            </p>
            <div style={{ display: 'flex', gap: 8, marginTop: 20, flexWrap: 'wrap' }}>
              {social.map((s) => <a key={s.label} className="tag" data-link="1" href={s.href} target="_blank" rel="noreferrer" style={{ padding: '6px 10px' }}>{s.label}</a>)}
            </div>
          </div>
          <FooterCol title="Navigate" items={[['Home', () => navigate('home')], ['Programs', () => navigate('programs')], ['About', () => navigate('about')], ['Testimonials', () => navigate('testimonials')], ['Contact', () => navigate('contact')]]} />
          <FooterCol title="Programs" items={[
            ['Self-Upgrade', () => goProgram('self')],
            ['Trucking', () => goProgram('trucking')],
            ['YouTube Growth', () => goProgram('youtube')],
            ['Video Editing', () => goProgram('video')],
            ['Meet-Ups & Events', () => goProgram('meetups')]
          ]} />
          <FooterCol title="Contact" items={[
            ['Book a call', () => setPage('contact')],
            ['Email Viktor', { href: 'mailto:viktorzaharov@pm.me' }],
            ['Free guide', () => setPage('free-guide')]
          ]} />
        </div>
        <div className="rule"></div>
        <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginTop: 24, fontSize: 12, color: 'var(--text-dim)', flexWrap: 'wrap', gap: 16 }} className="mono">
          <span>© 2026 VIKTOR ZAHAROV · COACHING</span>
          <div style={{ display: 'flex', gap: 18, flexWrap: 'wrap' }}>
            <button onClick={() => setPage('privacy')} className="nav-link" style={{ cursor: 'pointer', padding: 0, background: 'transparent', border: 0, fontFamily: 'inherit', fontSize: 12, letterSpacing: 'inherit', textTransform: 'inherit' }}>PRIVACY</button>
            <button onClick={() => setPage('terms')} className="nav-link" style={{ cursor: 'pointer', padding: 0, background: 'transparent', border: 0, fontFamily: 'inherit', fontSize: 12, letterSpacing: 'inherit', textTransform: 'inherit' }}>TERMS</button>
            <span>NORTH YORK, ON · CANADA</span>
          </div>
        </div>
      </div>
    </footer>);

}
function FooterCol({ title, items }) {
  return (
    <div>
      <div className="eyebrow" style={{ marginBottom: 18 }}>{title}</div>
      <ul style={{ listStyle: 'none', padding: 0, margin: 0, display: 'flex', flexDirection: 'column', gap: 10 }}>
        {items.map(([label, target]) => {
          const isLink = target && typeof target === 'object' && target.href;
          return (
            <li key={label}>
              {isLink ?
                <a href={target.href} target={target.target || undefined} rel={target.target === '_blank' ? 'noreferrer' : undefined} className="nav-link">{label}</a> :
                <button onClick={target} className="nav-link" style={{ cursor: 'pointer', padding: 0, background: 'transparent', border: 0, textAlign: 'left', display: 'block', width: '100%' }}>{label}</button>
              }
            </li>);
        })}
      </ul>
    </div>);
}

/* ---- Marquee ---- */
function Marquee({ items }) {
  return (
    <div style={{ borderTop: '1px solid var(--line)', borderBottom: '1px solid var(--line)', padding: '18px 0', overflow: 'hidden', background: 'var(--bg-2)' }}>
      <div className="marquee">
        {[...items, ...items].map((it, i) =>
        <div key={i} style={{ display: 'flex', alignItems: 'center', gap: 24, padding: '0 24px', color: 'var(--text-mute)', fontSize: 14, whiteSpace: 'nowrap' }}>
            <span style={{ color: 'var(--orange)' }}>✦</span>
            <span style={{ textTransform: 'uppercase', letterSpacing: '0.06em', fontWeight: 500 }}>{it}</span>
          </div>
        )}
      </div>
    </div>);

}

/* ---- Section header ---- */
function SectionHead({ kicker, title, sub, num }) {
  // On narrow viewports the 80px num gutter wastes too much horizontal room — render num
  // inline next to the kicker instead so the title gets the full content width.
  const isNarrow = typeof window !== 'undefined' && window.innerWidth < 760;
  if (isNarrow) {
    return (
      <div style={{ marginBottom: 36 }}>
        {(num || kicker) &&
          <div style={{ display: 'flex', gap: 12, alignItems: 'center', marginBottom: 14, flexWrap: 'wrap' }}>
            {num && <div className="mono" style={{ fontSize: 11, color: 'var(--orange)', letterSpacing: '0.16em' }}>{num}</div>}
            {kicker && <div className="eyebrow" style={{ margin: 0 }}>{kicker}</div>}
          </div>
        }
        <h2 className="display" style={{ fontSize: 'clamp(30px, 9vw, 44px)', margin: 0, marginBottom: sub ? 14 : 0, maxWidth: '100%' }}>{title}</h2>
        {sub && <p style={{ color: 'var(--text-mute)', fontSize: 16, lineHeight: 1.55, margin: 0, maxWidth: '100%' }}>{sub}</p>}
      </div>);
  }
  return (
    <div style={{ display: 'flex', gap: 24, alignItems: 'flex-start', marginBottom: 48, flexWrap: 'wrap' }}>
      <div style={{ flex: '0 0 80px' }}>
        {num && <div className="mono" style={{ fontSize: 12, color: 'var(--orange)', letterSpacing: '0.1em' }}>{num}</div>}
      </div>
      <div style={{ flex: 1, minWidth: 260 }}>
        {kicker && <div className="eyebrow" style={{ marginBottom: 12 }}>{kicker}</div>}
        <h2 className="display" style={{ fontSize: 'clamp(34px, 5vw, 64px)', margin: 0, marginBottom: sub ? 16 : 0 }}>{title}</h2>
        {sub && <p style={{ color: 'var(--text-mute)', fontSize: 18, maxWidth: 640, lineHeight: 1.5, margin: 0 }}>{sub}</p>}
      </div>
    </div>);
}

/* ---- Stat block ---- */
function Stat({ value, label, suffix }) {
  return (
    <div style={{ padding: '24px 0' }}>
      <div className="display num" style={{ fontSize: 'clamp(48px, 7vw, 84px)', color: 'var(--text)', lineHeight: 1 }}>
        {value}<span style={{ color: 'var(--orange)' }}>{suffix}</span>
      </div>
      <div style={{ marginTop: 8, color: 'var(--text-mute)', fontSize: 14, letterSpacing: '-0.01em', maxWidth: 240 }}>{label}</div>
    </div>);

}

/* ---- Photo placeholder fallback ---- */
function Photo({ src, alt, ratio = '4/5', tag, style, imgStyle, eager }) {
  // The wrapper carries `.photo-wrap` so global CSS guarantees min-width:0, max-width:100%, and overflow:hidden.
  // When the caller passes height:'100%' (Programs page) the aspectRatio is naturally ignored — that's intentional.
  // Falls back to a placeholder block if the image fails to load. Reset on src change.
  const [stage, setStage] = useState(0);
  useEffect(() => { setStage(0); }, [src]);
  const candidate = stage === 0 ? src : null;
  return (
    <div className="photo-wrap" style={{ aspectRatio: ratio, borderRadius: 14, background: '#1a1816', contain: 'layout paint', ...style }}>
      {candidate ?
        <img
          src={candidate}
          alt={alt || ''}
          loading={eager ? 'eager' : 'lazy'}
          decoding="async"
          draggable="false"
          onError={() => setStage((s) => s + 1)}
          style={{ width: '100%', height: '100%', objectFit: 'cover', objectPosition: 'center', display: 'block', ...imgStyle }}
        /> :
        <div className="ph" style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }}>{alt}</div>}
      {tag && <div className="mono" style={{ position: 'absolute', bottom: 12, left: 12, padding: '6px 10px', background: 'rgba(10,9,8,0.7)', backdropFilter: 'blur(8px)', borderRadius: 6, fontSize: 10, letterSpacing: '0.12em', color: 'var(--text)' }}>{tag}</div>}
    </div>);

}

Object.assign(window, { Logo, Nav, Footer, Marquee, SectionHead, Stat, Photo });