// Into the Wild: edition pages

const PROGRAMME_04 = [
  {
    n: "01", title: "Arrivals & Circle", loc: "Pontedeume · Teambuilding",
    phase: "I · Teambuilding",
    tags: ["Arrivals", "Icebreakers", "Kit check"],
    body: "Twenty strangers pile off planes, trains and a rickety bus from Santiago. We meet at a rural albergue on the edge of the forest, share a stew, hand out headlamps and walk a short opening ritual down to the estuary.",
    stats: [{k:"Distance", v:"0 km"}, {k:"Sleep", v:"Das Nest"}, {k:"Dinner", v:"Caldo galego"}]
  },
  {
    n: "02", title: "Names, Knots, Nerves", loc: "Caaveiro basecamp · Teambuilding",
    phase: "I · Teambuilding",
    tags: ["Cooperative games", "Roles"],
    body: "A full day of non-formal facilitation. We work through name-learning games, trust exercises, cooking rotations, and the contract the group will live by for the rest of the week. Everyone leaves with a task and a partner.",
    stats: [{k:"Sessions", v:"4"}, {k:"Groups of", v:"3"}, {k:"Sleep", v:"Albergue"}]
  },
  {
    n: "03", title: "Into the Fragas", loc: "River Eume · Teambuilding",
    phase: "I · Teambuilding",
    tags: ["First hike", "Peer learning"],
    body: "Our first proper walk into the forest canopy. Skill-share in clearings — map-reading, fire-lighting, conflict transformation, singing badly in six languages. Return to the albergue for a cultural-exchange dinner: one dish per country.",
    stats: [{k:"Distance", v:"9 km"}, {k:"Workshops", v:"3"}, {k:"Plates", v:"54"}]
  },
  {
    n: "04", title: "Deep Forest", loc: "Caaveiro → Monfero · Experiential",
    phase: "II · Experiential",
    tags: ["Bivouac", "Navigation"],
    body: "Phase two begins. We leave the albergue with everything on our backs and cross the Eume valley on foot. You take turns leading with the map. First night out — tarps and hammocks under three-hundred-year-old oaks.",
    stats: [{k:"Distance", v:"16 km"}, {k:"Ascent", v:"+520m"}, {k:"Sleep", v:"Bivouac"}]
  },
  {
    n: "05", title: "Silent Morning, Loud Night", loc: "Monfero waterfalls · Experiential",
    phase: "II · Experiential",
    tags: ["Solo time", "Wild swim", "Campfire"],
    body: "Four hours walking without speaking — harder than it sounds. A cold swim under the Cabalar falls. Ends in a circle around the fire where everyone shares one thing the silence said to them. Some cry. It's fine.",
    stats: [{k:"Distance", v:"12 km"}, {k:"Words", v:"~0"}, {k:"Songs", v:"∞"}]
  },
  {
    n: "06", title: "Descent & Debrief", loc: "Back to Pontedeume · Reflection",
    phase: "III · Reflection",
    tags: ["Youthpass", "Closing"],
    body: "Off the ridge, into a borrowed farmhouse. We run Youthpass reflection workshops, swap contacts, plan the winter reunion, cry again, then have the best dinner of our lives. Goodbyes at the train station the next morning.",
    stats: [{k:"Distance", v:"8 km"}, {k:"Youthpass", v:"Signed"}, {k:"Goodbyes", v:"Tearful"}]
  },
];


const COUNTRIES_04 = [
  {
    code: "ES",
    name: "Spain",
    sub: "Lead · Xeracion",
    flag: (
      <svg viewBox="0 0 6 4" preserveAspectRatio="none" aria-hidden="true">
        <rect width="6" height="4" fill="#AA151B" />
        <rect y="1" width="6" height="2" fill="#F1BF00" />
      </svg>
    )
  },
  {
    code: "IT",
    name: "Italy",
    sub: "Partner",
    flag: (
      <svg viewBox="0 0 3 2" preserveAspectRatio="none" aria-hidden="true">
        <rect width="1" height="2" x="0" fill="#009246" />
        <rect width="1" height="2" x="1" fill="#F1F2F1" />
        <rect width="1" height="2" x="2" fill="#CE2B37" />
      </svg>
    )
  },
  {
    code: "PT",
    name: "Portugal",
    sub: "Partner",
    flag: (
      <svg viewBox="0 0 5 3" preserveAspectRatio="none" aria-hidden="true">
        <rect width="2" height="3" fill="#006600" />
        <rect width="3" height="3" x="2" fill="#FF0000" />
        <circle cx="2" cy="1.5" r="0.55" fill="#FFCC29" stroke="#000" strokeWidth="0.03" />
        <circle cx="2" cy="1.5" r="0.32" fill="#FFFFFF" stroke="#000" strokeWidth="0.025" />
      </svg>
    )
  },
  {
    code: "FR",
    name: "France",
    sub: "Partner",
    flag: (
      <svg viewBox="0 0 3 2" preserveAspectRatio="none" aria-hidden="true">
        <rect width="1" height="2" x="0" fill="#0055A4" />
        <rect width="1" height="2" x="1" fill="#FFFFFF" />
        <rect width="1" height="2" x="2" fill="#EF4135" />
      </svg>
    )
  },
  {
    code: "GR",
    name: "Greece",
    sub: "Partner",
    flag: (
      <svg viewBox="0 0 27 18" preserveAspectRatio="none" aria-hidden="true">
        <rect width="27" height="18" fill="#0D5EAF" />
        {[1,3,5,7].map((i) => (
          <rect key={i} y={i*2} width="27" height="2" fill="#FFFFFF" />
        ))}
        <rect x="0" y="0" width="10" height="10" fill="#0D5EAF" />
        <rect x="4" y="0" width="2" height="10" fill="#FFFFFF" />
        <rect x="0" y="4" width="10" height="2" fill="#FFFFFF" />
      </svg>
    )
  }
];


function RouteMap() {
  const [hover, setHover] = React.useState(null);
  const pins = [
    { n: 1, name: "Pontedeume", code: "Start · Estuary", x: 140, y: 470 },
    { n: 2, name: "Albergue Caaveiro", code: "Basecamp · Days 1–3", x: 280, y: 380 },
    { n: 3, name: "Monastery of Caaveiro", code: "Day 3 · Riverbend", x: 420, y: 320 },
    { n: 4, name: "Cabalar Falls", code: "Day 5 · Wild swim", x: 560, y: 230 },
    { n: 5, name: "Monfero ridge", code: "Day 4–5 · Bivouac", x: 680, y: 180 },
    { n: 6, name: "Farmhouse A Capela", code: "Day 6 · Reflection", x: 780, y: 350 },
  ];

  const path = "M 140 470 Q 200 430 280 380 Q 360 340 420 320 Q 480 280 560 230 Q 620 200 680 180 Q 740 250 780 350";

  return (
    <section className="map-section" id="route">
      <div className="map-head">
        <div>
          <h2 className="section-title" style={{color:'var(--cream)'}}>
            The <em>Fragas</em>
            <br/>do <span style={{WebkitTextStroke:'1.5px var(--cream)', color:'transparent', fontStyle:'normal'}}>Eume.</span>
          </h2>
        </div>
      </div>

      <div className="map-wrap">
        <svg className="map-svg" viewBox="0 0 900 600" preserveAspectRatio="xMidYMid meet">
          {/* Grid */}
          <defs>
            <pattern id="grid" width="40" height="40" patternUnits="userSpaceOnUse">
              <path d="M 40 0 L 0 0 0 40" fill="none" stroke="#1A1F16" strokeWidth="0.4" opacity="0.12"/>
            </pattern>
            <pattern id="topo" width="120" height="120" patternUnits="userSpaceOnUse">
              <circle cx="60" cy="60" r="12" fill="none" stroke="#2D4A2B" strokeWidth="0.6" opacity="0.25"/>
              <circle cx="60" cy="60" r="28" fill="none" stroke="#2D4A2B" strokeWidth="0.6" opacity="0.2"/>
              <circle cx="60" cy="60" r="45" fill="none" stroke="#2D4A2B" strokeWidth="0.6" opacity="0.15"/>
            </pattern>
          </defs>
          <rect width="900" height="600" fill="url(#grid)"/>
          <rect width="900" height="600" fill="url(#topo)"/>

          {/* River */}
          <path d="M 0 350 Q 150 330 300 360 T 600 340 Q 750 330 900 360"
                fill="none" stroke="#7FA9B8" strokeWidth="4" opacity="0.5"/>
          <path d="M 400 0 Q 420 150 380 280 Q 360 400 340 550 L 340 600"
                fill="none" stroke="#7FA9B8" strokeWidth="3" opacity="0.4"/>

          {/* Forest icons */}
          {[[80,120],[130,260],[50,420],[160,520],[420,80],[580,150],[780,100],[800,260],[860,480],[460,510],[240,480],[520,240]].map(([x,y], i) =>
            <g key={i} transform={`translate(${x} ${y})`}>
              <path d="M 0 -12 L 10 8 L -10 8 Z" fill="#2D4A2B" opacity="0.4"/>
              <rect x="-2" y="6" width="4" height="6" fill="#2D4A2B" opacity="0.4"/>
            </g>
          )}

          {/* Compass */}
          <g transform="translate(840 80)" opacity="0.6">
            <circle r="28" fill="none" stroke="#1A1F16" strokeWidth="1"/>
            <path d="M 0 -24 L 4 0 L 0 24 L -4 0 Z" fill="#C44A1B"/>
            <text y="-32" textAnchor="middle" fontFamily="'JetBrains Mono', monospace" fontSize="10" fill="#1A1F16">N</text>
          </g>

          {/* Route */}
          <path d={path} fill="none" stroke="#C44A1B" strokeWidth="2.5" strokeDasharray="6 5"/>
          <path d={path} fill="none" stroke="#C44A1B" strokeWidth="6" opacity="0.15"/>

          {/* Pins */}
          {pins.map((p) => (
            <g key={p.n} transform={`translate(${p.x} ${p.y})`}
               onMouseEnter={() => setHover(p.n)}
               onMouseLeave={() => setHover(null)}
               style={{cursor:'pointer'}}>
              <circle r={hover === p.n ? 16 : 12} fill="#EFE7D6" stroke="#1A1F16" strokeWidth="2"/>
              <circle r={hover === p.n ? 7 : 5} fill="#C44A1B"/>
              <text y="-22" textAnchor="middle"
                    fontFamily="'Fraunces', serif"
                    fontStyle="italic"
                    fontWeight="700"
                    fontSize={hover === p.n ? 20 : 16}
                    fill="#1A1F16">
                {p.name}
              </text>
              <text y="-6" textAnchor="middle"
                    fontFamily="'JetBrains Mono', monospace"
                    fontSize="9"
                    letterSpacing="1"
                    fill="#1A1F16" opacity="0.6">
                {p.code.toUpperCase()}
              </text>
            </g>
          ))}
        </svg>

        <ul className="map-pins-list">
          {pins.map(p => (
            <li key={p.n}
                className={hover === p.n ? 'active' : ''}
                onMouseEnter={() => setHover(p.n)}
                onMouseLeave={() => setHover(null)}>
              <span className="pin-dot"/>
              <span className="pin-num">{String(p.n).padStart(2,'0')}</span>
              <span>{p.name} · {p.code}</span>
            </li>
          ))}
        </ul>
      </div>
    </section>
  );
}


function EditionHero({ e }) {
  return (
    <section className="hero hero-edition" id="top">
      <div className="hero-bg" />
      <div className="hero-topo" />
      <h1 className="hero-headline hero-headline-edition">
        <span className="line">Into the Wild</span>
        <span className="line">
          <span className="marker"><span>{e.roman}</span></span>
          <span className="outline">{e.year}</span>
        </span>
      </h1>

      <div className="hero-bottom">
        <p className="hero-blurb">{e.lead}</p>
        <div className="hero-meta">
          <div className="meta-block">
            <span className="meta-label">When</span>
            <span className="meta-val">{e.dates}</span>
          </div>
          <div className="meta-block">
            <span className="meta-label">Where</span>
            <span className="meta-val">{e.place}</span>
          </div>
        </div>
      </div>
    </section>);

}

function EditionSwitcher({ current }) {
  return (
    <nav className="edition-switch" aria-label="Editions">
      {EDITIONS.map((e) =>
      <a
        key={e.n}
        href={editionUrl(e.n)}
        className={e.n === current ? 'active' : ''}
        aria-current={e.n === current ? 'page' : undefined}>
          <span className="sw-num">{e.roman}</span>
          <span className="sw-year">{e.year}</span>
        </a>
      )}
      <a href="/#next" className="sw-next">
        <span className="sw-num">V</span>
        <span className="sw-year">2027 ✶</span>
      </a>
    </nav>);

}

function EditionStory({ e }) {
  return (
    <section className="section manifesto" id="story">
      <div className="wrap">
        <h2 className="section-title">
          {e.name}<br /><em>{e.sub.toLowerCase()}.</em>
        </h2>
        <div className="manifesto-grid">
          <div>
            <dl className="known-list">
              {e.how.map((h) =>
              <div key={h.k} className="known-item">
                  <dt>{h.k}</dt>
                  <dd>{h.v}</dd>
                </div>
              )}
            </dl>
            <div className="edition-tags" style={{ marginTop: 24 }}>
              {e.card.tags.map((t) => <span key={t} className="chip-dark">{t}</span>)}
            </div>
          </div>
          <div className="manifesto-body">
            {e.story.map((p, i) => <p key={i}>{p}</p>)}
            {e.infopack &&
            <p><a className="text-link" href={e.infopack} target="_blank" rel="noopener">Read the edition {e.roman} infopack ↗</a></p>
            }
            <div className="manifesto-sig">The Xeracion team</div>
          </div>
        </div>
      </div>
    </section>);

}

function EditionStats({ stats }) {
  return (
    <div className="stats">
      {stats.map((s, i) =>
      <div key={i}>
          <div className="stat-val">{s.val}<span className="unit">{s.unit}</span></div>
          <div className="stat-label">{s.label}</div>
        </div>
      )}
    </div>);

}

function Programme() {
  const [open, setOpen] = React.useState(0);
  const toggle = (i) => setOpen(open === i ? -1 : i);
  return (
    <section className="section itinerary" id="programme">
      <div className="wrap">
        <h2 className="section-title">
          Three <em>phases.</em>
          <br />Six days in <span className="outline">one forest.</span>
        </h2>
        <div className="phase-legend">
          <span><b>I</b> Teambuilding <i>(days 1 to 3)</i></span>
          <span><b>II</b> Experiential adventure <i>(days 4 and 5)</i></span>
          <span><b>III</b> Reflection <i>(day 6)</i></span>
        </div>
        <div className="itinerary-list">
          {PROGRAMME_04.map((d, i) =>
          <div key={d.n} className={`day-row ${open === i ? 'open' : ''}`}>
              <button
              type="button"
              className="day-head"
              aria-expanded={open === i}
              onClick={() => toggle(i)}>
                <span className="day-num">{d.n}</span>
                <span>
                  <span className="day-title">{d.title}</span>
                  <span className="day-loc">{d.loc}</span>
                </span>
                <span className="day-tags">
                  {d.tags.map((t) => <span key={t} className="chip">{t}</span>)}
                </span>
                <span className="day-chev" aria-hidden="true">→</span>
              </button>
              {open === i &&
            <div className="day-expand">
                  <p>{d.body}</p>
                  <div className="mini-stat">
                    {d.stats.map((s) => <span key={s.k}>{s.k}<strong>{s.v}</strong></span>)}
                  </div>
                </div>
            }
            </div>
          )}
        </div>
      </div>
    </section>);

}

function Partners() {
  return (
    <section className="section countries" id="partners">
      <div className="wrap">
        <h2 className="section-title">
          Five <em>flags,</em>
          <br />one <span className="outline">handshake.</span>
        </h2>
        <div className="countries-grid">
          {COUNTRIES_04.map((c, i) =>
          <div key={c.code} className="country-cell">
              <div className="country-flag">{c.flag}</div>
              <div className="country-meta">
                <span className="country-num">0{i + 1}</span>
                <div>
                  <div className="country-name">{c.name}</div>
                  <div className="country-sub">{c.code} · {c.sub}</div>
                </div>
              </div>
            </div>
          )}
        </div>
      </div>
    </section>);

}

function NextEditionCta() {
  return (
    <section className="section apply">
      <h2 className="big-title">Your <em>turn?</em></h2>
      <p className="apply-sub">
        Edition V is a survival challenge in {NEXT_EDITION.place}, {NEXT_EDITION.when}.
        Leave your email and you will be the first to know when applications open.
      </p>
      <a href="/#notify" className="apply-btn">Get notified <span className="arrow">→</span></a>
    </section>);

}

function PrevNext({ e }) {
  const i = EDITIONS.findIndex((x) => x.n === e.n);
  const prev = EDITIONS[i - 1];
  const next = EDITIONS[i + 1];
  return (
    <nav className="prevnext" aria-label="More editions">
      {prev ?
      <a href={editionUrl(prev.n)} className="pn-prev">
          <span className="pn-label">← Previous</span>
          <span className="pn-name">{prev.name} · {prev.year}</span>
        </a> :
      <span />}
      {next ?
      <a href={editionUrl(next.n)} className="pn-next">
          <span className="pn-label">Next →</span>
          <span className="pn-name">{next.name} · {next.year}</span>
        </a> :
      <a href="/#next" className="pn-next">
          <span className="pn-label">Next →</span>
          <span className="pn-name">Into the Wild V · 2027</span>
        </a>}
    </nav>);

}

function EditionPage({ id }) {
  const e = EDITIONS.find((x) => x.n === id);
  if (!e) return <HomePage />;
  const quotes = TESTIMONIALS.filter((t) => t.edition === e.n);
  return (
    <>
      <Nav />
      <main>
        <EditionHero e={e} />
        <EditionSwitcher current={e.n} />
        <EditionStory e={e} />
        <EditionStats stats={e.stats} />
        {e.hasProgramme && <Programme />}
        {e.hasProgramme && <RouteMap />}
        {e.hasProgramme && <Partners />}
        {quotes.length > 0 &&
        <Testimonials
          id="voices"
          title={<>In their <em>words.</em></>}
          quotes={quotes} />
        }
        <PrevNext e={e} />
        <NextEditionCta />
      </main>
      <Footer />
    </>);

}

Object.assign(window, { EditionPage });
