/* Carelon Passport — map / globe visuals (brand spectrum) */

const GlobeViz = () => {
  // pin coords in a 420x420 viewBox
  const A = { x: 150, y: 252 };   // Atlanta (home)
  const L = { x: 252, y: 150 };   // London (next)
  const D = { x: 322, y: 214 };   // Dubai (upcoming)
  const arc = (p, q, lift) => `M ${p.x} ${p.y} Q ${(p.x + q.x) / 2} ${(p.y + q.y) / 2 - lift} ${q.x} ${q.y}`;
  const pct = (p) => ({ left: (p.x / 420) * 100 + "%", top: (p.y / 420) * 100 + "%" });

  const meridians = [];
  for (let i = 1; i <= 5; i++) {
    const rx = 150 * Math.cos((i / 6) * Math.PI);
    meridians.push(Math.abs(rx));
  }

  return (
    <div style={{ position: "absolute", inset: 0, overflow: "hidden" }}>
      <svg viewBox="0 0 420 420" width="100%" height="100%" preserveAspectRatio="xMidYMid meet" style={{ position: "absolute", inset: 0 }}>
        <defs>
          <radialGradient id="globeFill" cx="38%" cy="34%" r="75%">
            <stop offset="0%" stopColor="#F6F1FF" />
            <stop offset="55%" stopColor="#EFE9FE" />
            <stop offset="100%" stopColor="#E6F6F6" />
          </radialGradient>
          <linearGradient id="routeGrad" x1="0" y1="0" x2="1" y2="1">
            <stop offset="0%" stopColor="#5009B5" />
            <stop offset="55%" stopColor="#794CFF" />
            <stop offset="100%" stopColor="#00BBBA" />
          </linearGradient>
          <radialGradient id="globeGlow" cx="50%" cy="50%" r="50%">
            <stop offset="60%" stopColor="rgba(121,76,255,0)" />
            <stop offset="100%" stopColor="rgba(121,76,255,.12)" />
          </radialGradient>
          <filter id="soft" x="-40%" y="-40%" width="180%" height="180%">
            <feGaussianBlur stdDeviation="6" />
          </filter>
        </defs>

        {/* ambient glow orb */}
        <circle cx="210" cy="205" r="190" fill="url(#globeGlow)" />
        {/* sphere */}
        <circle cx="210" cy="205" r="150" fill="url(#globeFill)" stroke="#E3DBF6" strokeWidth="1" />
        {/* parallels */}
        <g stroke="#CDBDF0" strokeWidth="1" fill="none" opacity="0.85">
          {[-110, -58, 0, 58, 110].map((dy, i) => (
            <ellipse key={i} cx="210" cy="205" rx={Math.sqrt(Math.max(0, 150 * 150 - dy * dy))} ry={14 - Math.abs(dy) * 0.03} transform={`translate(0 ${dy})`} />
          ))}
        </g>
        {/* meridians */}
        <g stroke="#CDBDF0" strokeWidth="1" fill="none" opacity="0.7">
          {meridians.map((rx, i) => <ellipse key={i} cx="210" cy="205" rx={rx} ry="150" />)}
          <line x1="210" y1="55" x2="210" y2="355" />
        </g>
        {/* highlight */}
        <ellipse cx="168" cy="150" rx="60" ry="42" fill="#FFFFFF" opacity="0.5" filter="url(#soft)" />

        {/* routes */}
        <path d={arc(A, L, 46)} fill="none" stroke="url(#routeGrad)" strokeWidth="2.4" strokeLinecap="round" strokeDasharray="2 7" className="cp-route" />
        <path d={arc(L, D, 30)} fill="none" stroke="#9F86E8" strokeWidth="2" strokeLinecap="round" strokeDasharray="2 7" opacity="0.8" />

        {/* pins */}
        {[{ p: A, c: "#5009B5" }, { p: L, c: "#794CFF" }, { p: D, c: "#00BBBA" }].map((m, i) => (
          <g key={i}>
            <circle cx={m.p.x} cy={m.p.y} r="11" fill={m.c} opacity="0.14" className="cp-pin-halo" />
            <circle cx={m.p.x} cy={m.p.y} r="5.5" fill="#fff" />
            <circle cx={m.p.x} cy={m.p.y} r="3.6" fill={m.c} />
          </g>
        ))}
      </svg>

      {/* labels */}
      <FloatLabel style={pct(L)} title="London, UK" sub="Next destination" tone="violet" offset={[14, -10]} />
      <FloatLabel style={pct(A)} title="Atlanta, GA" sub="Home" tone="purple" offset={[-34, 16]} />
      <FloatLabel style={pct(D)} title="Dubai, UAE" sub="Upcoming" tone="teal" offset={[16, 8]} />
    </div>
  );
};

const FloatLabel = ({ style, title, sub, tone = "purple", offset = [0, 0] }) => {
  const t = TONES[tone];
  return (
    <div className="cp-floatlabel" style={{
      position: "absolute", left: style.left, top: style.top,
      transform: `translate(${offset[0]}px, ${offset[1]}px)`,
      display: "flex", alignItems: "center", gap: 8, padding: "7px 11px 7px 8px",
      background: "rgba(255,255,255,.92)", backdropFilter: "blur(6px)", borderRadius: 12,
      border: "1px solid var(--line)", boxShadow: "0 6px 18px -8px rgba(28,21,48,.28)", whiteSpace: "nowrap",
    }}>
      <span style={{ width: 24, height: 24, borderRadius: 7, background: t.bg, color: t.fg, display: "flex", alignItems: "center", justifyContent: "center" }}>
        <Icon name="pin" size={14} stroke={2} />
      </span>
      <div>
        <div style={{ fontSize: 12.5, fontWeight: 700, color: "var(--ink)", lineHeight: 1.15 }}>{title}</div>
        <div style={{ fontSize: 10.5, fontWeight: 600, color: t.fg, letterSpacing: ".03em", textTransform: "uppercase" }}>{sub}</div>
      </div>
    </div>
  );
};

/* Stylized London street/river map for the provider module (wide banner) */
const CityMap = ({ active = 0, onPin }) => {
  const pins = [
    { x: 168, y: 96 }, { x: 388, y: 80 }, { x: 588, y: 104 },
  ];
  return (
    <svg viewBox="0 0 760 150" width="100%" height="100%" preserveAspectRatio="xMidYMid slice" style={{ display: "block" }}>
      <defs>
        <linearGradient id="cityBg" x1="0" y1="0" x2="1" y2="1">
          <stop offset="0%" stopColor="#F4F0FD" />
          <stop offset="100%" stopColor="#EAF6F6" />
        </linearGradient>
      </defs>
      <rect width="760" height="150" fill="url(#cityBg)" />
      {/* blocks */}
      <g fill="#fff" opacity="0.55">
        {[[40,20],[140,16],[250,30],[360,18],[470,26],[560,16],[660,30],[60,96],[230,104],[430,100],[640,96],[300,60],[510,64]].map(([x,y],i)=>(
          <rect key={i} x={x} y={y} width={46 + (i%3)*16} height={26 + (i%2)*10} rx="6" />
        ))}
      </g>
      {/* streets */}
      <g stroke="#DCD2F2" strokeWidth="7" strokeLinecap="round" opacity="0.85">
        <line x1="-10" y1="52" x2="780" y2="60" />
        <line x1="-10" y1="118" x2="780" y2="110" />
        <line x1="120" y1="-10" x2="100" y2="170" />
        <line x1="320" y1="-10" x2="338" y2="170" />
        <line x1="540" y1="-10" x2="524" y2="170" />
      </g>
      {/* river */}
      <path d="M-10 138 C 120 120, 240 156, 400 134 S 660 110, 780 132" fill="none" stroke="#BFE3F6" strokeWidth="18" strokeLinecap="round" opacity="0.8" />
      {/* pins */}
      {pins.map((p, i) => {
        const on = i === active;
        return (
          <g key={i} style={{ cursor: "pointer" }} onClick={() => onPin && onPin(i)}>
            {on && <circle cx={p.x} cy={p.y} r="18" fill="#5009B5" opacity="0.12" className="cp-pin-halo" />}
            <g transform={`translate(${p.x} ${p.y})`}>
              <path d="M0 6 C -9 6 -15 -1 -15 -9 A 15 15 0 1 1 15 -9 C 15 -1 9 6 0 6 Z" transform="translate(0 -6)" fill={on ? "#5009B5" : "#fff"} stroke={on ? "#5009B5" : "#C9BCEC"} strokeWidth="1.5" />
              <text x="0" y="-13.5" textAnchor="middle" fontSize="13" fontWeight="700" fill={on ? "#fff" : "#5009B5"} fontFamily="inherit">{i + 1}</text>
            </g>
          </g>
        );
      })}
    </svg>
  );
};

Object.assign(window, { GlobeViz, CityMap });
