// STADIUM STYLES — 4 total variations (classic + 3 new).
// All take the same props as BookingStadium and render the seat-map
// portion differently. They're drop-in replacements for the
// StadiumSeats() visualization.

// ---------------------------------------------------------------
// CURVED — amphitheater arcs; fewer seats up top, more down bottom.
// ---------------------------------------------------------------
function StadiumCurved({ total, taken, onPick, mySeatIdx }) {
  // 5 curved rows, counts chosen so they sum to ~total (60)
  const rows = [8, 10, 12, 14, 16]; // = 60
  const rowOffsets = rows.reduce((acc,c,i) => [...acc, (acc[i-1]||0) + (i>0?rows[i-1]:0)], []);
  const flatIdx = (r, s) => rowOffsets[r] + s;
  // Deterministic scattered pattern
  const isTaken = (idx) => {
    if (idx === mySeatIdx) return false;
    const h = ((idx * 2654435761) >>> 0) % total;
    return h < taken;
  };

  return (
    <div style={{
      padding:'18px 12px 14px',marginTop:10,borderRadius:14,
      background:'linear-gradient(180deg,rgba(124,58,237,0.07),rgba(11,11,20,0.02))',
      position:'relative',overflow:'hidden',
    }}>
      {/* Stage (pulsing orb) */}
      <div style={{textAlign:'center',marginBottom:10}}>
        <div style={{
          display:'inline-block',width:58,height:58,borderRadius:'50%',
          background:BRAND.gradient,
          boxShadow:'0 0 42px rgba(124,58,237,0.75), inset 0 2px 8px rgba(255,255,255,0.35)',
          animation:'apex-glow-breathe 3s ease-in-out infinite',
        }}/>
        <div style={{
          fontSize:9,letterSpacing:3,textTransform:'uppercase',color:'var(--mute)',
          fontWeight:800,marginTop:6,
        }}>
          ← STAGE · Apex Live →
        </div>
      </div>
      <SoloSeatsLabel/>

      {/* Rows */}
      <div style={{display:'flex',flexDirection:'column',gap:7,padding:'0 2px'}}>
        {rows.map((count, rIdx) => {
          // Curve: scale row width
          const widthPct = 55 + rIdx * 9; // 55 → 91
          return (
            <div key={rIdx} style={{
              display:'flex',justifyContent:'center',gap:3,
              width:`${widthPct}%`,margin:'0 auto',
              transform:`perspective(300px) rotateX(${12 - rIdx*2.5}deg)`,
              transformOrigin:'top',
            }}>
              {Array.from({length:count}).map((_, sIdx) => {
                const idx = flatIdx(rIdx, sIdx);
                const filled = isTaken(idx);
                const isMine = idx === mySeatIdx;
                return (
                  <button key={sIdx}
                    onClick={() => !filled && onPick && onPick(idx)}
                    disabled={filled}
                    title={isMine ? 'Your seat ✓' : (filled ? `Row ${rIdx+1} · Seat ${sIdx+1} · Taken` : `Row ${rIdx+1} · Seat ${sIdx+1}`)}
                    style={{
                      position:'relative',
                      flex:1,aspectRatio:'1/1.1',
                      minWidth:0,maxWidth:22,
                      border:'none',padding:0,borderRadius:'4px 4px 2px 2px',
                      background: isMine
                        ? 'linear-gradient(180deg,#10b981 0%,#059669 100%)'
                        : (filled
                          ? 'linear-gradient(180deg,rgba(124,58,237,0.75) 0%,rgba(124,58,237,0.55) 100%)'
                          : 'linear-gradient(180deg,rgba(255,255,255,0.95) 0%,#f5f7fa 100%)'),
                      boxShadow: isMine
                        ? '0 0 0 2px #10b981, 0 0 12px rgba(16,185,129,0.7)'
                        : (filled
                          ? 'inset 0 -2px 0 rgba(0,0,0,0.2), 0 1px 2px rgba(124,58,237,0.3)'
                          : 'inset 0 -2px 0 rgba(11,11,20,0.08), 0 0 0 1px var(--border-strong)'),
                      cursor: filled ? 'not-allowed' : 'pointer',
                      transition:'transform 0.15s',
                      transform: isMine ? 'translateY(-3px) scale(1.12)' : 'none',
                      display:'flex',alignItems:'center',justifyContent:'center',
                    }}
                    onMouseEnter={e => { if (!filled && !isMine) e.currentTarget.style.transform = 'translateY(-3px) scale(1.1)'; }}
                    onMouseLeave={e => { if (!isMine) e.currentTarget.style.transform = 'translateY(0) scale(1)'; }}
                  >
                    {!filled && !isMine && (
                      <span style={{
                        width:3,height:3,borderRadius:'50%',background:'#10b981',
                        boxShadow:'0 0 3px rgba(16,185,129,0.9)',
                        animation:'apex-avail-blink 1.4s ease-in-out infinite',
                      }}/>
                    )}
                  </button>
                );
              })}
            </div>
          );
        })}
      </div>

      <SeatLegend total={total} taken={taken}/>
    </div>
  );
}

// ---------------------------------------------------------------
// ZOOM — gallery grid: each seat is a webcam-like tile
// ---------------------------------------------------------------
function StadiumZoom({ total, taken, onPick, mySeatIdx }) {
  // A few realistic avatars / initials
  const INITIALS = ['JM','SA','TK','RP','DN','KL','AB','MH','CJ','EV','NW','PS','LR','GC','QT','BD','OX','HF','UY','WI'];
  const TILE_H = 48;
  const isTaken = (i) => {
    if (i === mySeatIdx) return false;
    const h = ((i * 2654435761) >>> 0) % total;
    return h < taken;
  };
  return (
    <div style={{
      padding:'14px 10px 12px',marginTop:10,borderRadius:12,
      background:'#0b0b14',color:'#fff',position:'relative',
    }}>
      <SoloSeatsLabel tone="dark"/>
      {/* Zoom top bar */}
      <div style={{
        display:'flex',justifyContent:'space-between',alignItems:'center',
        padding:'0 4px 10px',borderBottom:'1px solid rgba(255,255,255,0.08)',
        fontSize:11,fontWeight:600,
      }}>
        <div style={{display:'flex',alignItems:'center',gap:8}}>
          <span style={{display:'inline-flex',alignItems:'center',gap:5,color:'#ef4444',fontWeight:800,letterSpacing:1.5,fontSize:10}}>
            <LiveDot color="#ef4444" size={6}/> LIVE
          </span>
          <span style={{color:'rgba(255,255,255,0.65)'}}>Apex · The Live Walkthrough</span>
        </div>
        <span style={{
          fontSize:10,color:'rgba(255,255,255,0.55)',fontFamily:'ui-monospace,Menlo,monospace',letterSpacing:0.5,
        }}>{taken} waiting · {total - taken} open</span>
      </div>

      {/* Gallery grid */}
      <div style={{
        display:'grid',gridTemplateColumns:'repeat(6, 1fr)',gap:4,padding:'10px 2px',
      }}>
        {Array.from({length: total}).map((_, i) => {
          const filled = isTaken(i);
          const isMine = i === mySeatIdx;
          const init = INITIALS[i % INITIALS.length];
          const hue = (i * 37) % 360;
          return (
            <button key={i}
              onClick={() => !filled && onPick && onPick(i)}
              disabled={filled}
              title={isMine ? 'Your seat ✓' : (filled ? `Agent ${init}` : `Join the room`)}
              style={{
                position:'relative',aspectRatio:'4/3',height:'auto',minHeight:TILE_H,
                borderRadius:5,padding:0,border:'none',
                background: isMine
                  ? 'linear-gradient(135deg,#10b981 0%,#059669 100%)'
                  : (filled
                    ? `linear-gradient(135deg, hsl(${hue} 45% 25%) 0%, hsl(${(hue+40)%360} 40% 18%) 100%)`
                    : 'rgba(255,255,255,0.04)'),
                boxShadow: isMine
                  ? '0 0 0 2px #10b981, 0 0 14px rgba(16,185,129,0.7)'
                  : 'inset 0 0 0 1px rgba(255,255,255,0.08)',
                cursor: filled ? 'default' : 'pointer',
                display:'flex',alignItems:'center',justifyContent:'center',
                overflow:'hidden',transition:'transform 0.15s',
                transform: isMine ? 'scale(1.06)' : 'scale(1)',
              }}
              onMouseEnter={e => { if (!filled && !isMine) { e.currentTarget.style.transform='scale(1.04)'; e.currentTarget.style.background='rgba(124,58,237,0.2)'; }}}
              onMouseLeave={e => { if (!filled && !isMine) { e.currentTarget.style.transform='scale(1)'; e.currentTarget.style.background='rgba(255,255,255,0.04)'; }}}
            >
              {isMine ? (
                <span style={{fontSize:11,fontWeight:800,color:'#fff',letterSpacing:0.5}}>YOU</span>
              ) : filled ? (
                <>
                  <span style={{fontSize:11,fontWeight:800,color:'rgba(255,255,255,0.85)',letterSpacing:0.5}}>{init}</span>
                  <span style={{
                    position:'absolute',bottom:2,left:3,width:5,height:5,borderRadius:'50%',
                    background:'#10b981',boxShadow:'0 0 4px #10b981',
                  }}/>
                </>
              ) : (
                <span style={{
                  display:'flex',alignItems:'center',gap:4,
                  fontSize:9,fontWeight:700,color:'rgba(255,255,255,0.45)',letterSpacing:1,textTransform:'uppercase',
                }}>
                  <span style={{
                    width:5,height:5,borderRadius:'50%',background:'#10b981',
                    boxShadow:'0 0 6px rgba(16,185,129,0.9)',
                    animation:'apex-avail-blink 1.4s ease-in-out infinite',
                  }}/>
                  Open
                </span>
              )}
            </button>
          );
        })}
      </div>

      <div style={{
        marginTop:6,fontSize:11,color:'rgba(255,255,255,0.65)',
        textAlign:'center',fontFamily:'ui-monospace,Menlo,monospace',letterSpacing:0.3,
      }}>
        Tap an open tile to claim your spot in the room.
      </div>
      <SeatLegend total={total} taken={taken}/>
    </div>
  );
}

// ---------------------------------------------------------------
// AUDITORIUM — stylized side-view cross-section (SVG)
// ---------------------------------------------------------------
function StadiumAuditorium({ total, taken, onPick, mySeatIdx }) {
  // 6 tiered rows; seats rendered as little silhouettes.
  const rows = [6, 8, 10, 12, 12, 12]; // = 60
  const rowOffsets = rows.reduce((acc,c,i) => [...acc, (acc[i-1]||0) + (i>0?rows[i-1]:0)], []);
  const flatIdx = (r, s) => rowOffsets[r] + s;
  const isTaken = (idx) => {
    if (idx === mySeatIdx) return false;
    const h = ((idx * 2654435761) >>> 0) % total;
    return h < taken;
  };
  return (
    <div style={{
      padding:'16px 10px 12px',marginTop:10,borderRadius:14,
      background:'linear-gradient(180deg,#13131d 0%,#0b0b14 100%)',
      color:'#fff',position:'relative',overflow:'hidden',
    }}>
      <div style={{position:'relative',zIndex:2}}><SoloSeatsLabel tone="dark"/></div>
      {/* Ambient fog */}
      <div style={{
        position:'absolute',inset:0,
        background:'radial-gradient(ellipse at 50% 90%, rgba(124,58,237,0.22) 0%, transparent 55%)',
        pointerEvents:'none',
      }}/>

      <div style={{position:'relative',zIndex:1}}>
        {/* Big screen at top */}
        <div style={{
          margin:'0 auto 18px',maxWidth:'80%',aspectRatio:'16/5',
          borderRadius:6,
          background:`linear-gradient(135deg, rgba(124,58,237,0.6) 0%, rgba(59,130,246,0.5) 100%)`,
          boxShadow:'0 0 40px rgba(124,58,237,0.6), inset 0 0 0 2px rgba(255,255,255,0.1)',
          position:'relative',display:'flex',alignItems:'center',justifyContent:'center',
          overflow:'hidden',
        }}>
          {/* Scan lines */}
          <div style={{
            position:'absolute',inset:0,
            background:'repeating-linear-gradient(0deg,transparent 0 3px,rgba(255,255,255,0.03) 3px 4px)',
            pointerEvents:'none',
          }}/>
          <div style={{
            fontSize:10.5,fontWeight:800,letterSpacing:3,textTransform:'uppercase',
            color:'#fff',textShadow:'0 0 10px rgba(255,255,255,0.6)',
            zIndex:1,display:'flex',alignItems:'center',gap:7,
          }}>
            <LiveDot color="#fff" size={6}/> Apex · On screen
          </div>
        </div>

        {/* Rows — tiered (each row pushed further back, slightly smaller) */}
        <div style={{display:'flex',flexDirection:'column',gap:5,padding:'0 4px'}}>
          {rows.map((count, rIdx) => {
            const scale = 1 - rIdx * 0.03; // slight shrink each row back
            return (
              <div key={rIdx} style={{
                display:'flex',justifyContent:'center',alignItems:'flex-end',gap:2,
                transform:`scale(${scale})`,transformOrigin:'center top',
                opacity: 1 - rIdx * 0.04,
              }}>
                {Array.from({length:count}).map((_, sIdx) => {
                  const idx = flatIdx(rIdx, sIdx);
                  const filled = isTaken(idx);
                  const isMine = idx === mySeatIdx;
                  return (
                    <button key={sIdx}
                      onClick={() => !filled && onPick && onPick(idx)}
                      disabled={filled}
                      title={isMine ? 'Your seat ✓' : (filled ? 'Taken' : `Row ${rIdx+1} · Seat ${sIdx+1}`)}
                      style={{
                        background:'transparent',border:'none',padding:0,margin:0,
                        cursor:filled?'not-allowed':'pointer',
                        transition:'transform 0.15s',lineHeight:0,
                      }}
                      onMouseEnter={e => { if (!filled) e.currentTarget.style.transform='translateY(-3px) scale(1.2)'; }}
                      onMouseLeave={e => e.currentTarget.style.transform='translateY(0) scale(1)'}
                    >
                      {/* Silhouette seat SVG — head + chair */}
                      <svg width="14" height="18" viewBox="0 0 14 18" fill="none" xmlns="http://www.w3.org/2000/svg" style={isMine?{filter:'drop-shadow(0 0 6px #10b981)'}:undefined}>
                        {filled ? (
                          <>
                            <circle cx="7" cy="5" r="3" fill="rgba(255,255,255,0.82)"/>
                            <path d="M2 14c0-2.7 2.2-5 5-5s5 2.3 5 5v1H2v-1z" fill="rgba(255,255,255,0.78)"/>
                          </>
                        ) : isMine ? (
                          <>
                            <circle cx="7" cy="5" r="3" fill="#10b981"/>
                            <path d="M2 14c0-2.7 2.2-5 5-5s5 2.3 5 5v1H2v-1z" fill="#10b981"/>
                          </>
                        ) : (
                          <>
                            <rect x="2" y="6" width="10" height="8" rx="1.5" fill="none" stroke="rgba(255,255,255,0.28)" strokeWidth="1.2"/>
                            <rect x="3" y="14" width="8" height="2" rx="0.5" fill="rgba(255,255,255,0.22)"/>
                            <circle cx="7" cy="3" r="1.4" fill="#10b981" style={{filter:'drop-shadow(0 0 3px #10b981)',animation:'apex-avail-blink 1.4s ease-in-out infinite'}}/>
                          </>
                        )}
                      </svg>
                    </button>
                  );
                })}
              </div>
            );
          })}
        </div>

        {/* Floor strip */}
        <div style={{
          marginTop:10,height:3,borderRadius:2,
          background:'linear-gradient(90deg,transparent 0%,rgba(124,58,237,0.5) 50%,transparent 100%)',
        }}/>

        <div style={{
          display:'flex',justifyContent:'space-between',alignItems:'center',
          padding:'10px 4px 0',fontSize:11,color:'rgba(255,255,255,0.68)',letterSpacing:0.3,
        }}>
          <span style={{display:'flex',alignItems:'center',gap:5}}>
            <span style={{width:7,height:7,borderRadius:'50%',background:'rgba(255,255,255,0.8)'}}/> {taken} seated
          </span>
          <span style={{fontWeight:700,color:'#fff'}}>{total - taken} open seats →</span>
        </div>
      </div>
      <SeatLegend total={total} taken={taken}/>
    </div>
  );
}

// Router — replaces StadiumSeats based on tweaks.stadiumStyle
function StadiumSwitcher(props) {
  const { tweaks } = useApp();
  const style = tweaks.stadiumStyle || 'classic';
  if (style === 'curved')     return <StadiumCurved {...props}/>;
  if (style === 'zoom')       return <StadiumZoom {...props}/>;
  if (style === 'auditorium') return <StadiumAuditorium {...props}/>;
  return <StadiumSeats {...props}/>;
}

Object.assign(window, { StadiumCurved, StadiumZoom, StadiumAuditorium, StadiumSwitcher });
