/* global React */
(function () {
const { useState, useEffect, useRef } = React;
const NS = window.AaronChowdhuryFaisalDesignSystem_418701;
const { NavBar, SectionHeading, Button, Input } = NS;

/* Nocturnal São Paulo skyline reel (Aaron's own, hot-linked from Pexels). */
const HERO_VID = 'https://videos.pexels.com/video-files/19910523/19910523-uhd_2560_1440_30fps.mp4';
const HERO_POSTER = 'https://images.pexels.com/videos/19910523/aerial-brazil-city-dji-19910523.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750';

const SC_PROFILE = 'https://soundcloud.com/shotbyfaisal';

/* ---------- scroll reveal (opacity always 1; only transform animates, so
   content is never frozen hidden in a backgrounded preview iframe) ---------- */
const _cbs = new Set();
let _bound = false;
function check() {
  const root = document.getElementById('scroll');
  const vh = root ? root.clientHeight : window.innerHeight;
  _cbs.forEach((e) => {
    const el = e.ref.current; if (!el) { _cbs.delete(e); return; }
    if (el.getBoundingClientRect().top < vh * 0.92) { e.go(); _cbs.delete(e); }
  });
}
function bind() {
  if (_bound) return; _bound = true;
  const root = document.getElementById('scroll');
  const h = () => requestAnimationFrame(check);
  (root || window).addEventListener('scroll', h, { passive: true });
  window.addEventListener('resize', h, { passive: true });
  setTimeout(() => { _cbs.forEach((e) => e.go()); _cbs.clear(); }, 2500);
}
function Reveal({ children, delay = 0, motion = true, style = {}, as = 'div', className: extraCls = '', ...rest }) {
  const ref = useRef(null);
  const [pre, setPre] = useState(motion);
  useEffect(() => {
    if (!motion) { setPre(false); return; }
    const el = ref.current; if (!el) return;
    bind();
    const e = { ref, go: () => setPre(false) };
    _cbs.add(e); requestAnimationFrame(check);
    return () => { _cbs.delete(e); };
  }, [motion]);
  const Tag = as;
  const base = pre ? 'reveal is-pre' : (motion ? 'reveal anim' : 'reveal');
  const cls = extraCls ? base + ' ' + extraCls : base;
  return <Tag ref={ref} className={cls} style={{ animationDelay: delay + 'ms', ...style }} {...rest}>{children}</Tag>;
}

/* ---------- data ---------- */
const GENRES = ['Afro House', 'Tech House', 'Tribal', 'Circuit'];

const MIXES = [
  { t: 'Sunset Social', meta: 'Afro House', url: 'https://soundcloud.com/shotbyfaisal/sunset-social-afro-house' },
  { t: 'Workout Mix 77', meta: 'Tech House · High Energy', url: 'https://soundcloud.com/shotbyfaisal/workout-mix-77' },
  { t: 'Workout Mix 72', meta: 'Tech House · High Energy', url: 'https://soundcloud.com/shotbyfaisal/workout-mix-72' },
];

/* ---------- sections ---------- */
function BgVideo({ src, poster, style }) {
  const ref = useRef(null);
  useEffect(() => {
    const v = ref.current;
    if (!v) return;
    const tryPlay = () => { const p = v.play(); if (p && p.catch) p.catch(() => {}); };
    tryPlay();
    v.addEventListener('canplay', tryPlay);
    v.addEventListener('loadeddata', tryPlay);
    const onInteract = () => tryPlay();
    window.addEventListener('pointerdown', onInteract, { once: true });
    window.addEventListener('touchstart', onInteract, { once: true });
    const t = setTimeout(tryPlay, 400);
    return () => {
      clearTimeout(t);
      v.removeEventListener('canplay', tryPlay);
      v.removeEventListener('loadeddata', tryPlay);
      window.removeEventListener('pointerdown', onInteract);
      window.removeEventListener('touchstart', onInteract);
    };
  }, [src]);
  return <video ref={ref} src={src} poster={poster} muted loop playsInline autoPlay preload="auto" style={style} />;
}

function Hero({ motion }) {
  return (
    <header style={{ position: 'relative', minHeight: '94vh', display: 'flex', flexDirection: 'column', justifyContent: 'center', overflow: 'hidden' }}>
      <BgVideo src={HERO_VID} poster={HERO_POSTER} style={{ position: 'absolute', inset: 0, width: '100%', height: '100%', objectFit: 'cover', filter: 'saturate(0.85) brightness(0.42)' }} />
      <div style={{ position: 'absolute', inset: 0, background: 'linear-gradient(170deg, rgba(13,0,22,0.7) 0%, rgba(8,8,8,0.8) 60%)' }} />
      <div style={{ position: 'absolute', inset: 0, background: 'radial-gradient(100% 70% at 50% 110%, rgba(123,47,255,0.5), transparent 60%)', mixBlendMode: 'screen' }} />
      <div style={{ position: 'absolute', inset: 0, background: 'radial-gradient(60% 50% at 15% 8%, rgba(159,95,255,0.22), transparent 60%)', mixBlendMode: 'screen' }} />
      <div style={{ position: 'relative', textAlign: 'center', padding: '0 32px' }}>
        <Reveal motion={motion} style={{ fontFamily: "'Space Grotesk', sans-serif", fontSize: 12, fontWeight: 700, letterSpacing: '0.25em', textTransform: 'uppercase', color: 'var(--accent)', marginBottom: 22 }}>
          DJ · Afro House · Tech House
        </Reveal>
        <Reveal motion={motion} delay={80} as="h1" style={{ fontFamily: "'Bebas Neue', sans-serif", fontSize: 'clamp(96px, 20vw, 280px)', lineHeight: 0.82, letterSpacing: '0.02em', margin: 0, color: 'var(--text)' }}>
          FAISAL
        </Reveal>
        <Reveal motion={motion} delay={150} as="p" style={{ fontFamily: "'Inter', sans-serif", fontSize: 18, lineHeight: 1.7, color: '#aaa', maxWidth: 480, margin: '28px auto 36px' }}>
          Nocturnal energy, global rhythm. The DJ alias of Aaron Chowdhury — resident at Life Time Buckhead.
        </Reveal>
        <Reveal motion={motion} delay={220} className="hero-cta" style={{ display: 'flex', gap: 12, justifyContent: 'center', flexWrap: 'wrap' }}>
          <Button variant="primary" size="lg" as="a" href="#book">Book Faisal</Button>
          <Button variant="ghost" size="lg" as="a" href="#mixes">▶ Latest Mix</Button>
        </Reveal>
      </div>
    </header>
  );
}

function Marquee() {
  const items = [];
  for (let i = 0; i < 4; i++) GENRES.forEach((g) => items.push(g));
  return (
    <div style={{ borderTop: '1px solid var(--border-soft)', borderBottom: '1px solid var(--border-soft)', overflow: 'hidden', background: 'var(--surface-2)' }}>
      <div className="marquee-track" style={{ display: 'flex', alignItems: 'center', gap: 40, padding: '18px 0', whiteSpace: 'nowrap' }}>
        {items.concat(items).map((g, i) => (
          <span key={i} style={{ display: 'inline-flex', alignItems: 'center', gap: 40 }}>
            <span style={{ fontFamily: "'Bebas Neue', sans-serif", fontSize: 26, letterSpacing: '0.06em', color: i % 2 ? 'var(--accent)' : 'var(--text-muted)' }}>{g}</span>
            <span style={{ width: 5, height: 5, borderRadius: '50%', background: 'var(--border-ghost)' }} />
          </span>
        ))}
      </div>
    </div>
  );
}

function MixRow({ m, open, onToggle }) {
  const src = `https://w.soundcloud.com/player/?url=${encodeURIComponent(m.url)}&color=%237B2FFF&auto_play=${open ? 'true' : 'false'}&hide_related=true&show_comments=false&show_user=true&show_reposts=false&visual=false`;
  return (
    <div style={{ background: open ? 'var(--accent-tint)' : 'var(--surface-2)', border: `1px solid ${open ? 'var(--accent)' : 'var(--border-soft)'}`, borderRadius: 8, overflow: 'hidden', transition: 'border-color .25s ease, background .25s ease' }}>
      <div onClick={onToggle} style={{ display: 'flex', alignItems: 'center', gap: 20, padding: '18px 22px', cursor: 'pointer' }}>
        <div style={{ width: 46, height: 46, borderRadius: '50%', flexShrink: 0, background: open ? 'var(--accent)' : 'transparent', border: `1px solid ${open ? 'var(--accent)' : 'var(--border-ghost)'}`, display: 'flex', alignItems: 'center', justifyContent: 'center', color: open ? 'var(--accent-fg)' : 'var(--text)', fontSize: 14, transition: 'all .2s ease' }}>{open ? '❚❚' : '▶'}</div>
        <div style={{ flex: 1 }}>
          <div style={{ fontFamily: "'Space Grotesk', sans-serif", fontSize: 18, fontWeight: 700, color: 'var(--text)' }}>{m.t}</div>
          <div style={{ fontFamily: "'Space Grotesk', sans-serif", fontSize: 12, fontWeight: 600, letterSpacing: '0.1em', textTransform: 'uppercase', color: 'var(--text-muted)', marginTop: 4 }}>{m.meta}</div>
        </div>
        <div style={{ display: 'flex', alignItems: 'center', gap: 2, height: 32, opacity: open ? 1 : 0.4 }}>
          {Array.from({ length: 28 }).map((_, k) => (
            <span key={k} style={{ width: 3, height: `${20 + Math.abs(Math.sin(k * 1.3)) * 80}%`, background: open ? 'var(--accent)' : 'var(--border-ghost)', borderRadius: 2 }} />
          ))}
        </div>
        <span style={{ fontFamily: "'Space Grotesk', sans-serif", fontSize: 11, fontWeight: 600, letterSpacing: '0.1em', textTransform: 'uppercase', color: open ? 'var(--accent)' : 'var(--text-dim)', width: 64, textAlign: 'right' }}>{open ? 'Close' : 'Play'}</span>
      </div>
      {open && (
        <div style={{ padding: '0 16px 16px' }}>
          <iframe title={m.t} width="100%" height="166" scrolling="no" frameBorder="no" allow="autoplay"
            src={src} style={{ display: 'block', borderRadius: 6 }} />
        </div>
      )}
    </div>
  );
}

function Mixes({ motion }) {
  const [open, setOpen] = useState(null);
  return (
    <section id="mixes" className="sec" style={{ padding: '96px 48px', maxWidth: 1100, margin: '0 auto' }}>
      <Reveal motion={motion} style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-end', marginBottom: 36, flexWrap: 'wrap', gap: 16 }}>
        <SectionHeading kicker="Listen" rule size="section">MIXES</SectionHeading>
        <Button variant="outline" size="sm" as="a" href={SC_PROFILE} target="_blank" rel="noopener noreferrer">All on SoundCloud →</Button>
      </Reveal>
      <div style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
        {MIXES.map((m, i) => (
          <Reveal key={i} motion={motion} delay={i * 80}>
            <MixRow m={m} open={open === i} onToggle={() => setOpen(open === i ? null : i)} />
          </Reveal>
        ))}
      </div>
    </section>
  );
}

function Residency({ motion }) {
  return (
    <section id="residency" className="sec" style={{ padding: '32px 48px 96px', maxWidth: 1100, margin: '0 auto' }}>
      <Reveal motion={motion}>
        <div style={{ position: 'relative', borderRadius: 12, overflow: 'hidden', border: '1px solid var(--border)', background: 'linear-gradient(135deg, #160a26 0%, #0b0612 60%, #080808 100%)', padding: '56px 48px' }}>
          <div style={{ position: 'absolute', inset: 0, background: 'radial-gradient(70% 120% at 90% 10%, var(--accent-tint-strong), transparent 55%)' }} />
          <div className="resi-row" style={{ position: 'relative', display: 'flex', justifyContent: 'space-between', alignItems: 'center', gap: 32, flexWrap: 'wrap' }}>
            <div>
              <div style={{ fontFamily: "'Space Grotesk', sans-serif", fontSize: 12, fontWeight: 700, letterSpacing: '0.15em', textTransform: 'uppercase', color: 'var(--accent)', marginBottom: 14, display: 'flex', alignItems: 'center', gap: 12 }}>
                <span style={{ width: 28, height: 1, background: 'var(--accent)' }} />
                Resident DJ
              </div>
              <div style={{ fontFamily: "'Bebas Neue', sans-serif", fontSize: 'clamp(44px, 6vw, 76px)', lineHeight: 0.92, letterSpacing: '0.02em', color: 'var(--text)' }}>
                LIFE TIME<br />BUCKHEAD
              </div>
              <p style={{ fontFamily: "'Inter', sans-serif", fontSize: 16, lineHeight: 1.7, color: '#aaa', maxWidth: 460, margin: '20px 0 0' }}>
                Holding it down as resident — afro house to tech house, every set built for the room. Upcoming dates landing soon.
              </p>
            </div>
            <div style={{ display: 'flex', flexDirection: 'column', gap: 12, minWidth: 200 }}>
              <Button variant="primary" as="a" href="#book">Get notified of dates</Button>
              <Button variant="ghost" as="a" href={SC_PROFILE} target="_blank" rel="noopener noreferrer">Hear the sound →</Button>
            </div>
          </div>
        </div>
      </Reveal>
    </section>
  );
}

function Book({ motion }) {
  return (
    <section id="book" className="sec" style={{ padding: '96px 48px', borderTop: '1px solid var(--border-soft)', background: 'var(--surface-2)' }}>
      <div style={{ maxWidth: 640, margin: '0 auto', textAlign: 'center' }}>
        <Reveal motion={motion}>
          <SectionHeading kicker="Booking" rule size="display" align="center">BOOK A SET</SectionHeading>
          <p style={{ fontFamily: "'Inter', sans-serif", fontSize: 16, lineHeight: 1.7, color: '#aaa', margin: '20px auto 36px', maxWidth: 460 }}>
            Clubs, festivals, private events, brand activations. No date is too far out — tell me the room and we'll make it move.
          </p>
        </Reveal>
        <Reveal motion={motion} delay={100} className="grid-2" style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 16, textAlign: 'left' }}>
          <Input label="Name" placeholder="Your name" />
          <Input label="Event Date" placeholder="MM / DD / YYYY" />
          <div style={{ gridColumn: '1 / -1' }}><Input label="Venue / Details" multiline rows={4} placeholder="Where, what, how long" /></div>
          <div style={{ gridColumn: '1 / -1', display: 'flex', justifyContent: 'center', marginTop: 8 }}>
            <Button variant="primary" size="lg">Send Booking Inquiry</Button>
          </div>
        </Reveal>
      </div>
    </section>
  );
}

function Footer() {
  return (
    <footer style={{ borderTop: '1px solid var(--border-soft)', padding: '40px 48px' }}>
      <div style={{ maxWidth: 1100, margin: '0 auto', display: 'flex', justifyContent: 'space-between', alignItems: 'center', flexWrap: 'wrap', gap: 16 }}>
        <span style={{ fontFamily: "'Bebas Neue', sans-serif", fontSize: 24, letterSpacing: '0.08em', color: 'var(--accent)' }}>FAISAL</span>
        <div style={{ display: 'flex', gap: 24, alignItems: 'center' }}>
          {[
            { s: 'SoundCloud', href: SC_PROFILE },
            { s: 'Instagram', href: 'https://www.instagram.com/shotbyfaisal/' },
          ].map(({ s, href }) => (
            <a key={s} href={href} target="_blank" rel="noopener noreferrer" style={{ fontFamily: "'Space Grotesk', sans-serif", fontSize: 12, fontWeight: 500, letterSpacing: '0.08em', textTransform: 'uppercase', color: 'var(--text-muted)', textDecoration: 'none' }}>{s}</a>
          ))}
          <span className="brand-aaron"><a href="../" style={{ fontFamily: "'Space Grotesk', sans-serif", fontSize: 12, fontWeight: 500, letterSpacing: '0.08em', textTransform: 'uppercase', color: 'var(--accent)', textDecoration: 'none' }}>Aaron Chowdhury →</a></span>
        </div>
      </div>
    </footer>
  );
}

function MobileNav({ logo, links, scrolled }) {
  const [open, setOpen] = useState(false);
  return (
    <div className="mobile-only" style={{ position: 'sticky', top: 0, zIndex: 60 }}>
      <div style={{ height: 60, display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '0 20px',
        background: (scrolled || open) ? 'rgba(8,8,8,0.92)' : 'transparent', backdropFilter: (scrolled || open) ? 'blur(12px)' : 'none',
        borderBottom: `1px solid ${(scrolled || open) ? 'var(--border)' : 'transparent'}`, transition: 'background .3s ease, border-color .3s ease' }}>
        <span style={{ fontFamily: "'Bebas Neue', sans-serif", fontSize: 24, letterSpacing: '0.08em', color: 'var(--accent)' }}>{logo}</span>
        <button onClick={() => setOpen(!open)} aria-label="Menu" style={{ width: 44, height: 44, display: 'flex', flexDirection: 'column', justifyContent: 'center', gap: 5, background: 'none', border: 'none', cursor: 'pointer', padding: 0 }}>
          <span style={{ width: 24, height: 2, background: 'var(--text)', borderRadius: 2, transition: 'transform .25s ease', transform: open ? 'translateY(7px) rotate(45deg)' : 'none' }} />
          <span style={{ width: 24, height: 2, background: 'var(--text)', borderRadius: 2, opacity: open ? 0 : 1, transition: 'opacity .2s ease' }} />
          <span style={{ width: 24, height: 2, background: 'var(--text)', borderRadius: 2, transition: 'transform .25s ease', transform: open ? 'translateY(-7px) rotate(-45deg)' : 'none' }} />
        </button>
      </div>
      <div style={{ overflow: 'hidden', maxHeight: open ? 360 : 0, transition: 'max-height .35s cubic-bezier(0.16,1,0.3,1)',
        background: 'rgba(8,8,8,0.97)', backdropFilter: 'blur(12px)', borderBottom: open ? '1px solid var(--border)' : 'none' }}>
        <div style={{ display: 'flex', flexDirection: 'column', padding: '8px 20px 20px' }}>
          {links.map((l, i) => (
            <a key={i} href={l.href || '#'} onClick={() => setOpen(false)}
              style={{ fontFamily: "'Space Grotesk', sans-serif", fontSize: 17, fontWeight: 600, letterSpacing: '0.04em',
                textTransform: 'uppercase', textDecoration: 'none', color: l.cta ? 'var(--accent)' : 'var(--text)',
                padding: '15px 0', borderBottom: i < links.length - 1 ? '1px solid var(--border-soft)' : 'none' }}>{l.label}</a>
          ))}
        </div>
      </div>
    </div>
  );
}

function FaisalApp() {
  const [scrolled, setScrolled] = useState(false);
  useEffect(() => {
    const el = document.getElementById('scroll');
    if (!el) return;
    const onScroll = () => setScrolled(el.scrollTop > 40);
    el.addEventListener('scroll', onScroll);
    return () => el.removeEventListener('scroll', onScroll);
  }, []);
  const motion = window.matchMedia('(prefers-reduced-motion: reduce)').matches ? false : true;
  const links = [
    { label: 'Mixes', href: '#mixes' },
    { label: 'Residency', href: '#residency' },
    { label: 'Book', href: '#book', cta: true },
  ];
  return (
    <div className="brand-faisal">
      <div className="desktop-only"><NavBar logo="FAISAL" links={links} scrolled={scrolled} /></div>
      <MobileNav logo="FAISAL" links={links} scrolled={scrolled} />
      <Hero motion={motion} />
      <Marquee />
      <Mixes motion={motion} />
      <Residency motion={motion} />
      <Book motion={motion} />
      <Footer />
    </div>
  );
}

window.FaisalApp = FaisalApp;
})();
