/* ============================================================
   SKI NEW ENGLAND — ski-resort decor
   Snow-drift dividers · chairlift · base-lodge scene
   Exported to window.
   ============================================================ */

/* ---------- Snow-drift divider ----------
   A scalloped snowbank edge between two sections.
   above = color of the section above (container bg)
   below = color of the section below (fill)
   pokeDown=true  → snow ledge from ABOVE hangs down into below (e.g. white over navy)
   pokeDown=false → snow from BELOW piles up into above
*/
function SnowDivider({ above, below, height = 58, lumps = 13, pokeDown = true, crest }) {
  const W = 1440, midY = height * 0.5, amp = height * 0.42, seg = W / lumps;
  const dir = pokeDown ? 1 : -1;
  let d = `M0,${midY}`;
  for (let i = 0; i < lumps; i++) {
    const xm = (i + 0.5) * seg, x1 = (i + 1) * seg;
    d += ` Q ${xm.toFixed(1)},${(midY + dir * amp).toFixed(1)} ${x1.toFixed(1)},${midY}`;
  }
  d += ` L${W},${height} L0,${height} Z`;
  // a faint crest line for snow sparkle
  let crestD = `M0,${midY}`;
  for (let i = 0; i < lumps; i++) {
    const xm = (i + 0.5) * seg, x1 = (i + 1) * seg;
    crestD += ` Q ${xm.toFixed(1)},${(midY + dir * amp).toFixed(1)} ${x1.toFixed(1)},${midY}`;
  }
  return (
    <div style={{ position: "relative", height, background: above, lineHeight: 0 }} aria-hidden="true">
      <svg viewBox={`0 0 ${W} ${height}`} preserveAspectRatio="none" width="100%" height="100%" style={{ display: "block", width: "100%", height: "100%" }}>
        <path d={d} fill={below} />
        {crest && <path d={crestD} fill="none" stroke={crest} strokeWidth="2.5" vectorEffect="non-scaling-stroke" opacity="0.5" />}
      </svg>
    </div>
  );
}

/* ---------- Chairlift ----------
   Decorative cable + towers with chairs that ride along it.
   Sits absolutely inside a positioned, dark section (behind content).
*/
function Chairlift({ color = "rgba(255,255,255,0.55)", chairTone = "rgba(255,255,255,0.7)", car = "chair" }) {
  const cableY = 26;
  const towers = [180, 720, 1260];
  const spacing = 190;
  const cycle = car === "gondola" ? 5 : 1;
  const chairXs = [];
  for (let x = -spacing; x <= 1440 + spacing * cycle; x += spacing) chairXs.push(x);
  const MARKS = ["instagram", "logo", "facebook", "tiktok", "youtube"];
  const WindowMark = ({ kind, cx, cy, tone }) => {
    if (kind === "logo") return (
      <svg x={cx - 11.5} y={cy - 7.6} width="23" height="15.2" viewBox="78 90 276 183" preserveAspectRatio="xMidYMid meet" overflow="hidden" style={{ overflow: "hidden" }}>
        <image href="uploads/SNE%20Logo%20small.png" x="0" y="0" width="430" height="371" />
      </svg>
    );
    const s = 0.6, t = `translate(${cx - 12 * s},${cy - 12 * s}) scale(${s})`;
    if (kind === "instagram") return (
      <g transform={t} fill="none" stroke={tone} strokeWidth="2.4"><rect x="3" y="3" width="18" height="18" rx="5" /><circle cx="12" cy="12" r="4" /><circle cx="17.3" cy="6.7" r="1.3" fill={tone} stroke="none" /></g>
    );
    if (kind === "facebook") return (
      <g transform={t} fill={tone}><path d="M22 12a10 10 0 1 0-11.6 9.9v-7H7.9V12h2.5V9.8c0-2.5 1.5-3.9 3.8-3.9 1.1 0 2.2.2 2.2.2v2.5h-1.3c-1.2 0-1.6.8-1.6 1.6V12h2.8l-.5 2.9h-2.3v7A10 10 0 0 0 22 12Z" /></g>
    );
    if (kind === "tiktok") return (
      <g transform={t} fill={tone}><path d="M16.5 3c.3 2.1 1.5 3.4 3.5 3.6v2.4c-1.2.1-2.4-.3-3.5-1v6.6c0 3.4-2.8 5.9-6 5.4-2.8-.4-4.6-3-4.2-5.8.4-2.5 2.7-4.3 5.2-4v2.6c-.4-.1-.8-.1-1.2 0-1.1.3-1.8 1.3-1.6 2.4.2 1.1 1.3 1.9 2.4 1.6 1-.2 1.6-1.1 1.6-2.2V3h3.3Z" /></g>
    );
    return (
      <g transform={t} fill={tone}><path d="M23 12s0-3.3-.4-4.8a2.5 2.5 0 0 0-1.8-1.8C19.3 5 12 5 12 5s-7.3 0-8.8.4A2.5 2.5 0 0 0 1.4 7.2C1 8.7 1 12 1 12s0 3.3.4 4.8a2.5 2.5 0 0 0 1.8 1.8c1.5.4 8.8.4 8.8.4s7.3 0 8.8-.4a2.5 2.5 0 0 0 1.8-1.8C23 15.3 23 12 23 12Zm-13 3V9l5.2 3-5.2 3Z" /></g>
    );
  };
  const Gondola = ({ x, i }) => (
    <g>
      {/* hanger straight down from the grip on the cable */}
      <line x1={x} y1={cableY} x2={x} y2={cableY + 13} stroke={chairTone} strokeWidth="2.2" vectorEffect="non-scaling-stroke" strokeLinecap="round" />
      <circle cx={x} cy={cableY} r="2" fill={chairTone} />
      {/* cabin */}
      <rect x={x - 16} y={cableY + 12} width="33" height="28" rx="7.5" fill={chairTone} />
      <rect x={x - 12} y={cableY + 17} width="25" height="17" rx="4" fill="rgba(255,255,255,0.9)" />
      <WindowMark kind={MARKS[((i % MARKS.length) + MARKS.length) % MARKS.length]} cx={x + 0.5} cy={cableY + 25.5} tone={chairTone} />
    </g>
  );
  const Chair = ({ x }) => (
    <g stroke={chairTone} strokeWidth="2" fill="none" vectorEffect="non-scaling-stroke" strokeLinecap="round">
      <line x1={x} y1={cableY} x2={x} y2={cableY + 22} />
      <line x1={x - 10} y1={cableY + 22} x2={x + 12} y2={cableY + 22} />
      <line x1={x + 12} y1={cableY + 22} x2={x + 12} y2={cableY + 13} />
      <line x1={x - 10} y1={cableY + 22} x2={x - 10} y2={cableY + 27} />
    </g>
  );
  return (
    <svg viewBox="0 0 1440 70" preserveAspectRatio="xMidYMin slice" width="100%" height="70"
      style={{ position: "absolute", top: 0, left: 0, overflow: "visible" }} aria-hidden="true">
      {/* cable */}
      <line x1="0" y1={cableY} x2="1440" y2={cableY} stroke={color} strokeWidth="1.5" vectorEffect="non-scaling-stroke" />
      {/* towers descending from the top edge */}
      {towers.map((x) => (
        <g key={x} stroke={color} strokeWidth="2" vectorEffect="non-scaling-stroke">
          <line x1={x} y1="-6" x2={x} y2={cableY} />
          <line x1={x - 12} y1={cableY - 4} x2={x + 12} y2={cableY - 4} />
          <circle cx={x} cy={cableY} r="2.4" fill={color} stroke="none" />
        </g>
      ))}
      {/* riding cars */}
      <g className={car === "gondola" ? "chairlift-chairs chairlift-chairs--cycle" : "chairlift-chairs"}>
        {chairXs.map((x, i) => (car === "gondola" ? <Gondola key={i} x={x} i={i} /> : <Chair key={i} x={x} />))}
      </g>
    </svg>
  );
}

/* ---------- Snow edge ---------- sits at the bottom of a dark header and
   hands off to the page below with a drifted snowline. Parent needs position:relative. */
function SnowEdge({ fill = "var(--frost-50)", height = 26, flip = false, side = "bottom" }) {
  const top = side === "top";
  const tf = [flip ? "scaleX(-1)" : "", top ? "scaleY(-1)" : ""].filter(Boolean).join(" ") || "none";
  return (
    <svg viewBox="0 0 1440 26" preserveAspectRatio="none" aria-hidden="true" style={{ position: "absolute", left: 0, right: 0, [top ? "top" : "bottom"]: -1, width: "100%", height: height, display: "block", zIndex: 3, pointerEvents: "none", transform: tf }}>
      <path d="M0,26 L1440,26 L1440,9 Q 1290,0 1104,10 Q 902,21 706,9 Q 524,-1 328,11 Q 168,20 0,7 Z" fill={fill} />
      <g fill="none" stroke={fill} strokeLinecap="round" vectorEffect="non-scaling-stroke">
        <path d="M1440,4 Q 1290,-5 1104,5 Q 902,16 706,4 Q 524,-6 328,6 Q 168,15 0,2" strokeWidth="3" strokeOpacity="0.5" strokeDasharray="210 60 150 90 300 48" />
        <path d="M1440,-1 Q 1296,-10 1110,0 Q 908,11 712,-1 Q 530,-11 334,1 Q 174,10 0,-3" strokeWidth="2" strokeOpacity="0.28" strokeDasharray="120 140 240 70 170 110" />
      </g>
    </svg>
  );
}

/* ---------- Pine tree ---------- */
function Pine({ x, y, s = 1, fill = "var(--ink-900)", snow = "#fff" }) {
  return (
    <g transform={`translate(${x},${y}) scale(${s})`}>
      <rect x="-3" y="0" width="6" height="14" fill={fill} />
      <path d="M0,-44 L18,-8 L-18,-8 Z" fill={fill} />
      <path d="M0,-44 L11,-22 L-11,-22 Z" fill={fill} />
      {/* snow dabs */}
      <path d="M0,-44 L7,-30 Q0,-26 -7,-30 Z" fill={snow} opacity="0.92" />
      <path d="M-18,-8 Q0,-15 18,-8 Q0,-4 -18,-8 Z" fill={snow} opacity="0.75" />
    </g>
  );
}

/* ---------- Footer banner ---------- flat clipart: chairlift over a snowfield */
function LodgeScene() {
  const W = 1440, H = 560;
  const metal = "#4a5c6e", metalL = "#64778c", chairFill = "#e2ebf5";

  const conifer = (x, baseY, h, w, tiers) => {
    const top = baseY - h, right = [];
    for (let t = 0; t < tiers; t++) {
      const y = top + (h * (t + 1)) / tiers;
      const hw = (w / 2) * (0.18 + 0.82 * ((t + 1) / tiers));
      right.push([x + hw, y], [x + hw * 0.55, y]);
    }
    const left = right.map(([px, py]) => [x - (px - x), py]).reverse();
    return "M" + x + "," + top + right.concat(left).map(([px, py]) => " L" + px.toFixed(1) + "," + py.toFixed(1)).join("") + " Z";
  };

  // snow-laden pine: pale shadow copy behind, white copy on top
  const Tree = ({ x, base, h, w, tiers }) => (
    <g>
      <path d={conifer(x + w * 0.1, base, h, w, tiers)} fill="#d3e1f0" />
      <path d={conifer(x, base, h, w, tiers)} fill="#ffffff" />
    </g>
  );

  // dense background treeline
  const TreeBand = ({ from, to, step, base, h, w, fill, jitter = 0 }) => {
    const out = [];
    for (let x = from, i = 0; x <= to; x += step, i++) {
      const k = 1 + ((i % 3) - 1) * 0.22;
      out.push(<path key={x} d={conifer(x, base + (i % 2) * jitter, h * k, w, 4)} fill={fill} />);
    }
    return <g>{out}</g>;
  };

  const Chair = ({ x, y, s }) => (
    <g transform={`translate(${x},${y}) scale(${s})`}>
      <path d="M0,0 L0,22 Q0,32 10,32 L22,32" fill="none" stroke={metal} strokeWidth="7" strokeLinecap="round" strokeLinejoin="round" />
      <rect x="18" y="26" width="26" height="13" rx="4" fill={metal} />
      <path d="M-52,42 L54,42 Q60,42 59,48 L48,104 Q47,110 41,110 L-45,110 Q-51,110 -52,104 L-58,48 Q-59,42 -52,42 Z" fill={chairFill} stroke={metal} strokeWidth="5" strokeLinejoin="round" />
      <line x1="-55" y1="62" x2="56" y2="62" stroke={metal} strokeWidth="4" />
      {[0, 1, 2, 3].map((i) => (
        <rect key={i} x={-54 + i * 0.8} y={112 + i * 9} width={108 - i * 2} height="5.5" rx="2.5" fill={metal} />
      ))}
    </g>
  );

  const Tower = ({ x, top, bottom, s = 1 }) => (
    <g>
      <rect x={x - 7 * s} y={top} width={14 * s} height={bottom - top} fill={metal} />
      <rect x={x - 52 * s} y={top - 2} width={104 * s} height={11 * s} rx={5 * s} fill={metal} />
      <rect x={x - 46 * s} y={top - 13 * s} width={30 * s} height={12 * s} rx={3 * s} fill={metalL} />
      <rect x={x + 16 * s} y={top - 13 * s} width={30 * s} height={12 * s} rx={3 * s} fill={metalL} />
    </g>
  );

  // four cables sweeping from the top-left to the tower head
  const head = [1010, 344];
  const starts = [[-40, -70], [-40, 10], [-40, 74], [-40, 130]];
  const at = (i, x) => {
    const [x0, y0] = starts[i];
    return y0 + ((head[1] + i * 6 - y0) * (x - x0)) / (head[0] + i * 4 - x0);
  };
  // the line carries on past the first tower, down through the second and off frame
  const contSlope = 0.545;
  const atC = (i, x) => head[1] + i * 6 + contSlope * (x - (head[0] + i * 4));

  return (
    <div aria-hidden="true" style={{ background: "var(--retro-900)", lineHeight: 0 }}>
      <svg viewBox={`0 0 ${W} ${H}`} width="100%" style={{ display: "block", width: "100%", height: "auto" }}>
        <defs>
          <linearGradient id="clip-sky" x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor="#e7eff8" />
            <stop offset="100%" stopColor="#d6e3f0" />
          </linearGradient>
        </defs>

        <rect width={W} height={H} fill="url(#clip-sky)" />

        {/* clouds */}
        <g fill="#eff5fb">
          <path d="M20,310 q26,-40 74,-26 q22,-34 70,-20 q40,-18 60,16 q34,2 40,30 L20,310 Z" />
          <path d="M300,322 q30,-36 76,-22 q26,-28 68,-12 q38,-12 52,34 L300,322 Z" />
          <path d="M840,312 q34,-40 88,-24 q28,-30 76,-14 q44,-16 62,38 L840,312 Z" />
          <path d="M1130,300 q30,-42 82,-26 q26,-32 72,-16 q46,-14 64,42 L1130,300 Z" />
        </g>

        {/* mountain ranges, far to near */}
        <path d="M0,372 L120,336 L250,370 L380,330 L470,300 L560,336 L660,366 L790,326 L920,364 L1060,330 L1200,366 L1320,338 L1440,360 L1440,420 L0,420 Z" fill="#a2b3c9" />
        <path d="M470,300 L506,324 L434,324 Z M470,300 L492,314 L478,310 L468,318 L452,312 L448,324 Z" fill="#ffffff" />
        <path d="M0,398 L150,366 L300,402 L450,364 L600,404 L760,370 L900,406 L1050,372 L1200,408 L1340,378 L1440,400 L1440,452 L0,452 Z" fill="#8c9fb8" />
        <path d="M0,424 L180,398 L360,430 L540,400 L720,434 L900,404 L1080,436 L1260,406 L1440,430 L1440,474 L0,474 Z" fill="#7a8ea9" />

        {/* dark treeline across the mid-ground */}
        <TreeBand from={430} to={1180} step={21} base={436} h={44} w={24} fill="#6a7f9b" jitter={4} />
        <TreeBand from={436} to={1190} step={27} base={446} h={54} w={28} fill="#5e7391" jitter={5} />

        {/* lift line */}
        <g stroke={metal} fill="none" strokeLinecap="round">
          {starts.map(([x0, y0], i) => (
            <line key={i} x1={x0} y1={y0} x2={head[0] + i * 4} y2={head[1] + i * 6} strokeWidth={2.6} />
          ))}
          {starts.map((_, i) => (
            <line key={`c${i}`} x1={head[0] + i * 4} y1={head[1] + i * 6} x2={1440} y2={atC(i, 1440)} strokeWidth={2} opacity={0.9} />
          ))}
        </g>

        {/* towers */}
        <Tower x={1010} top={344} bottom={462} />
        <Tower x={1142} top={416} bottom={458} s={0.52} />

        {/* chairs: two close in the upper left, the rest receding to the towers */}
        <Chair x={830} y={at(0, 830)} s={0.3} />
        <Chair x={940} y={at(1, 940)} s={0.23} />
        <Chair x={1075} y={atC(0, 1075)} s={0.17} />
        <Chair x={1148} y={atC(1, 1148)} s={0.12} />
        <Chair x={640} y={at(0, 640)} s={0.42} />
        <Chair x={250} y={at(0, 250)} s={0.78} />
        <Chair x={470} y={at(1, 470) - 44} s={1.05} />

        {/* snowfield */}
        <path d="M0,560 L0,460 Q 240,424 520,446 Q 780,466 1040,448 Q 1260,434 1440,452 L1440,560 Z" fill="#ffffff" />
        <path d="M0,492 Q 280,466 560,482 Q 840,498 1100,480 Q 1290,468 1440,478" fill="none" stroke="#eef4fb" strokeWidth="10" strokeLinecap="round" />
        <ellipse cx="1010" cy="462" rx="46" ry="9" fill="#ffffff" />
        <ellipse cx="1142" cy="458" rx="23" ry="5" fill="#ffffff" />

        {/* flocked pines, big at both ends */}
        <g>
          <Tree x={40} base={458} h={262} w={128} tiers={9} />
          <Tree x={168} base={470} h={208} w={104} tiers={8} />
          <Tree x={272} base={462} h={248} w={120} tiers={9} />
          <Tree x={382} base={472} h={186} w={94} tiers={7} />
          <Tree x={470} base={464} h={150} w={78} tiers={6} />
          <Tree x={1160} base={462} h={168} w={86} tiers={7} />
          <Tree x={1252} base={472} h={232} w={114} tiers={8} />
          <Tree x={1364} base={464} h={276} w={134} tiers={9} />
          <Tree x={1440} base={474} h={198} w={100} tiers={8} />
        </g>

        {/* snow edge dissolving into the footer blue */}
        <path d="M0,560 L0,540 Q 180,528 360,540 Q 540,552 720,540 Q 900,528 1080,540 Q 1260,552 1440,538 L1440,560 Z" fill="var(--retro-900)" />
      </svg>
    </div>
  );
}

Object.assign(window, { SnowDivider, Chairlift, Pine, LodgeScene, SnowEdge });
