// ee-visuals.jsx — heavier reusable schematic visuals (monoline, brand-teal):
// VisualStage panel, SitePlan (top-down), HouseElevation + Reticle, RoofSection, Phone.
// Loaded as text/babel after ee-kit.jsx.

// Grounding panel that holds a scene's visual (upper-right composition slot).
function VisualStage({ t, children, pad = true }) {
  const p = ramp(t, 0.15, 0.6, Easing.easeOutCubic);
  return (
    <div style={{
      position: 'absolute', left: 1016, top: 150, width: 832, height: 570,
      borderRadius: 28, background: EE.teal05, border: `1px solid ${EE.border}`,
      boxShadow: '0 18px 40px rgba(4,69,76,0.08)',
      opacity: p, transform: `translateY(${(1 - p) * 18}px)`,
      overflow: 'hidden',
    }}>
      {children}
    </div>
  );
}

// stroke-draw helper: returns dash style to "draw" a path of length len by progress p
function draw(len, p) {
  return { strokeDasharray: len, strokeDashoffset: len * (1 - clamp(p, 0, 1)) };
}

// ── Top-down site plan. Overlays driven by 0..1 opacity props. ──────────────
function SitePlan({ t, show = {} }) {
  const s = (k) => clamp(show[k] || 0, 0, 1);
  const T = EE.teal, M = EE.mint;
  return (
    <svg viewBox="0 0 832 570" width="832" height="570" style={{ position: 'absolute', inset: 0 }}
      fill="none" stroke={T} strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round">
      {/* site-map label */}
      <text x="44" y="56" fill={EE.muted} stroke="none" fontFamily={EE.body} fontSize="15" fontWeight="600" letterSpacing="3">SITE MAP</text>
      {/* compass */}
      <g transform="translate(760,52)" opacity="0.8">
        <circle cx="0" cy="0" r="18" stroke={EE.muted} strokeWidth="2" />
        <path d="M0 -12l4 9-4-3-4 3z" fill={T} stroke="none" />
        <text x="0" y="-22" fill={EE.muted} stroke="none" fontFamily={EE.body} fontSize="11" fontWeight="700" textAnchor="middle">N</text>
      </g>
      {/* lot boundary */}
      <rect x="60" y="84" width="712" height="448" rx="10" stroke={EE.border} strokeWidth="2" />
      {/* lawn / landscaping markers (small trees) */}
      <g stroke={EE.teal20 || '#cddadc'} strokeWidth="2">
        <circle cx="120" cy="150" r="14" stroke="#cddadc" />
        <circle cx="120" cy="470" r="14" stroke="#cddadc" />
        <circle cx="712" cy="150" r="14" stroke="#cddadc" />
      </g>
      {/* house footprint */}
      <rect x="250" y="150" width="320" height="200" rx="6" fill={EE.surface} />
      <text x="410" y="258" fill={EE.muted} stroke="none" fontFamily={EE.body} fontSize="16" fontWeight="600" textAnchor="middle" letterSpacing="2">HOUSE</text>
      {/* driveway to street (bottom) */}
      <path d="M360 350v182M460 350v182" stroke={EE.border} strokeWidth="2" />
      <text x="300" y="500" fill={EE.muted} stroke="none" fontFamily={EE.body} fontSize="13" fontWeight="600" letterSpacing="2">DRIVE</text>

      {/* OVERLAY: driveway protection mat */}
      <g opacity={s('mat')}>
        <rect x="362" y="352" width="96" height="178" fill={M} fillOpacity="0.16" stroke={M} strokeWidth="2" strokeDasharray="6 6" />
      </g>

      {/* OVERLAY: tarps over landscaping + AC + walkway */}
      <g opacity={s('tarp')}>
        <rect x="92" y="122" width="56" height="56" rx="6" fill={M} fillOpacity="0.18" stroke={M} />
        <rect x="92" y="442" width="56" height="56" rx="6" fill={M} fillOpacity="0.18" stroke={M} />
        <rect x="684" y="122" width="56" height="56" rx="6" fill={M} fillOpacity="0.18" stroke={M} />
        {/* AC unit by house */}
        <rect x="588" y="300" width="44" height="40" rx="5" fill={M} fillOpacity="0.18" stroke={M} />
        <text x="610" y="360" fill={T} stroke="none" fontFamily={EE.body} fontSize="11" fontWeight="600" textAnchor="middle">AC</text>
        {/* walkway strip */}
        <rect x="156" y="356" width="86" height="26" rx="4" fill={M} fillOpacity="0.18" stroke={M} />
      </g>

      {/* OVERLAY: trailer + bin parked per map (top right of drive) */}
      <g opacity={s('trailer')}>
        <rect x="500" y="430" width="120" height="56" rx="6" fill={EE.surface} stroke={T} />
        <circle cx="528" cy="492" r="7" fill={EE.surface} stroke={T} />
        <circle cx="592" cy="492" r="7" fill={EE.surface} stroke={T} />
        <text x="560" y="464" fill={T} stroke="none" fontFamily={EE.body} fontSize="12" fontWeight="600" textAnchor="middle">TRAILER</text>
        <rect x="636" y="436" width="84" height="50" rx="5" fill={EE.surface} stroke={T} />
        <text x="678" y="465" fill={T} stroke="none" fontFamily={EE.body} fontSize="11" fontWeight="600" textAnchor="middle">BIN</text>
      </g>

      {/* OVERLAY: lift point + staged material bundles at house corner */}
      <g opacity={s('materials')}>
        <g transform="translate(214,236)">
          {[0, 1, 2].map((i) => (
            <rect key={i} x={-i * 5} y={-i * 16} width="64" height="14" rx="3" fill={M} fillOpacity={0.22} stroke={M} />
          ))}
          <text x="32" y="44" fill={T} stroke="none" fontFamily={EE.body} fontSize="12" fontWeight="600" textAnchor="middle">LIFT POINT</text>
        </g>
      </g>

      {/* OVERLAY: lift-point pin marker */}
      <g opacity={s('lift')}>
        <circle cx="250" cy="200" r="9" fill={M} stroke={T} strokeWidth="2.5" />
      </g>

      {/* OVERLAY: dump trailer under tear-off zone (along house lower edge) */}
      <g opacity={s('dump')}>
        <rect x="300" y="360" width="170" height="40" rx="6" fill={EE.surface} stroke={T} strokeWidth="2.5" />
        <circle cx="332" cy="404" r="7" fill={EE.surface} stroke={T} />
        <circle cx="438" cy="404" r="7" fill={EE.surface} stroke={T} />
        <text x="385" y="384" fill={T} stroke="none" fontFamily={EE.body} fontSize="11" fontWeight="600" textAnchor="middle">DUMP TRAILER</text>
        {/* tear-off zone marker on house edge */}
        <path d="M300 350h170" stroke={M} strokeWidth="4" />
      </g>
    </svg>
  );
}

// ── House front elevation with a roaming camera reticle (scene 2) ───────────
function HouseElevation({ t }) {
  const T = EE.teal;
  // reticle positions over local 832x570 (4 elevations cycle + driveway)
  const stops = [
    { x: 416, y: 250, label: 'FRONT' },
    { x: 250, y: 250, label: 'LEFT' },
    { x: 582, y: 250, label: 'RIGHT' },
    { x: 416, y: 250, label: 'REAR' },
    { x: 416, y: 470, label: 'DRIVEWAY' },
  ];
  const per = 1.05; // seconds per shot, starts ~0.9s
  const startAt = 0.9;
  const idx = clamp(Math.floor((t - startAt) / per), 0, stops.length - 1);
  const cur = stops[Math.max(0, idx)];
  const inShot = (t - startAt) - idx * per;
  const flash = idx >= 0 && t > startAt ? clamp(1 - inShot / 0.28, 0, 1) : 0;
  const shotCount = clamp(Math.floor((t - startAt) / per) + 1, 0, 5);

  return (
    <div style={{ position: 'absolute', inset: 0 }}>
      <svg viewBox="0 0 832 570" width="832" height="570" style={{ position: 'absolute', inset: 0 }}
        fill="none" stroke={T} strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round">
        {/* ground */}
        <path d="M70 430h692" stroke={EE.border} strokeWidth="2" />
        {/* house body */}
        <rect x="270" y="250" width="292" height="180" rx="4" fill={EE.surface} />
        {/* roof */}
        <path d="M250 250l166-96 166 96" fill={EE.surface} />
        {/* door */}
        <rect x="392" y="330" width="48" height="100" rx="3" />
        {/* windows */}
        <rect x="306" y="300" width="56" height="46" rx="3" />
        <rect x="470" y="300" width="56" height="46" rx="3" />
        {/* chimney */}
        <path d="M500 196v-30h26v46" />
        {/* driveway */}
        <path d="M300 430l-40 60h180l24-60" stroke={EE.border} strokeWidth="2" fill="none" />
      </svg>

      {/* shutter flash */}
      <div style={{ position: 'absolute', inset: 0, background: '#fff', opacity: flash * 0.55, pointerEvents: 'none' }} />

      {/* reticle */}
      <Reticle x={cur.x} y={cur.y} label={cur.label} opacity={t > startAt ? 1 : 0} />

      {/* captured counter */}
      <div style={{ position: 'absolute', right: 28, bottom: 24, display: 'flex', alignItems: 'center', gap: 10,
        opacity: t > startAt ? 1 : 0 }}>
        <Icon name="camera" size={22} color={EE.teal} stroke={2} />
        <span style={{ fontFamily: EE.body, fontWeight: 700, fontSize: 20, color: EE.teal, fontVariantNumeric: 'tabular-nums' }}>{shotCount} / 5</span>
      </div>
    </div>
  );
}

function Reticle({ x, y, label, opacity }) {
  const s = 132, b = 28;
  const corner = (cx, cy, dx, dy) => (
    <path d={`M${cx} ${cy + dy * b}V${cy}H${cx + dx * b}`} />
  );
  return (
    <div style={{ position: 'absolute', left: x, top: y, transform: 'translate(-50%,-50%)', opacity,
      transition: 'left 360ms cubic-bezier(0.2,0.7,0.2,1), top 360ms cubic-bezier(0.2,0.7,0.2,1)' }}>
      <svg width={s} height={s} viewBox={`0 0 ${s} ${s}`} fill="none" stroke={EE.mint} strokeWidth="4" strokeLinecap="round"
        style={{ position: 'absolute', left: -s / 2, top: -s / 2 }}>
        {corner(8, 8, 1, 1)}
        {corner(s - 8, 8, -1, 1)}
        {corner(8, s - 8, 1, -1)}
        {corner(s - 8, s - 8, -1, -1)}
      </svg>
      <div style={{ position: 'absolute', left: -s / 2, top: s / 2 + 6, width: s, textAlign: 'center',
        fontFamily: EE.body, fontWeight: 700, fontSize: 13, letterSpacing: '0.12em', color: EE.teal }}>{label}</div>
    </div>
  );
}

// ── Roof cross-section with anchor + harness (scene 4) ──────────────────────
function RoofSection({ t }) {
  const T = EE.teal;
  const anchorP = ramp(t, 0.7, 0.6, Easing.easeOutBack);          // anchor drops to ridge
  const lineP = ramp(t, 1.5, 0.7);                                 // harness line draws
  const tagP = ramp(t, 2.4, 0.5);                                  // TIED OFF tag
  const ridge = { x: 416, y: 150 };
  const worker = { x: 300, y: 318 };
  const lineLen = Math.hypot(ridge.x - worker.x, ridge.y - (worker.y - 60));
  return (
    <div style={{ position: 'absolute', inset: 0 }}>
      <svg viewBox="0 0 832 570" width="832" height="570" style={{ position: 'absolute', inset: 0 }}
        fill="none" stroke={T} strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round">
        {/* roof triangle */}
        <path d="M120 420L416 150l296 270" fill={EE.surface} />
        <path d="M120 420h592" stroke={EE.border} strokeWidth="2" />
        {/* deck batten lines */}
        <g stroke={EE.border} strokeWidth="1.5">
          <path d="M210 360l296-180M300 360l232-140" />
        </g>
        {/* anchor at ridge */}
        <g opacity={anchorP} transform={`translate(${ridge.x},${ridge.y}) translate(0, ${(1 - anchorP) * -40})`}>
          <rect x="-22" y="-6" width="44" height="16" rx="4" fill={EE.mint} fillOpacity="0.25" stroke={T} strokeWidth="2.5" />
          <circle cx="0" cy="2" r="5" fill={EE.surface} stroke={T} strokeWidth="2.5" />
        </g>
        {/* harness lifeline */}
        <path d={`M${ridge.x} ${ridge.y + 4}L${worker.x} ${worker.y - 60}`} stroke={EE.mint} strokeWidth="4"
          {...draw(lineLen, lineP)} />
      </svg>
      {/* worker on slope */}
      <Figure x={worker.x - 60} y={worker.y - 130} scale={0.78} opacity={ramp(t, 0.2, 0.5)} />
      {/* TIED OFF tag */}
      <div style={{ position: 'absolute', left: worker.x - 18, top: worker.y - 168,
        opacity: tagP, transform: `scale(${0.9 + 0.1 * tagP})`, transformOrigin: 'left bottom',
        display: 'flex', alignItems: 'center', gap: 8, background: EE.mint, color: EE.teal,
        padding: '8px 14px', borderRadius: 999, fontFamily: EE.body, fontWeight: 700, fontSize: 16,
        boxShadow: '0 8px 20px rgba(4,69,76,0.18)' }}>
        <Icon name="check" size={18} color={EE.teal} stroke={3} /> TIED OFF
      </div>
    </div>
  );
}

// ── Phone with messaging UI (scene 7) ───────────────────────────────────────
function Phone({ t }) {
  const typed = "Site set, tear-off starting.";
  const charP = ramp(t, 0.7, 1.3, Easing.linear);
  const shown = typed.slice(0, Math.round(charP * typed.length));
  const photosP = ramp(t, 2.3, 0.5, Easing.easeOutBack);
  const sentP = ramp(t, 3.1, 0.5);
  return (
    <div style={{ position: 'absolute', left: '50%', top: 28, transform: 'translateX(-50%)',
      width: 320, height: 514, background: EE.surface, borderRadius: 40, border: `10px solid ${EE.teal}`,
      boxShadow: '0 24px 50px rgba(4,69,76,0.18)', overflow: 'hidden' }}>
      {/* header */}
      <div style={{ height: 64, background: EE.teal, display: 'flex', alignItems: 'center', gap: 12, padding: '0 20px' }}>
        <div style={{ width: 34, height: 34, borderRadius: 999, background: EE.mint, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
          <Icon name="users" size={18} color={EE.teal} stroke={2.4} />
        </div>
        <div style={{ lineHeight: 1.25 }}>
          <div style={{ fontFamily: EE.body, fontWeight: 700, fontSize: 14, color: '#fff', whiteSpace: 'nowrap' }}>Crew group chat</div>
          <div style={{ fontFamily: EE.body, fontWeight: 500, fontSize: 11, color: 'rgba(255,255,255,0.6)', whiteSpace: 'nowrap' }}>Office, Foreman, Crew</div>
        </div>
      </div>
      {/* thread */}
      <div style={{ padding: 18, display: 'flex', flexDirection: 'column', alignItems: 'flex-end', gap: 10 }}>
        <div style={{ maxWidth: 230, background: EE.mint, color: EE.teal, padding: '12px 16px',
          borderRadius: '16px 16px 4px 16px', fontFamily: EE.body, fontWeight: 600, fontSize: 16, minHeight: 22 }}>
          {shown}<span style={{ opacity: charP < 1 ? (Math.floor(t * 3) % 2 ? 1 : 0) : 0 }}>|</span>
        </div>
        {/* two photo thumbnails */}
        <div style={{ display: 'flex', gap: 8, opacity: photosP, transform: `translateY(${(1 - photosP) * 10}px)` }}>
          {[0, 1].map((i) => (
            <div key={i} style={{ width: 90, height: 70, borderRadius: 12, background: EE.teal05,
              border: `1px solid ${EE.border}`, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
              <Icon name="camera" size={22} color={EE.teal} stroke={2} />
            </div>
          ))}
        </div>
        <div style={{ display: 'flex', alignItems: 'center', gap: 6, opacity: sentP,
          fontFamily: EE.body, fontWeight: 600, fontSize: 11, color: EE.muted }}>
          Delivered 8:12 AM <Icon name="check" size={13} color={EE.mint} stroke={3} />
        </div>
      </div>
    </div>
  );
}

Object.assign(window, { VisualStage, SitePlan, HouseElevation, Reticle, RoofSection, Phone, draw });
