// Land-use comparison — DC pinned on the left, alternatives cycle on the right.
// Bars stacked into BENEFITS (forest green) and IMPACTS (terracotta) groups.
const LU_CAPTIONS = {
  dc: 'Low impact, high revenue.',
  wh: 'Heavy traffic, moderate wages.',
  re: 'High activity, lower wages.',
  rs: 'Low jobs, high public-service demand.',
};
const LU_BENEFIT_KEYS = ['Permanent Jobs', 'Average Wages', 'Tax Revenue Stability'];
const LU_IMPACT_KEYS  = ['Daily Traffic', 'Truck Traffic', 'Public Service Demand', 'Land Use Intensity'];

// Hoisted out of Comparison so their identity is stable across re-renders.
// Inline definitions caused React to remount both panels on every setAltIdx,
// replaying the DC bar animation alongside the alt cycle.
function LuBarGroup({ use, label, keys, kind }) {
  return (
    <div className="lu-group">
      <div className="lu-group-label">— {label}</div>
      {keys.map(k => {
        const m = use.metrics[k];
        const pct = (m.level / 4) * 100;
        return (
          <div key={k} className="lu-row">
            <div className="lu-metric">{k}</div>
            <div className="lu-bar-track">
              <div
                className={`lu-bar-fill lu-bar-${kind}`}
                style={{ '--w': `${pct}%` }}
              />
            </div>
            <div className="lu-label tag-mono">{m.label}</div>
          </div>
        );
      })}
    </div>
  );
}

function LuPanel({ use, hero }) {
  return (
    <div className={`lu-panel ${hero ? 'lu-panel-hero' : ''}`}>
      <div className="lu-panel-head">
        <div className="tag-mono lu-panel-eyebrow">
          {hero ? '— Recommended neighbor' : '— Alternative use'}
        </div>
        <h3 className="lu-panel-name">{use.name}</h3>
        <div className="lu-panel-sub">{use.sub}</div>
        <p className="lu-panel-caption">{LU_CAPTIONS[use.id]}</p>
      </div>
      <LuBarGroup use={use} label="BENEFITS" keys={LU_BENEFIT_KEYS} kind="benefit" />
      <LuBarGroup use={use} label="IMPACTS"  keys={LU_IMPACT_KEYS}  kind="impact" />
    </div>
  );
}

function Comparison() {
  const uses = [
    { id:'dc', name: 'Data Center', icon: <Icons.Shield size={22}/>, sub: 'Piedmont Technology Park',
      metrics: {
        'Daily Traffic': { level: 1, label: 'Low' },
        'Truck Traffic': { level: 1, label: 'Very limited' },
        'Permanent Jobs': { level: 3, label: 'Moderate, highly skilled' },
        'Average Wages': { level: 4, label: 'High' },
        'Public Service Demand': { level: 1, label: 'Low' },
        'Tax Revenue Stability': { level: 4, label: 'Very stable, long-term' },
        'Land Use Intensity': { level: 2, label: 'Moderate size, low activity' },
      }
    },
    { id:'wh', name: 'Warehouse', icon: <Icons.Truck size={22}/>, sub: 'Large distribution facility',
      metrics: {
        'Daily Traffic': { level: 4, label: 'High' },
        'Truck Traffic': { level: 4, label: 'Frequent' },
        'Permanent Jobs': { level: 3, label: 'Moderate, logistics-focused' },
        'Average Wages': { level: 2, label: 'Moderate' },
        'Public Service Demand': { level: 2, label: 'Moderate' },
        'Tax Revenue Stability': { level: 2, label: 'Moderate' },
        'Land Use Intensity': { level: 4, label: 'Large buildings, high activity' },
      }
    },
    { id:'re', name: 'Retail', icon: <Icons.Briefcase size={22}/>, sub: 'Shopping & services',
      metrics: {
        'Daily Traffic': { level: 4, label: 'High' },
        'Truck Traffic': { level: 3, label: 'Regular deliveries' },
        'Permanent Jobs': { level: 4, label: 'Higher count, service-oriented' },
        'Average Wages': { level: 1, label: 'Lower to moderate' },
        'Public Service Demand': { level: 3, label: 'Higher' },
        'Tax Revenue Stability': { level: 2, label: 'Market-dependent' },
        'Land Use Intensity': { level: 3, label: 'Smaller buildings, high activity' },
      }
    },
    { id:'rs', name: 'Residential', icon: <Icons.Home size={22}/>, sub: 'Subdivision development',
      metrics: {
        'Daily Traffic': { level: 2, label: 'Moderate' },
        'Truck Traffic': { level: 1, label: 'Very limited' },
        'Permanent Jobs': { level: 0, label: 'None' },
        'Average Wages': { level: 0, label: 'N/A' },
        'Public Service Demand': { level: 4, label: 'Higher (schools, services)' },
        'Tax Revenue Stability': { level: 3, label: 'Stable but service-intensive' },
        'Land Use Intensity': { level: 3, label: 'Smaller, continuous activity' },
      }
    },
  ];

  const dc = uses[0];
  const alts = uses.slice(1);
  const [altIdx, setAltIdx] = useState(0);
  const alt = alts[altIdx];

  // Auto-cycle through alternatives every 2s. Click on a tab resets the timer.
  useEffect(() => {
    const id = setTimeout(() => setAltIdx((altIdx + 1) % alts.length), 2000);
    return () => clearTimeout(id);
  }, [altIdx]);

  return (
    <section id="landuse" style={{ padding: '140px 0', background: 'var(--paper)' }}>
      <div className="container-x">
        <div style={{ display:'flex', alignItems:'end', justifyContent:'space-between', gap: 40, flexWrap:'wrap', marginBottom: 40 }}>
          <div style={{ maxWidth: 720 }}>
            <Reveal><div className="eyebrow"><span className="dot" /> Land use comparison</div></Reveal>
            <Reveal delay={1}><h2 className="h2" style={{ marginTop: 18 }}>The best possible <span className="accent-underline">neighbor.</span></h2></Reveal>
          </div>
          <Reveal delay={2}>
            <p style={{ color: 'var(--slate-700)', fontSize: 16, lineHeight: 1.6, maxWidth: 480 }}>
              Data center campuses offer significant advantages in traffic, wages, and tax revenue stability compared to alternative development options for the same parcel.
            </p>
          </Reveal>
        </div>

        <Reveal as="div" className="lu-grid">
          {/* DC pinned — animates once on Reveal.in */}
          <div className="lu-side">
            <div className="lu-alt-tabs lu-alt-tabs-static">
              <span className="lu-alt-pill active">{dc.name}</span>
            </div>
            <LuPanel use={dc} hero />
          </div>

          {/* Alternative cycles — re-mounts via key={altIdx} so bar animation replays each cycle */}
          <div className="lu-side">
            <div className="lu-alt-tabs">
              {alts.map((u, i) => (
                <button
                  key={u.id}
                  className={`lu-alt-pill ${i === altIdx ? 'active' : ''}`}
                  onClick={() => setAltIdx(i)}
                >
                  vs {u.name}
                </button>
              ))}
            </div>
            <div key={altIdx} className="lu-alt-stage">
              <LuPanel use={alt} />
            </div>
          </div>
        </Reveal>
      </div>

      <style>{`
        :root {
          --lu-benefit: #4d8567;        /* forest-500 */
          --lu-benefit-deep: #326a4e;   /* forest-600 */
          --lu-impact: #b8553e;          /* terracotta */
          --lu-impact-deep: #8a3a26;
          --lu-bar-track: rgba(10,24,18,.06);
        }

        .lu-grid {
          display: grid;
          grid-template-columns: 1fr 1fr;
          gap: 24px;
          align-items: start;
        }
        .lu-side { display: flex; flex-direction: column; gap: 12px; }

        .lu-alt-tabs { display: flex; gap: 6px; flex-wrap: wrap; min-height: 36px; }
        .lu-alt-pill {
          font-family: var(--font-mono);
          font-size: 11px;
          letter-spacing: .12em;
          text-transform: uppercase;
          padding: 8px 14px;
          border-radius: 999px;
          border: 1px solid rgba(20,32,27,.12);
          background: var(--white);
          color: var(--slate-500);
          cursor: pointer;
          transition: background .2s ease, color .2s ease, border-color .2s ease;
        }
        .lu-alt-pill:hover { color: var(--ink); border-color: var(--ink); }
        .lu-alt-pill.active {
          background: var(--ink);
          color: var(--paper);
          border-color: var(--ink);
        }
        .lu-alt-tabs-static .lu-alt-pill { cursor: default; }

        .lu-panel {
          background: var(--white);
          border: 1px solid rgba(20,32,27,.08);
          border-radius: 18px;
          padding: 28px 28px 24px;
        }
        .lu-panel-hero {
          background: var(--cream-100);
          border-color: rgba(32,74,121,.18);
          box-shadow: 0 0 0 1px rgba(32,74,121,.06) inset;
        }
        .lu-panel-eyebrow { color: var(--slate-500); }
        .lu-panel-hero .lu-panel-eyebrow { color: var(--accent-deep); }
        .lu-panel-name {
          font-family: var(--font-display);
          font-size: 32px;
          font-weight: 500;
          letter-spacing: -0.02em;
          line-height: 1;
          margin: 8px 0 0;
          color: var(--ink);
        }
        .lu-panel-sub { color: var(--slate-500); font-size: 13px; margin-top: 4px; }
        .lu-panel-caption {
          font-family: var(--font-display);
          font-size: 18px;
          line-height: 1.35;
          color: var(--ink);
          margin: 18px 0 0;
          font-style: italic;
        }

        .lu-group { margin-top: 22px; }
        .lu-group-label {
          font-family: var(--font-mono);
          font-size: 10.5px;
          letter-spacing: .18em;
          color: var(--slate-500);
          text-transform: uppercase;
          margin-bottom: 10px;
        }
        .lu-row {
          display: grid;
          grid-template-columns: 130px 1fr 130px;
          gap: 16px;
          align-items: center;
          padding: 7px 0;
        }
        .lu-metric {
          font-family: var(--font-display);
          font-size: 13px;
          font-weight: 500;
          color: var(--ink);
        }
        .lu-bar-track {
          height: 10px;
          background: var(--lu-bar-track);
          border-radius: 999px;
          overflow: hidden;
          position: relative;
        }
        .lu-bar-fill {
          height: 100%;
          width: 0;
          border-radius: inherit;
          animation: lu-barFill .9s cubic-bezier(.2,.8,.25,1) both;
        }
        .lu-bar-benefit {
          background: linear-gradient(90deg, var(--lu-benefit), var(--lu-benefit-deep));
        }
        .lu-bar-impact {
          background: linear-gradient(90deg, var(--lu-impact), var(--lu-impact-deep));
        }
        .lu-label {
          color: var(--slate-500);
          font-size: 10.5px;
          letter-spacing: .04em;
          text-align: right;
        }
        @keyframes lu-barFill {
          from { width: 0; }
          to { width: var(--w); }
        }

        /* Hold bar fill at 0 until the section reveals into view, then play. */
        .reveal:not(.in) .lu-bar-fill { animation-play-state: paused; }

        /* Mobile: stack DC over alt, both full-width. */
        @media (max-width: 760px) {
          .lu-grid { grid-template-columns: 1fr; gap: 18px; }
          .lu-row { grid-template-columns: 110px 1fr 90px; gap: 10px; }
          .lu-panel { padding: 22px 20px 18px; }
          .lu-panel-name { font-size: 28px; }
          .lu-panel-caption { font-size: 16px; }
        }
        @media (max-width: 480px) {
          .lu-row {
            grid-template-columns: 1fr auto;
            row-gap: 4px;
          }
          .lu-row .lu-bar-track { grid-column: 1 / -1; }
          .lu-label { text-align: left; grid-column: 1 / -1; }
        }
      `}</style>
    </section>
  );
}
window.Comparison = Comparison;
