// yolo. marketing site — editorial magazine + polaroid hero
const { useState, useEffect, useRef, useMemo } = React;

// Polaroid photo bank — using picsum seeds so the photos are consistent and travel-y.
const PHOTOS = [
  { seed: 'reykjavik',  caption: 'Reykjavík · ‑8°',     hand: 'Inga' },
  { seed: 'kyoto-rain', caption: 'Kyoto, raining',       hand: 'M.' },
  { seed: 'surf-ojai',  caption: 'first time up',        hand: 'J' },
  { seed: 'lisbon-22',  caption: 'Lisbon w/ S',           hand: 'a.' },
  { seed: 'patagonia',  caption: 'Patagonia, day 4',      hand: 'L' },
  { seed: 'marrakech',  caption: 'Marrakech market',      hand: 'r' },
  { seed: 'oaxaca-mez', caption: 'mezcal, oaxaca',        hand: 'p.' },
  { seed: 'tokyo-mid',  caption: 'Tokyo, midnight',       hand: 'k' },
  { seed: 'hanoi-77',   caption: 'Hà Nội · 77¢ pho',      hand: 'V' },
  { seed: 'iceland-cave', caption: 'ice cave, finally',   hand: 'M' },
  { seed: 'big-sur',    caption: 'Big Sur, no service',   hand: 'a' },
  { seed: 'bali-sun',   caption: 'Bali · 5:43am',         hand: 's' },
  { seed: 'nyc-snow',   caption: 'NYC, first snow',       hand: 'J.' },
  { seed: 'amalfi',     caption: 'Amalfi, sunburnt',      hand: 'L' },
  { seed: 'jaipur-hot', caption: 'Jaipur, too hot',       hand: 'r' },
  { seed: 'banff',      caption: 'Banff, before sunrise', hand: 'k.' },
  { seed: 'porto-tile', caption: 'Porto tiles',           hand: 'a' },
  { seed: 'sahara-2am', caption: 'Sahara · 2am',          hand: 'i' },
  { seed: 'budapest',   caption: 'Budapest baths',        hand: 's' },
  { seed: 'sf-fog',     caption: 'SF, the fog',           hand: 'M.' },
  { seed: 'mexico-cit', caption: 'CDMX, churros',         hand: 'p' },
  { seed: 'queenstown', caption: 'Queenstown skydive',    hand: 'A' },
  { seed: 'cinque',     caption: 'Cinque Terre',          hand: 'L' },
  { seed: 'desert-rd',  caption: 'desert road, AZ',       hand: 'J' },
  { seed: 'tulum-22',   caption: 'Tulum, finally',        hand: 'm.' },
  { seed: 'seoul-han',  caption: 'Seoul, Han at dusk',    hand: 'k' },
  { seed: 'norway-fj',  caption: 'fjord, day 2',          hand: 'E' },
  { seed: 'pnp-flag',   caption: 'Cape Point',            hand: 's.' },
  { seed: 'rome-7am',   caption: 'Rome · 7am, empty',     hand: 'L.' },
  { seed: 'kerala-bw',  caption: 'Kerala backwaters',     hand: 'R' },
  { seed: 'havana-66',  caption: 'Havana, 1966 vibes',    hand: 'j' },
  { seed: 'paris-rain', caption: 'Paris in the rain',     hand: 'm' },
  { seed: 'sydney-nye', caption: 'Sydney NYE',            hand: 'A.' },
  { seed: 'istanbul-x', caption: 'İstanbul, both sides',  hand: 'd' },
  { seed: 'capetown-t', caption: 'Table Mtn., windy',     hand: 'n' },
  { seed: 'goa-mons',   caption: 'Goa, monsoon',          hand: 'p.' },
  { seed: 'sapa-rice',  caption: 'Sa Pa terraces',        hand: 'V.' },
  { seed: 'taroko',     caption: 'Taroko Gorge',          hand: 'c' },
  { seed: 'chamonix',   caption: 'Chamonix, top',         hand: 'L' },
  { seed: 'wadi-rum',   caption: 'Wadi Rum, no moon',     hand: 'i.' },
  { seed: 'oslo-summ',  caption: 'Oslo, white nights',    hand: 'E.' },
  { seed: 'tasmania',   caption: 'Tassie east coast',     hand: 'A' },
  { seed: 'puglia-sun', caption: 'Puglia, white town',    hand: 'l' },
  { seed: 'cuzco-rd',   caption: 'Cuzco, getting there',  hand: 'm' },
  { seed: 'banff-lake', caption: 'Moraine, before crowd', hand: 'k' },
  { seed: 'galapagos',  caption: 'Galápagos, swam w/',    hand: 'd.' },
  { seed: 'edinburgh',  caption: 'Edinburgh, January',    hand: 'C' },
  { seed: 'lapland',    caption: 'aurora, finally',       hand: 'E' },
  { seed: 'mongolia',   caption: 'Gobi, no signal',       hand: 'b' },
  { seed: 'osaka-st',   caption: 'Dōtonbori, late',       hand: 'k.' },
  { seed: 'naxos-sea',  caption: 'Naxos, swimming',       hand: 'a' },
  { seed: 'crete-22',   caption: 'Crete · taverna',       hand: 'L.' },
  { seed: 'la-paz',     caption: 'La Paz, dizzy',         hand: 'r' },
  { seed: 'helsinki',   caption: 'Helsinki sauna',        hand: 'M' },
  { seed: 'la-rooftop', caption: 'LA, that rooftop',      hand: 'j.' },
  { seed: 'koh-rong',   caption: 'Koh Rong · plankton',   hand: 'V' },
  { seed: 'sicily-am',  caption: 'Taormina sunrise',      hand: 'L' },
  { seed: 'iguazu',     caption: 'Iguazú, drenched',      hand: 'p' },
  { seed: 'bryce-can',  caption: 'Bryce, dawn',           hand: 'k' },
  { seed: 'antelope-c', caption: 'Antelope Canyon',       hand: 'j' },
  { seed: 'phong-nha',  caption: 'Phong Nha, dripping',   hand: 'V.' },
  { seed: 'sintra-fog', caption: 'Sintra in fog',         hand: 'a.' },
  { seed: 'svalbard',   caption: 'Svalbard, polar bear',  hand: 'E' },
  { seed: 'milan-tram', caption: 'Milan tram #1',         hand: 'L' },
  { seed: 'cartagena',  caption: 'Cartagena, salsa',      hand: 'p.' },
  { seed: 'lake-como',  caption: 'Como, ferry',           hand: 'M.' },
  { seed: 'medellin',   caption: 'Medellín, comuna 13',   hand: 'r' },
  { seed: 'bhutan-22',  caption: 'Tiger&rsquo;s Nest',           hand: 'd' },
  { seed: 'fiji-noon',  caption: 'Fiji · island #3',      hand: 'A' },
  { seed: 'newzealand', caption: 'Milford Sound',         hand: 'L.' },
  { seed: 'lalibela',   caption: 'Lalibela, holy week',   hand: 's' },
  { seed: 'beirut-bal', caption: 'Beirut, balcony night', hand: 'n' },
  { seed: 'venice-am',  caption: 'Venice · 6am no tourists', hand: 'L' },
  { seed: 'mumbai-rd',  caption: 'Mumbai · raining',      hand: 'p' },
  { seed: 'machu-pic',  caption: 'Machu Picchu, finally', hand: 'j.' },
  { seed: 'maldives',   caption: 'Maldives · still',      hand: 'a' },
  { seed: 'angkor-am',  caption: 'Angkor · sunrise',      hand: 'V' },
  { seed: 'rio-corc',   caption: 'Rio, Corcovado',        hand: 'b' },
  { seed: 'dubrovnik',  caption: 'Dubrovnik walls',       hand: 'L' },
  { seed: 'siena-pal',  caption: 'Siena, palio day',      hand: 'L.' },
  { seed: 'kathmandu',  caption: 'Kathmandu, momos',      hand: 'p' },
  { seed: 'oahu-no2',   caption: 'Oahu · north shore',    hand: 's' },
  { seed: 'bordeaux',   caption: 'Bordeaux, the wine',    hand: 'M' },
  { seed: 'fes-leather',caption: 'Fès tannery',           hand: 'r' },
  { seed: 'singapore',  caption: 'Singapore, midnight',   hand: 'k.' },
  { seed: 'salzburg',   caption: 'Salzburg, snow',        hand: 'E' },
  { seed: 'havana-2',   caption: 'Havana, malecón',       hand: 'j' },
  { seed: 'okinawa',    caption: 'Okinawa, quiet',        hand: 'M.' },
  { seed: 'andalucia',  caption: 'Sevilla, naranjos',     hand: 'a' },
  { seed: 'nara-deer',  caption: 'Nara · deer everywhere',hand: 'k' },
  { seed: 'utah-arc',   caption: 'Arches, golden hour',   hand: 'J' },
  { seed: 'split-cro',  caption: 'Split, harbour',        hand: 'A.' },
  { seed: 'galway',     caption: 'Galway, raining (yes)', hand: 'C' },
  { seed: 'gili-air',   caption: 'Gili Air · snorkel',    hand: 'd' },
  { seed: 'jokulsarl',  caption: 'Jökulsárlón floes',     hand: 'E.' },
  { seed: 'georgia-tb', caption: 'Tbilisi · supra night', hand: 'n' },
  { seed: 'comino-bg',  caption: 'Comino · blue lagoon',  hand: 'L' },
  { seed: 'kakadu',     caption: 'Kakadu, crocs',         hand: 'A' },
  { seed: 'taj-am',     caption: 'Taj · 6:14am',          hand: 'p.' },
  { seed: 'st-john',    caption: 'St. John, sailing',     hand: 'j.' },
  { seed: 'mara-safari',caption: 'Maasai Mara',           hand: 's.' },
];

// pseudo-random deterministic positions for polaroid scatter — looks chaotic, stays stable
// Bleeds beyond the hero edges so coverage is full corner-to-corner.
const COLS = 10;
const ROWS = 7;
const SCATTER = PHOTOS.map((_, i) => {
  const cell = i % (COLS * ROWS);
  const col = cell % COLS;
  const row = Math.floor(cell / COLS);
  // base position in cell — extend slightly beyond [0,1] so polaroids bleed off edges
  const baseX = -0.05 + (col + 0.5) / COLS * 1.10;
  const baseY = -0.04 + (row + 0.5) / ROWS * 1.08;
  const phi = 0.61803398875;
  const jx = (((i * phi * 11) % 1) - 0.5) * 0.10;
  const jy = (((i * phi * 13 + 0.17) % 1) - 0.5) * 0.12;
  const x = baseX + jx;
  const y = baseY + jy;
  // wild rotation — photos dumped from a shoebox
  const rot = ((i * 137.508) % 90) - 45; // -45 to +45 degrees
  const scale = 0.78 + ((i * 0.41) % 0.62); // 0.78 → 1.40
  return { x, y, rot, scale, z: (i * 17) % 100 };
});

// ============ POLAROID ============
function Polaroid({ photo, x, y, rot, scale, z, gridMode }) {
  const left = gridMode === 'grid'
    ? `${(x * 100)}%`
    : `${(x * 100)}%`;
  const top = gridMode === 'grid'
    ? `${(y * 100)}%`
    : `${(y * 100)}%`;
  const rotation = gridMode === 'grid' ? 0 : rot;

  return (
    <div
      className="polaroid"
      style={{
        left, top,
        transform: `translate(-50%, -50%) rotate(${rotation}deg) scale(${scale})`,
        zIndex: z,
      }}
    >
      <div className="polaroid-photo">
        <img
          src={`https://picsum.photos/seed/${photo.seed}/400/500`}
          alt=""
          loading="lazy"
        />
      </div>
      <div className="polaroid-caption">
        <span className="hand">{photo.hand}</span>
        <span>{photo.caption}</span>
      </div>
    </div>
  );
}

// ============ HERO ============
function Hero({ variant, onCTAClick }) {
  // variants: scatter (default), grid, ticker
  // Shuffle the photo deck once per mount. SCATTER is deterministic
  // (jitter is a function of cell index), so the layout stays stable
  // within a page load, but each refresh gets a new photo→cell
  // assignment. This also spreads out any picsum seed collisions
  // where two different seeds happen to return the same image.
  const photos = useMemo(() => {
    const arr = PHOTOS.slice();
    for (let i = arr.length - 1; i > 0; i--) {
      const j = Math.floor(Math.random() * (i + 1));
      [arr[i], arr[j]] = [arr[j], arr[i]];
    }
    return arr;
  }, []);
  return (
    <section className={`hero hero--${variant}`}>
      <div className="hero-photos">
        {photos.map((p, i) => (
          <Polaroid
            key={p.seed}
            photo={p}
            {...SCATTER[i]}
            gridMode={variant}
          />
        ))}
      </div>
      <div className="hero-veil" />
      <div className="hero-text">
        <div className="hero-eyebrow">
          <span className="dot" /> The only app for living more
        </div>
        <h1 className="hero-headline">
          <em>One life.</em><br />
          A thousand <em>yolos.</em>
        </h1>
        <p className="hero-sub">
          Keep a list. Make them happen. Brag a little. <br />
          Read other people&rsquo;s lists and steal the good ideas.
        </p>
        <div className="hero-ctas">
          <button className="btn btn-primary" onClick={onCTAClick}>
            Get the app
            <svg width="14" height="14" viewBox="0 0 14 14" fill="none">
              <path d="M2 7h10M8 3l4 4-4 4" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"/>
            </svg>
          </button>
          <a className="btn btn-ghost" href="#how-it-works">
            How it works
          </a>
        </div>
      </div>
    </section>
  );
}

// ============ HOW IT WORKS ============
function HowItWorks() {
  const steps = [
    {
      n: '01',
      title: 'Make a list.',
      body: 'Anything you want to do before you don\u2019t. A weekend. A decade. The next 80 years.',
      example: 'Surf in Sayulita · Read all of Calvino · Sleep in a yurt',
    },
    {
      n: '02',
      title: 'Plan it. Or don\u2019t.',
      body: 'Tap any yolo to see flights, stays, and people doing it this month. Or just sit with the list until you\u2019re ready.',
      example: 'Plan · Stay · Bring · When',
    },
    {
      n: '03',
      title: 'Cross it off. Loud.',
      body: 'Share the proof. Inspire the next person. Watch your feed fill up with their attempts at yours.',
      example: 'Done. Photo. Story. Inspired 12.',
    },
  ];

  return (
    <section id="how-it-works" className="howitworks">
      <div className="section-eyebrow">How it works</div>
      <h2 className="section-h">
        Three steps. <em>Then forever.</em>
      </h2>
      <ol className="hiw-grid">
        {steps.map(s => (
          <li className="hiw-step" key={s.n}>
            <div className="hiw-num">{s.n}</div>
            <h3 className="hiw-title">{s.title}</h3>
            <p className="hiw-body">{s.body}</p>
            <div className="hiw-example">{s.example}</div>
          </li>
        ))}
      </ol>
    </section>
  );
}

// ============ FEATURE STRIP ============
function FeatureStrip() {
  const features = [
    { kw: 'Lists', body: 'Public, private, or shared with one friend.' },
    { kw: 'Plan', body: 'Flights, stays, gear — without leaving the yolo.' },
    { kw: 'Inspired-by', body: 'Steal lovingly. Credit automatically.' },
    { kw: 'Proof', body: 'Photo, story, location. Or a single word.' },
    { kw: 'Maps', body: 'Where you\u2019ve been. Where you\u2019re going next.' },
    { kw: 'Quiet mode', body: 'Some yolos aren\u2019t for the timeline.' },
  ];
  return (
    <section className="features">
      {features.map(f => (
        <div className="feat" key={f.kw}>
          <div className="feat-kw"><em>{f.kw}</em></div>
          <div className="feat-body">{f.body}</div>
        </div>
      ))}
    </section>
  );
}

// ============ PULLQUOTE ============
function Pullquote() {
  return (
    <section className="pullquote">
      <div className="pq-mark">&ldquo;</div>
      <blockquote>
        I used to keep a list in the Notes app. Eight years. Twelve items.
        I yolo&rsquo;d <em>thirty‑four</em> things this year.
      </blockquote>
      <div className="pq-attr">— a beta tester who didn&rsquo;t want to be named</div>
    </section>
  );
}

// ============ DOWNLOAD ============
function Download({ onSubmit, submitted }) {
  const [email, setEmail] = useState('');
  const [phone, setPhone] = useState('');
  const [method, setMethod] = useState('email');

  const submit = (e) => {
    e.preventDefault();
    onSubmit({ email, phone, method });
  };

  return (
    <section id="download" className="download">
      <div className="dl-text">
        <div className="section-eyebrow">Get the app</div>
        <h2 className="section-h">
          It&rsquo;s <em>free.</em><br />
          The yolos cost whatever they cost.
        </h2>
        <p className="dl-sub">
          iOS and Android. Send yourself a download link, or grab it from the store.
        </p>

        <div className="store-row">
          <a className="store-pill" href="https://apps.apple.com/app/id6768374605" aria-label="Download on the App Store">
            <svg className="store-pill-logo" viewBox="0 0 28 32" fill="currentColor" aria-hidden="true">
              <path d="M21.94 16.96c-.04-3.93 3.21-5.81 3.35-5.9-1.83-2.67-4.67-3.04-5.68-3.08-2.42-.25-4.72 1.43-5.95 1.43-1.23 0-3.13-1.4-5.14-1.36-2.65.04-5.09 1.54-6.45 3.91-2.75 4.76-.7 11.81 1.97 15.68 1.31 1.9 2.86 4.02 4.9 3.94 1.97-.08 2.71-1.27 5.09-1.27 2.38 0 3.04 1.27 5.12 1.23 2.11-.04 3.45-1.93 4.73-3.83 1.49-2.2 2.11-4.32 2.15-4.43-.05-.02-4.12-1.58-4.16-6.27zM17.66 5.27C18.72 3.97 19.43 2.17 19.24.35c-1.55.06-3.44 1.04-4.53 2.34-.98 1.15-1.83 2.99-1.6 4.76 1.73.13 3.5-.88 4.55-2.18z"/>
            </svg>
            <div className="store-pill-text">
              <span className="store-pill-eyebrow">Download on</span>
              <span className="store-pill-name">App Store</span>
            </div>
          </a>
          <a className="store-pill" href="https://play.google.com/store/apps/details?id=io.codeop.yolo" aria-label="Get it on Google Play">
            <svg className="store-pill-logo" viewBox="0 0 28 32" aria-hidden="true">
              <path d="M3.6 1.39c-.38.4-.6 1.03-.6 1.84v25.54c0 .81.22 1.44.61 1.84l.08.08L17.04 17.4v-.34L3.69 1.31l-.09.08z" fill="#5BC9F4"/>
              <path d="M21.49 21.84l-4.45-4.45v-.34l4.45-4.45.1.06 5.27 2.99c1.51.85 1.51 2.25 0 3.11l-5.27 2.99-.1.09z" fill="#FBD000"/>
              <path d="M21.59 21.75l-4.55-4.55L3.6 30.61c.51.53 1.32.59 2.25.07l15.74-8.93" fill="#EE3D44"/>
              <path d="M21.59 12.66L5.85 3.74c-.93-.52-1.74-.47-2.25.07l13.44 13.39 4.55-4.54z" fill="#37AB68"/>
            </svg>
            <div className="store-pill-text">
              <span className="store-pill-eyebrow">Get it on</span>
              <span className="store-pill-name">Google Play</span>
            </div>
          </a>
        </div>
      </div>

      <form className="dl-form" onSubmit={submit}>
        <div className="dl-form-head">
          <strong>Or, send me a link.</strong>
          <div className="method-toggle">
            <button
              type="button"
              className={method === 'email' ? 'on' : ''}
              onClick={() => setMethod('email')}
            >Email</button>
            <button
              type="button"
              className={method === 'phone' ? 'on' : ''}
              onClick={() => setMethod('phone')}
            >SMS</button>
          </div>
        </div>

        {method === 'email' ? (
          <input
            type="email"
            required
            value={email}
            onChange={e => setEmail(e.target.value)}
            placeholder="you@somewhere.com"
            className="dl-input"
          />
        ) : (
          <input
            type="tel"
            required
            value={phone}
            onChange={e => setPhone(e.target.value)}
            placeholder="+1 (555) 000‑0000"
            className="dl-input"
          />
        )}

        <button type="submit" className="btn btn-primary dl-submit" disabled={submitted}>
          {submitted ? 'Sent. Check your messages.' : 'Send link'}
        </button>

        <div className="dl-fine">
          One link. No marketing emails. We mean it.
        </div>
      </form>
    </section>
  );
}

// ============ FOOTER ============
function Footer() {
  return (
    <footer className="ftr">
      <div className="ftr-mark">
        <span className="ftr-y">y</span><span className="ftr-dot">.</span>
      </div>
      <nav className="ftr-nav">
        <div>
          <div className="ftr-col-h">App</div>
          <a href="https://apps.apple.com/app/id6768374605">iOS</a>
          <a href="https://play.google.com/store/apps/details?id=io.codeop.yolo">Android</a>
        </div>
        <div>
          <div className="ftr-col-h">Legal</div>
          <a href="/privacy">Privacy</a>
          <a href="/terms">Terms</a>
          <a href="/community-guidelines">Community guidelines</a>
          <a href="/child-safety">Child safety</a>
          <a href="/faq">FAQ</a>
        </div>
        <div>
          <div className="ftr-col-h">Talk</div>
          <a href="mailto:hello@codeop.io">hello@codeop.io</a>
        </div>
        <div>
          <div className="ftr-col-h">Publisher</div>
          <span>Code Op (Pty) Ltd</span>
        </div>
      </nav>
      <div className="ftr-fine">
        <span>© Code Op (Pty) Ltd {new Date().getFullYear()}</span>
        <span><em>Live a little, would you?</em></span>
      </div>
    </footer>
  );
}

// ============ NAV ============
function Nav({ onDownloadClick }) {
  const [scrolled, setScrolled] = useState(false);
  useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 80);
    window.addEventListener('scroll', onScroll);
    return () => window.removeEventListener('scroll', onScroll);
  }, []);
  return (
    <header className={`nav ${scrolled ? 'nav--scrolled' : ''}`}>
      <a className="nav-mark" href="#top">
        <span className="nav-y">yolo</span><span className="nav-dot">.</span>
      </a>
      <nav className="nav-links">
        <a href="#how-it-works">How it works</a>
        <a href="#features">Features</a>
        <a href="#download">Download</a>
      </nav>
      <button className="btn btn-primary btn-sm" onClick={onDownloadClick}>
        Get the app
      </button>
    </header>
  );
}

// ============ APP ROOT ============
function App() {
  const tweaksDefaults = window.__YOLO_TWEAKS || { heroVariant: 'scatter' };
  const [tweaks] = useState(tweaksDefaults);
  const [submitted, setSubmitted] = useState(false);

  const goToDownload = () => {
    document.getElementById('download')?.scrollIntoView({ behavior: 'smooth', block: 'start' });
  };

  const onFormSubmit = (data) => {
    // TODO(send-link): POST to /v1/notify/send-link once endpoint lands.
    // For now the form is a no-op stub; "Sent" is purely a UI confirmation.
    setSubmitted(true);
    setTimeout(() => setSubmitted(false), 4000);
  };

  return (
    <div id="top" className="page">
      <Nav onDownloadClick={goToDownload} />
      <Hero variant={tweaks.heroVariant || 'scatter'} onCTAClick={goToDownload} />
      <HowItWorks />
      <FeatureStrip />
      <Pullquote />
      <Download onSubmit={onFormSubmit} submitted={submitted} />
      <Footer />
    </div>
  );
}

ReactDOM.createRoot(document.getElementById('root')).render(<App />);
