/* Repair comparison — 3 illustrative shop cards with a value-score breakdown
   across 6 factors. Real-feeling Phoenix shop names (illustrative). */
function RepairComparison() {
  const { Badge } = window.RevvoDesignSystem_a89086;
  const { Reveal, Ic } = window;

  // Live bidding: shops "arrive" one by one when the section enters view.
  const liveRef = React.useRef(null);
  const [bidsIn, setBidsIn] = React.useState(0);
  const [runId, setRunId] = React.useState(0);
  const startBids = React.useCallback(() => {
    setBidsIn(0);
    const reduce = window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
    if (reduce) { setBidsIn(3); return; }
    [700, 1500, 2400].forEach((t, i) => setTimeout(() => setBidsIn(i + 1), t));
  }, []);
  React.useEffect(() => {
    const node = liveRef.current; if (!node) return;
    let fired = false;
    const trigger = () => { if (fired) return; fired = true; startBids(); };
    const check = () => { const r = node.getBoundingClientRect(); if (r.top < (window.innerHeight || 800) * 0.7 && r.bottom > 0) trigger(); };
    check();
    window.addEventListener('scroll', check, { passive: true });
    const poll = setInterval(() => { if (fired) { clearInterval(poll); return; } check(); }, 500);
    return () => { window.removeEventListener('scroll', check); clearInterval(poll); };
  }, [startBids, runId]);
  const replayBids = () => { setRunId((n) => n + 1); startBids(); };

  const shops = [
    { name: 'Camelback Auto Works', area: 'Central Phoenix · 1.8 mi', price: 210, eta: 'Tomorrow', rating: 4.9, reviews: 412, score: 96, best: true,
      factors: { Price: 92, Rating: 98, Distance: 90, Availability: 95, Speed: 94, Relevance: 97 } },
    { name: 'Roosevelt Auto Collective', area: 'Downtown · 3.2 mi', price: 240, eta: 'In 2 days', rating: 4.8, reviews: 286, score: 91, best: false,
      factors: { Price: 84, Rating: 95, Distance: 82, Availability: 90, Speed: 92, Relevance: 93 } },
    { name: 'South Mountain Service Co.', area: 'South Phoenix · 5.6 mi', price: 195, eta: 'In 4 days', rating: 4.7, reviews: 198, score: 88, best: false,
      factors: { Price: 98, Rating: 90, Distance: 74, Availability: 80, Speed: 84, Relevance: 91 } },
  ];
  const factorList = ['Price', 'Rating', 'Distance', 'Availability', 'Speed', 'Relevance'];

  return (
    <section id="compare" style={{ position: 'relative', padding: 'var(--space-section) clamp(20px, 5vw, 48px)',
      background: 'var(--surface-section)', borderTop: '1px solid var(--line-100)', borderBottom: '1px solid var(--line-100)' }}>
      <div style={{ maxWidth: 'var(--container)', margin: '0 auto' }}>
        <Reveal style={{ textAlign: 'center', marginBottom: 52 }}>
          <Badge variant="neutral" style={{ marginBottom: 20 }}>Compare offers</Badge>
          <h2 style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 'var(--fs-display-2)',
            letterSpacing: 'var(--ls-snug)', color: 'var(--text-strong)', margin: '0 0 14px', lineHeight: 'var(--lh-snug)' }}>
            The best value &mdash; not just the lowest price.
          </h2>
          <p style={{ fontSize: 'var(--fs-lead)', color: 'var(--text-body)', maxWidth: 580, margin: '0 auto' }}>
            Vetted local shops respond with real offers. ALEROQ scores each on six factors so you can choose with confidence.
          </p>
        </Reveal>

        {/* live bidding status bar */}
        <div ref={liveRef} style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 16,
          flexWrap: 'wrap', maxWidth: 'var(--container)', margin: '0 auto 22px' }}>
          <span style={{ display: 'inline-flex', alignItems: 'center', gap: 10, fontFamily: 'var(--font-mono)',
            fontSize: 12, letterSpacing: '0.12em', textTransform: 'uppercase', color: 'var(--text-muted)' }}>
            <span className={bidsIn < 3 ? 'blink' : ''} style={{ width: 8, height: 8, borderRadius: '50%',
              background: bidsIn < 3 ? 'var(--accent)' : 'var(--positive)', boxShadow: `0 0 10px ${bidsIn < 3 ? 'var(--accent)' : 'var(--positive)'}` }} />
            {bidsIn < 3 ? `Bidding live · ${bidsIn} of 3 in` : '3 offers received'}
          </span>
          <button onClick={replayBids} style={{ display: 'inline-flex', alignItems: 'center', gap: 7, padding: '7px 13px',
            cursor: 'pointer', background: 'transparent', border: '1px solid var(--border)', borderRadius: 'var(--radius-pill)',
            color: 'var(--text-muted)', font: 'inherit', fontFamily: 'var(--font-mono)', fontSize: 12 }}>
            <Ic name="RotateCcw" size={13} /> Replay bids
          </button>
        </div>

        <div className="compare-grid" style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 16 }}>
          {shops.map((s, i) => (
            <div key={s.name} style={{ height: '100%',
              opacity: bidsIn > i ? 1 : 0,
              transform: bidsIn > i ? 'none' : 'translateY(26px) scale(0.97)',
              filter: bidsIn > i ? 'none' : 'blur(6px)',
              transition: 'opacity 0.6s var(--ease-out), transform 0.6s var(--ease-out), filter 0.6s var(--ease-out)' }}>
              <div style={{
                position: 'relative', height: '100%', boxSizing: 'border-box',
                background: 'var(--surface)', borderRadius: 'var(--radius-lg)', padding: 'clamp(22px, 2.6vw, 28px)',
                border: `1px solid ${s.best ? 'rgba(22,214,128,0.5)' : 'var(--border)'}`,
                boxShadow: s.best ? 'var(--glow-green-sm, 0 0 28px rgba(22,214,128,0.18))' : 'none',
              }}>
                {s.best && (
                  <span style={{ position: 'absolute', top: 18, right: 20, display: 'inline-flex', alignItems: 'center', gap: 6,
                    padding: '5px 11px', borderRadius: 'var(--radius-pill)', background: 'var(--positive)', color: '#04130c',
                    fontFamily: 'var(--font-mono)', fontSize: 10, fontWeight: 700, letterSpacing: '0.08em', textTransform: 'uppercase' }}>
                    <Ic name="Sparkles" size={11} color="#04130c" /> Best value
                  </span>
                )}
                <div style={{ fontFamily: 'var(--font-display)', fontWeight: 600, fontSize: 19, color: 'var(--text-strong)', margin: '0 0 4px', maxWidth: '78%' }}>{s.name}</div>
                <div style={{ fontSize: 13, color: 'var(--text-muted)', marginBottom: 18 }}>{s.area}</div>

                <div style={{ display: 'flex', alignItems: 'baseline', gap: 10, marginBottom: 4 }}>
                  <span style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 34, color: 'var(--text-strong)' }}>${s.price}</span>
                  <span style={{ fontSize: 13, color: 'var(--text-muted)' }}>est. total</span>
                </div>
                <div style={{ display: 'flex', alignItems: 'center', gap: 14, fontSize: 13, color: 'var(--text-body)', marginBottom: 20 }}>
                  <span style={{ display: 'inline-flex', alignItems: 'center', gap: 5 }}><Ic name="Star" size={13} color="var(--silver-300)" /> {s.rating} ({s.reviews})</span>
                  <span style={{ display: 'inline-flex', alignItems: 'center', gap: 5 }}><Ic name="CalendarClock" size={13} color="var(--silver-300)" /> {s.eta}</span>
                </div>

                {/* value score */}
                <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '12px 14px', marginBottom: 16,
                  borderRadius: 'var(--radius-md)', background: 'var(--inset)', border: '1px solid var(--line-100)' }}>
                  <span style={{ fontFamily: 'var(--font-mono)', fontSize: 11, letterSpacing: '0.08em', textTransform: 'uppercase', color: 'var(--text-muted)' }}>Value score</span>
                  <span style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 22, color: s.best ? 'var(--positive-bright)' : 'var(--text-strong)' }}>{s.score}</span>
                </div>

                {/* factor bars */}
                <div style={{ display: 'grid', gap: 9 }}>
                  {factorList.map((f) => (
                    <div key={f} style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
                      <span style={{ flex: '0 0 78px', fontSize: 12, color: 'var(--text-muted)' }}>{f}</span>
                      <span style={{ flex: 1, height: 5, borderRadius: 3, background: 'var(--black-400)', overflow: 'hidden' }}>
                        <span style={{ display: 'block', height: '100%', width: `${s.factors[f]}%`, borderRadius: 3,
                          background: s.best ? 'linear-gradient(90deg, var(--positive), var(--green-300))' : 'var(--silver-400)' }} />
                      </span>
                    </div>
                  ))}
                </div>
                {bidsIn === i + 1 && bidsIn < 3 && (
                  <span className="newbid-pop" style={{ position: 'absolute', top: 18, left: 20, display: 'inline-flex',
                    alignItems: 'center', gap: 6, padding: '4px 10px', borderRadius: 'var(--radius-pill)',
                    background: 'var(--accent)', color: '#fff', fontFamily: 'var(--font-mono)', fontSize: 10,
                    fontWeight: 700, letterSpacing: '0.08em', textTransform: 'uppercase' }}>New bid</span>
                )}
              </div>
            </div>
          ))}
        </div>
        <Reveal delay={120}>
          <p style={{ textAlign: 'center', margin: '28px auto 0', fontSize: 13, color: 'var(--text-faint)', maxWidth: 560 }}>
            Shops and figures shown are illustrative. Real offers depend on your vehicle, location, and a mechanic&rsquo;s inspection.
          </p>
        </Reveal>
      </div>
    </section>
  );
}
window.RepairComparison = RepairComparison;
