// RouteMap.jsx — interactive map, redesigned for clarity + mobile-friendly
function RouteMap() {
  const { t } = window.useI18n();
  const [active, setActive] = React.useState(0);
  const days = t.route.days;

  // Per-pin positions in a 100x100 viewBox.
  // Days 0 & 6 share Mahé (Eden Island); Days 2 & 5 share Praslin.
  const pinPositions = [
    { x: 28, y: 72 },   // Day 0 — Mahé (Eden Island)
    { x: 24, y: 64 },   // Day 1 — Beau Vallon (Mahé)
    { x: 56, y: 40 },   // Day 2 — Praslin
    { x: 64, y: 30 },   // Day 3 — Curieuse / St Pierre
    { x: 73, y: 38 },   // Day 4 — La Digue
    { x: 56, y: 40 },   // Day 5 — Praslin (same as D2)
    { x: 28, y: 72 },   // Day 6 — Mahé finish (same as D0)
  ];

  const pins = days.map((d, i) => ({ ...d, ...pinPositions[i], idx: i }));

  // Group pins by location key — share a badge when at same point.
  const byKey = {};
  pins.forEach((p) => {
    const k = `${p.x}_${p.y}`;
    (byKey[k] = byKey[k] || []).push(p);
  });

  // Unique stops in chronological order (first occurrence keeps position).
  const stops = [];
  const seenKey = new Set();
  pins.forEach((p) => {
    const k = `${p.x}_${p.y}`;
    if (!seenKey.has(k)) {
      seenKey.add(k);
      stops.push({ ...p, days: byKey[k].map((q) => q.idx) });
    }
  });

  const pathFor = (slice) =>
    slice.map((p, i) => `${i === 0 ? "M" : "L"} ${p.x} ${p.y}`).join(" ");

  // Active stop = the unique stop containing the active day index.
  const activeStopIdx = stops.findIndex((s) => s.days.includes(active));

  // For the highlighted route, build the path through pins[0..active].
  const highlight = pathFor(pins.slice(0, active + 1));

  return (
    <section className="section section--navy" id="route">
      <div className="container">
        <div className="section-head">
          <div>
            <span className="eyebrow">{t.route.eyebrow}</span>
            <h2 className="section-title">{t.route.title}</h2>
          </div>
          <p className="section-sub">{t.route.sub}</p>
        </div>

        <div className="route-wrapper">
          <div className="route-map">
            <svg viewBox="0 0 100 100" preserveAspectRatio="xMidYMid meet">
              <defs>
                <pattern id="waves" x="0" y="0" width="8" height="4" patternUnits="userSpaceOnUse">
                  <path d="M0 2 Q2 0.5 4 2 T 8 2" fill="none" stroke="rgba(255,255,255,0.05)" strokeWidth="0.18" />
                </pattern>
                <radialGradient id="seaGlow" cx="50%" cy="40%" r="65%">
                  <stop offset="0%" stopColor="rgba(201,169,75,0.05)" />
                  <stop offset="100%" stopColor="rgba(201,169,75,0)" />
                </radialGradient>
              </defs>
              <rect width="100" height="100" fill="url(#waves)" />
              <rect width="100" height="100" fill="url(#seaGlow)" />

              {/* compass — top left */}
              <g transform="translate(8 10)" opacity="0.55">
                <circle r="3.6" fill="none" stroke="rgba(255,255,255,0.25)" strokeWidth="0.18" />
                <circle r="0.4" fill="rgba(201,169,75,0.7)" />
                <path d="M0 -3.6 L0.5 0 L0 3.6 L-0.5 0 Z" fill="rgba(201,169,75,0.55)" />
                <text x="0" y="-4.6" textAnchor="middle" fontSize="1.8" fill="rgba(255,255,255,0.55)" fontFamily="JetBrains Mono">N</text>
              </g>

              {/* scale bar — bottom right */}
              <g transform="translate(72 92)" opacity="0.45">
                <line x1="0" y1="0" x2="20" y2="0" stroke="rgba(255,255,255,0.4)" strokeWidth="0.25" />
                <line x1="0" y1="-1" x2="0" y2="1" stroke="rgba(255,255,255,0.4)" strokeWidth="0.25" />
                <line x1="20" y1="-1" x2="20" y2="1" stroke="rgba(255,255,255,0.4)" strokeWidth="0.25" />
                <text x="10" y="-1.6" textAnchor="middle" fontSize="1.6" fill="rgba(255,255,255,0.55)" fontFamily="JetBrains Mono">~30 NM</text>
              </g>

              {/* MAHÉ */}
              <path d="M22 60 Q25 56 30 58 Q34 60 35 66 Q36 72 33 78 Q30 84 26 82 Q21 79 20 73 Q19 66 22 60 Z"
                fill="rgba(247,244,236,0.10)" stroke="rgba(247,244,236,0.28)" strokeWidth="0.25" />
              <text x="27" y="89" textAnchor="middle" fontSize="2" fill="rgba(255,255,255,0.6)"
                fontFamily="JetBrains Mono" letterSpacing="0.3">MAHÉ</text>

              {/* PRASLIN */}
              <path d="M52 36 Q56 32 62 35 Q66 38 63 43 Q58 45 53 42 Z"
                fill="rgba(247,244,236,0.10)" stroke="rgba(247,244,236,0.28)" strokeWidth="0.25" />
              <text x="57" y="50" textAnchor="middle" fontSize="1.8" fill="rgba(255,255,255,0.55)"
                fontFamily="JetBrains Mono" letterSpacing="0.3">PRASLIN</text>

              {/* CURIEUSE */}
              <path d="M62 25 Q66 23 68 26 Q67 29 63 28 Z"
                fill="rgba(247,244,236,0.10)" stroke="rgba(247,244,236,0.28)" strokeWidth="0.25" />
              <text x="65" y="21.5" textAnchor="middle" fontSize="1.5" fill="rgba(255,255,255,0.5)"
                fontFamily="JetBrains Mono" letterSpacing="0.3">CURIEUSE</text>

              {/* LA DIGUE */}
              <path d="M72 35 Q75 33 76 37 Q75 41 72 40 Z"
                fill="rgba(247,244,236,0.10)" stroke="rgba(247,244,236,0.28)" strokeWidth="0.25" />
              <text x="78" y="42" textAnchor="start" fontSize="1.5" fill="rgba(255,255,255,0.55)"
                fontFamily="JetBrains Mono" letterSpacing="0.3">LA DIGUE</text>

              {/* dashed full route */}
              <path d={pathFor(pins)} fill="none" stroke="rgba(201,169,75,0.32)"
                strokeWidth="0.4" strokeDasharray="1 0.8" strokeLinecap="round" />

              {/* highlighted segment up to active */}
              <path d={highlight} fill="none" stroke="var(--gold)" strokeWidth="0.6" strokeLinecap="round" />

              {/* Stops — one badge per unique location, lists all day numbers */}
              {stops.map((s, i) => {
                const isActive = activeStopIdx === i;
                const labelText = s.days.length > 1
                  ? "D" + s.days.join("·")
                  : "D" + s.days[0];
                const w = Math.max(3.6, 2.2 + labelText.length * 0.8);

                // Smart label offset to avoid island silhouettes
                let dx = 0, dy = 4.2;
                if (s.x < 30 && s.y > 65) { dx = 4.5; dy = 0.4; }       // Mahé pins → label to the right
                if (s.x > 70) { dx = -4.5; dy = 0.4; }                   // La Digue → label to the left
                if (s.y < 32 && s.x > 60) { dx = 0; dy = -3.5; }         // Curieuse → label above

                return (
                  <g
                    key={i}
                    className={`route-pin ${isActive ? "active" : ""}`}
                    onClick={() => setActive(s.days[0])}
                    onMouseEnter={() => setActive(s.days[0])}
                    style={{ cursor: "pointer" }}
                  >
                    <circle cx={s.x} cy={s.y} r="3" fill={isActive ? "rgba(201,169,75,0.18)" : "rgba(201,169,75,0)"} />
                    <circle cx={s.x} cy={s.y} r={isActive ? 1.2 : 0.85}
                      fill={isActive ? "#c9a94b" : "#ffffff"}
                      stroke="#0b3a68" strokeWidth="0.25" />
                    <g transform={`translate(${s.x + dx} ${s.y + dy})`}>
                      <rect x={-w / 2} y="-1.3" width={w} height="2.6" rx="0.3"
                        fill={isActive ? "#c9a94b" : "rgba(11,29,54,0.85)"}
                        stroke={isActive ? "#c9a94b" : "rgba(255,255,255,0.25)"} strokeWidth="0.15" />
                      <text x="0" y="0.55" textAnchor="middle" fontSize="1.5"
                        fill={isActive ? "#0b3a68" : "rgba(255,255,255,0.85)"}
                        fontFamily="JetBrains Mono" fontWeight="500" letterSpacing="0.05">
                        {labelText}
                      </text>
                    </g>
                  </g>
                );
              })}
            </svg>
          </div>

          <div className="route-timeline">
            {days.map((d, i) => (
              <div
                key={i}
                className={`route-day ${active === i ? "active" : ""}`}
                onClick={() => setActive(i)}
                onMouseEnter={() => setActive(i)}
              >
                <div className="route-day__date">
                  {d.day}<small>{d.date}</small>
                </div>
                <div className="route-day__content">
                  <h4>{d.title}</h4>
                  <div className="route-day__loc">{d.loc}</div>
                  <div className="route-day__desc">{d.desc}</div>
                </div>
              </div>
            ))}
          </div>
        </div>
      </div>
    </section>
  );
}
window.RouteMap = RouteMap;
