// radial-orbital-services.jsx — services as an orbiting timeline of nodes.
// Adapted from radial-orbital-timeline. Center hub + 7 service nodes orbiting,
// auto-rotates, click a node to open its detail card. No deps — inline SVG icons.

/* ----- Service icon set ------------------------------------------------- */
const SVC_ICONS = {
  smm:
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round" width="18" height="18">
      <circle cx="12" cy="12" r="9" />
      <circle cx="12" cy="12" r="5" />
      <circle cx="12" cy="12" r="1.5" fill="currentColor" />
    </svg>,

  content:
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round" width="18" height="18">
      <path d="M4 19V5a2 2 0 0 1 2-2h11l3 3v13a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2z" />
      <path d="M8 8h8M8 12h8M8 16h5" />
    </svg>,

  production:
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round" width="18" height="18">
      <rect x="2" y="6" width="14" height="12" rx="2" />
      <path d="m22 8-6 4 6 4z" />
    </svg>,

  ads:
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round" width="18" height="18">
      <path d="M22 12A10 10 0 1 1 12 2" />
      <path d="M22 2 12 12" />
      <path d="M22 8V2h-6" />
    </svg>,

  visual:
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round" width="18" height="18">
      <circle cx="12" cy="12" r="9" />
      <circle cx="7" cy="10" r="1.5" fill="currentColor" />
      <circle cx="12" cy="7" r="1.5" fill="currentColor" />
      <circle cx="17" cy="10" r="1.5" fill="currentColor" />
      <path d="M9 18c2 0 5 -1 5 -4 0 -1 -1 -2 -3 -2 -1 0 -2 .5 -2 2 0 1.5 1 2.5 2.5 3" />
    </svg>,

  brand:
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round" width="18" height="18">
      <path d="m20 7-8-4-8 4v10l8 4 8-4z" />
      <path d="M4 7l8 4 8-4M12 11v10" />
    </svg>,

  sales:
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round" width="18" height="18">
      <path d="m3 17 6-6 4 4 8-8" />
      <path d="M14 7h7v7" />
    </svg>

};

/* ----- Component -------------------------------------------------------- */
function RadialOrbitalServices({ services, compact }) {
  const [angle, setAngle] = React.useState(0);
  const [active, setActive] = React.useState(null);
  const [autoRotate, setAutoRotate] = React.useState(true);
  const [hubHover, setHubHover] = React.useState(false);
  const containerRef = React.useRef(null);
  const orbitRef = React.useRef(null);

  const total = services.length;

  // Geometry (compact = phone-sized)
  const RADIUS = compact ? 122 : 330;
  const HUB = compact ? 96 : 132;
  const NODE = compact ? 56 : 72;
  const HEIGHT = compact ? 480 : 880;

  React.useEffect(() => {
    if (!autoRotate || hubHover) return;
    const t = setInterval(() => {
      setAngle((a) => (a + 0.25) % 360);
    }, 50);
    return () => clearInterval(t);
  }, [autoRotate, hubHover]);

  const toggle = (id) => {
    if (active === id) {
      setActive(null);
      setAutoRotate(true);
    } else {
      setActive(id);
      setAutoRotate(false);
      // Center on the clicked node by rotating so it lands at the bottom
      // (270°) — gives plenty of room above for the expanded card.
      const idx = services.findIndex((s) => s.num === id);
      const target = idx / total * 360;
      setAngle(270 - target);
    }
  };

  const onBgClick = (e) => {
    if (e.target === containerRef.current || e.target === orbitRef.current) {
      setActive(null);
      setAutoRotate(true);
    }
  };

  // Geometry

  return (
    <div
      ref={containerRef}
      onClick={onBgClick}
      style={{
        position: 'relative',
        width: '100%',
        height: HEIGHT,
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'center',
        overflow: 'visible'
      }}>
      
      <div
        ref={orbitRef}
        style={{
          position: 'absolute',
          width: RADIUS * 2 + 80,
          height: RADIUS * 2 + 80,
          display: 'flex',
          alignItems: 'center',
          justifyContent: 'center',
          perspective: '1000px'
        }}>
        
        {/* Orbit ring */}
        <div
          style={{
            position: 'absolute',
            width: RADIUS * 2,
            height: RADIUS * 2,
            borderRadius: '50%',
            border: '1px dashed rgba(244,241,236,0.08)'
          }} />
        
        <div
          style={{
            position: 'absolute',
            width: RADIUS * 2 + 60,
            height: RADIUS * 2 + 60,
            borderRadius: '50%',
            border: '1px solid rgba(91,139,255,0.06)'
          }} />

        {/* Connecting lines (revealed on hub hover) */}
        {services.map((s, i) => {
          const a = (i / total * 360 + angle) % 360;
          return (
            <div key={'line-' + s.num} style={{
              position: 'absolute',
              left: '50%', top: '50%',
              width: RADIUS,
              height: 1,
              transformOrigin: '0 50%',
              transform: `rotate(${a}deg)`,
              background: 'linear-gradient(90deg, rgba(91,139,255,0.5), rgba(91,139,255,0))',
              opacity: hubHover && active === null ? 1 : 0,
              transition: 'opacity .4s',
              pointerEvents: 'none',
              zIndex: 1
            }} />);
        })}

        {/* Center hub */}
        <div
          onMouseEnter={() => setHubHover(true)}
          onMouseLeave={() => setHubHover(false)}
          onClick={(e) => { e.stopPropagation(); setActive(null); setAutoRotate(true); }}
          data-cursor-hover
          data-cursor-label={active ? 'СБРОС' : 'ВСЕ'}
          style={{
            position: 'absolute',
            width: HUB, height: HUB, borderRadius: '50%',
            background: 'linear-gradient(135deg, #5b8bff 0%, #3b6cff 60%, #1a3aa8 100%)',
            display: 'flex', alignItems: 'center', justifyContent: 'center',
            zIndex: 5,
            cursor: 'pointer',
            transform: hubHover ? 'scale(1.06)' : 'scale(1)',
            transition: 'transform .3s cubic-bezier(.2,.7,.2,1)',
            boxShadow: hubHover
              ? '0 0 60px rgba(91,139,255,0.7), 0 0 120px rgba(91,139,255,0.35)'
              : '0 0 40px rgba(91,139,255,0.45), 0 0 80px rgba(91,139,255,0.25)'
          }}>

          {/* Pinging rings */}
          <span style={{
            position: 'absolute', width: HUB + 28, height: HUB + 28, borderRadius: '50%',
            border: '1px solid rgba(244,241,236,0.25)',
            animation: 'orb-ping 2.6s cubic-bezier(0,0,.2,1) infinite'
          }} />
          <span style={{
            position: 'absolute', width: HUB + 60, height: HUB + 60, borderRadius: '50%',
            border: '1px solid rgba(244,241,236,0.15)',
            animation: 'orb-ping 2.6s cubic-bezier(0,0,.2,1) infinite',
            animationDelay: '0.6s'
          }} />
          <div style={{
            display: 'flex', alignItems: 'center', justifyContent: 'center',
            fontWeight: 800, fontSize: compact ? 22 : 30,
            letterSpacing: '-0.03em', color: '#ffffff'
          }}>услуги</div>
        </div>

        {/* Service nodes */}
        {services.map((s, i) => {
          const a = (i / total * 360 + angle) % 360;
          const rad = a * Math.PI / 180;
          const x = RADIUS * Math.cos(rad);
          const y = RADIUS * Math.sin(rad);
          // Pseudo-depth — closer to camera (cos < 0 is "front") = bigger / more opaque
          const depth = (1 + Math.sin(rad)) / 2; // 0..1, 1 = front
          const opacity = 0.45 + depth * 0.55;
          const z = Math.round(100 + 50 * Math.cos(rad));
          const isActive = active === s.num;
          const Icon = SVC_ICONS[s.iconKey] || SVC_ICONS.smm;
          return (
            <div
              key={s.num}
              style={{
                position: 'absolute',
                transform: `translate(${x}px, ${y}px)`,
                opacity: isActive ? 1 : hubHover ? 1 : opacity,
                zIndex: isActive ? 200 : z,
                transition: 'opacity .5s, z-index .3s'
              }}>
              
              {/* Halo */}
              <span style={{
                position: 'absolute',
                left: '50%', top: '50%',
                width: compact ? 64 : 84, height: compact ? 64 : 84,
                marginLeft: compact ? -32 : -42, marginTop: compact ? -32 : -42,
                borderRadius: '50%',
                background: 'radial-gradient(circle, rgba(91,139,255,0.25) 0%, rgba(91,139,255,0) 70%)',
                pointerEvents: 'none'
              }} />

              {/* Node button */}
              <button
                className="orb-node-btn"
                onClick={(e) => {e.stopPropagation();toggle(s.num);}}
                data-cursor-hover
                style={{
                  width: NODE, height: NODE, borderRadius: '50%',
                  display: 'flex', alignItems: 'center', justifyContent: 'center',
                  background: isActive ? '#f4f1ec' : 'rgba(12,14,18,0.85)',
                  color: isActive ? '#0c0e12' : '#f4f1ec',
                  border: `1.5px solid ${isActive ? '#5b8bff' : 'rgba(244,241,236,0.35)'}`,
                  transform: isActive ? 'scale(1.25)' : hubHover ? 'scale(1.1)' : 'scale(1)',
                  transition: 'transform .3s, background .3s, color .3s, border-color .3s',
                  boxShadow: isActive ? '0 0 24px rgba(91,139,255,0.6)' : 'none',
                  cursor: 'pointer'
                }}>
                
                {Icon}
              </button>

              {/* Label — on compact, only the active node's label shows (avoids overlap) */}
              {(!compact || isActive) &&
              <div style={{
                position: 'absolute',
                top: NODE + 12, left: '50%',
                transform: `translateX(-50%) ${isActive ? 'scale(1.05)' : 'scale(1)'}`,
                whiteSpace: 'nowrap',
                fontSize: compact ? 13 : 15,
                fontWeight: 600,
                letterSpacing: '0.01em',
                color: isActive || hubHover ? '#f4f1ec' : 'rgba(244,241,236,0.75)',
                transition: 'color .3s, transform .3s',
                pointerEvents: 'none'
              }}>
                <span style={{
                  fontFamily: 'JetBrains Mono, monospace',
                  fontSize: 11,
                  marginRight: 8,
                  color: isActive ? '#5b8bff' : 'rgba(244,241,236,0.4)'
                }}>{s.num}</span>
                {s.title}
              </div>
              }

              {/* Expanded card */}
              {isActive &&
              <div
                onClick={(e) => e.stopPropagation()}
                style={{
                  position: 'absolute',
                  top: compact ? NODE + 26 : 128, left: '50%',
                  transform: 'translateX(-50%)',
                  width: compact ? 268 : 340,
                  background: 'rgba(12,14,18,0.96)',
                  border: '1px solid rgba(91,139,255,0.4)',
                  borderRadius: 18,
                  padding: '20px 22px 22px',
                  boxShadow: '0 24px 60px rgba(0,0,0,0.5), 0 0 0 1px rgba(255,255,255,0.04) inset',
                  animation: 'orb-card-in .35s cubic-bezier(.2,.7,.2,1)',
                  zIndex: 300
                }}>
                
                  <span style={{
                  position: 'absolute', top: -12, left: '50%',
                  transform: 'translateX(-50%)',
                  width: 1, height: 12,
                  background: 'rgba(91,139,255,0.6)'
                }} />
                  <div style={{
                  display: 'flex', justifyContent: 'space-between', alignItems: 'center',
                  marginBottom: 12
                }}>
                    <span style={{
                    padding: '4px 10px',
                    borderRadius: 999,
                    background: 'rgba(91,139,255,0.18)',
                    color: '#a8bfff',
                    fontSize: 10, fontWeight: 600,
                    letterSpacing: '0.06em',
                    textTransform: 'uppercase'
                  }}>{s.tag || 'услуга'}</span>
                    <span style={{
                    fontFamily: 'JetBrains Mono, monospace',
                    fontSize: 11,
                    color: 'rgba(244,241,236,0.5)'
                  }}>{s.num}</span>
                  </div>
                  <h4 style={{
                  fontSize: 22, fontWeight: 700, letterSpacing: '-0.02em',
                  lineHeight: 1.1, marginBottom: 10
                }}>{s.title}</h4>
                  <p style={{ fontSize: 13.5, color: 'var(--fg-2)', lineHeight: 1.55, marginBottom: 16 }}>
                    {s.desc}
                  </p>
                  {s.includes &&
                <>
                      <div style={{
                    fontSize: 10,
                    textTransform: 'uppercase',
                    letterSpacing: '0.1em',
                    color: 'rgba(244,241,236,0.5)',
                    marginBottom: 8,
                    paddingTop: 12,
                    borderTop: '1px solid rgba(244,241,236,0.08)'
                  }}>что внутри</div>
                      <ul style={{ margin: 0, padding: 0, listStyle: 'none', display: 'flex', flexDirection: 'column', gap: 6 }}>
                        {s.includes.map((it) =>
                    <li key={it} style={{
                      fontSize: 13, color: 'var(--fg-2)',
                      display: 'flex', alignItems: 'flex-start', gap: 8
                    }}>
                            <span style={{
                        flexShrink: 0,
                        width: 4, height: 4, borderRadius: '50%',
                        background: '#5b8bff', marginTop: 7
                      }} />
                            {it}
                          </li>
                    )}
                      </ul>
                    </>
                }
                </div>
              }
            </div>);

        })}
      </div>

      {/* Floating hint */}
      <div style={{
        position: 'absolute',
        bottom: compact ? 4 : -16, left: '50%',
        transform: 'translateX(-50%)',
        fontFamily: 'JetBrains Mono, monospace',
        fontSize: compact ? 11 : 14,
        color: 'rgba(244,241,236,0.62)',
        letterSpacing: '0.08em',
        textTransform: 'uppercase',
        pointerEvents: 'none',
        whiteSpace: 'nowrap'
      }}>
        {active ? 'тап по фону, чтобы закрыть' : (compact ? '· тап по точке — раскрыть ·' : '· наведи на центр — увидишь все · клик по точке — раскрыть ·')}
      </div>

      <style>{`
        .orb-node-btn svg { width: 28px; height: 28px; }
        @keyframes orb-ping {
          0%   { transform: scale(1);   opacity: .8; }
          100% { transform: scale(1.8); opacity: 0;  }
        }
        @keyframes orb-card-in {
          from { opacity: 0; transform: translateX(-50%) translateY(-6px); }
          to   { opacity: 1; transform: translateX(-50%) translateY(0); }
        }
      `}</style>
    </div>);

}

window.RadialOrbitalServices = RadialOrbitalServices;
window.SVC_ICONS = SVC_ICONS;

/* ----- Mobile services accordion (orbital doesn't fit phones) ----------- */
function ServicesAccordion({ services }) {
  const [open, setOpen] = React.useState(0);
  return (
    <div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
      {services.map((s, i) => {
        const Icon = SVC_ICONS[s.iconKey] || SVC_ICONS.smm;
        const isOpen = open === i;
        return (
          <div key={s.num} style={{
            borderRadius: 16,
            border: `1px solid ${isOpen ? 'rgba(91,139,255,0.4)' : 'rgba(244,241,236,0.1)'}`,
            background: isOpen ? 'rgba(91,139,255,0.06)' : 'rgba(16,19,25,0.7)',
            overflow: 'hidden',
            transition: 'border-color .3s, background .3s'
          }}>
            <button
              onClick={() => setOpen(isOpen ? -1 : i)}
              style={{
                width: '100%', textAlign: 'left',
                display: 'flex', alignItems: 'center', gap: 14,
                padding: '18px 16px'
              }}>
              <span style={{
                flexShrink: 0,
                width: 46, height: 46, borderRadius: '50%',
                display: 'flex', alignItems: 'center', justifyContent: 'center',
                background: isOpen ? 'linear-gradient(135deg, #5b8bff, #3b6cff)' : 'rgba(244,241,236,0.06)',
                color: isOpen ? '#fff' : 'var(--fg)',
                border: `1px solid ${isOpen ? 'transparent' : 'rgba(244,241,236,0.16)'}`,
                transition: 'background .3s, color .3s'
              }}>{Icon}</span>
              <span style={{ flex: 1, minWidth: 0 }}>
                <span style={{ display: 'block', fontSize: 17, fontWeight: 600, letterSpacing: '-0.02em', color: isOpen ? 'var(--accent)' : 'var(--fg)' }}>{s.title}</span>
                <span style={{ display: 'block', fontSize: 11, marginTop: 2, fontFamily: 'JetBrains Mono, monospace', color: 'var(--muted)', letterSpacing: '0.05em' }}>{s.num} · {s.tag}</span>
              </span>
              <span style={{ flexShrink: 0, fontSize: 20, color: 'var(--muted)', transform: isOpen ? 'rotate(45deg)' : 'none', transition: 'transform .3s' }}>+</span>
            </button>
            {isOpen &&
            <div style={{ padding: '0 16px 18px 76px', animation: 'orb-card-in .3s cubic-bezier(.2,.7,.2,1)' }}>
              <p style={{ fontSize: 14, color: 'var(--fg-2)', lineHeight: 1.55, marginBottom: 14 }}>{s.desc}</p>
              {s.includes &&
              <ul style={{ margin: 0, padding: 0, listStyle: 'none', display: 'flex', flexDirection: 'column', gap: 7 }}>
                {s.includes.map((it) =>
                <li key={it} style={{ fontSize: 13, color: 'var(--fg-2)', display: 'flex', alignItems: 'flex-start', gap: 9 }}>
                  <span style={{ flexShrink: 0, width: 4, height: 4, borderRadius: '50%', background: '#5b8bff', marginTop: 7 }} />
                  {it}
                </li>
                )}
              </ul>
              }
            </div>
            }
          </div>);
      })}
    </div>);
}

window.ServicesAccordion = ServicesAccordion;