// scene.jsx — scheduling setup animated visual
// Restyled to match Scheduling Graphic palette & materials.

const INK       = '#0f1a2e';
const INK_SOFT  = '#5b6a82';
const INK_MUTE  = '#8a96ab';
const MUTED     = '#e7ebf2';
const MUTED_2   = '#f3f5f9';
const BORDER    = '#dfe4ec';
const BG        = '#ffffff';

const BLUE       = 'oklch(60% 0.16 245)';
const BLUE_DEEP  = 'oklch(44% 0.16 245)';
const BLUE_SOFT  = 'oklch(95% 0.035 245)';
const BLUE_MID   = 'oklch(88% 0.07 245)';
const BLUE_INK   = 'oklch(38% 0.14 245)';

// ── Root ───────────────────────────────────────────────────────────────────
function Scene() {
  const t = useTime();

  return (
    <div style={{
      position: 'absolute', inset: 0, overflow: 'hidden',
      fontFamily: 'Inter, system-ui, -apple-system, sans-serif',
      color: INK,
    }}>
      <Backdrop />
      <ConfigCard t={t} />
    </div>
  );
}

// ── Backdrop: plain ──────────────────────────────────────────────────────
function Backdrop() {
  return null;
}

// ── Meta label (top-left) ─────────────────────────────────────────────────
function MetaLabel({ t }) {
  const op = animate({ from: 0, to: 1, start: 0.2, end: 0.8 })(t);
  return (
    <div style={{
      position: 'absolute',
      top: 52, left: 80,
      opacity: op,
      display: 'flex', alignItems: 'center', gap: 12,
      fontFamily: 'JetBrains Mono, ui-monospace, monospace',
      fontSize: 11,
      letterSpacing: '0.14em',
      textTransform: 'uppercase',
      color: INK_MUTE,
      fontWeight: 600,
    }}>
      <span style={{
        width: 8, height: 8, borderRadius: 4,
        background: BLUE,
        boxShadow: `0 0 0 3px color-mix(in oklch, transparent, ${BLUE} 20%)`,
      }}/>
      <span>Scheduling · Setup</span>
      <span style={{ color: MUTED }}>/</span>
      <span>v2.4</span>
    </div>
  );
}

// ── Config card ───────────────────────────────────────────────────────────
function ConfigCard({ t }) {
  const cardY = interpolate([0, 0.9], [22, 0], Easing.easeOutCubic)(t);
  const cardOp = animate({ from: 0, to: 1, start: 0, end: 0.7 })(t);

  return (
    <div style={{
      position: 'absolute',
      top: 140, left: '50%',
      marginLeft: -420,
      width: 840,
      opacity: cardOp,
      transform: `translateY(${cardY}px)`,
      background: BG,
      borderRadius: 16,
      boxShadow: '0 24px 48px -16px rgba(15,26,46,0.18), 0 0 0 1px rgba(15,26,46,0.05)',
    }}>
      {/* thin accent bar — matches graphic's identity-bar language */}
      <div style={{ height: 4, background: BLUE, borderRadius: '16px 16px 0 0' }}/>

      {/* Header */}
      <div style={{
        padding: '18px 24px',
        borderBottom: `1px solid ${MUTED}`,
        display: 'flex', alignItems: 'center', justifyContent: 'space-between',
      }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
          <div style={{
            width: 32, height: 32, borderRadius: 8,
            background: BLUE_SOFT,
            display: 'flex', alignItems: 'center', justifyContent: 'center',
            color: BLUE_INK,
          }}>
            <svg width="16" height="16" viewBox="0 0 24 24" fill="none">
              <rect x="3.5" y="5" width="17" height="15" rx="2.5" stroke="currentColor" strokeWidth="1.8"/>
              <path d="M3.5 9.5h17M8 3v4M16 3v4" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round"/>
            </svg>
          </div>
          <div>
            <div style={{
              fontSize: 10,
              fontWeight: 600,
              color: INK_MUTE,
              letterSpacing: '0.14em',
              textTransform: 'uppercase',
            }}>Meeting Type</div>
            <div style={{ fontSize: 15, fontWeight: 600, color: INK, letterSpacing: '-0.01em', marginTop: 2 }}>
              Intro Call · 30 min
            </div>
          </div>
        </div>
        <div style={{ display: 'flex', gap: 4 }}>
          <TabChip label="General" />
          <TabChip label="Availability" active />
          <TabChip label="Pages" />
          <TabChip label="Notifications" />
        </div>
      </div>

      {/* Body */}
      <div style={{ padding: '22px 24px 26px', display: 'flex', flexDirection: 'column', gap: 18 }}>
        <DurationRow t={t} />
        <StartTimeRow t={t} />
        <DateRangeRow t={t} />
        <BufferRow t={t} />
      </div>
    </div>
  );
}

function TabChip({ label, active }) {
  return (
    <div style={{
      padding: '6px 12px',
      fontSize: 12,
      fontWeight: active ? 600 : 500,
      color: active ? INK : INK_MUTE,
      borderRadius: 7,
      background: active ? MUTED_2 : 'transparent',
      letterSpacing: '-0.005em',
    }}>
      {label}
    </div>
  );
}

function Row({ label, sublabel, y, op, children, zIndex }) {
  return (
    <div style={{
      display: 'grid',
      gridTemplateColumns: '200px 1fr',
      alignItems: 'center',
      gap: 20,
      transform: `translateY(${y}px)`,
      opacity: op,
      position: 'relative',
      zIndex: zIndex || 'auto',
    }}>
      <div>
        <div style={{ fontSize: 13, fontWeight: 600, color: INK, letterSpacing: '-0.005em' }}>{label}</div>
        <div style={{ fontSize: 11.5, color: INK_MUTE, marginTop: 2 }}>{sublabel}</div>
      </div>
      <div>{children}</div>
    </div>
  );
}

// Duration pills
function DurationRow({ t }) {
  const y = interpolate([0.3, 1.0], [14, 0], Easing.easeOutCubic)(t);
  const op = animate({ from: 0, to: 1, start: 0.3, end: 0.9 })(t);

  let selected = 1;
  if (t >= 2.2 && t < 3.0) selected = 0;
  else if (t >= 3.0) selected = 1;

  return (
    <Row label="Duration" sublabel="How long is this meeting?" y={y} op={op}>
      <div style={{ display: 'flex', gap: 8 }}>
        {['15 min', '30 min', '60 min'].map((v, i) => (
          <DurationPill key={v} label={v} active={selected === i} />
        ))}
      </div>
    </Row>
  );
}

function DurationPill({ label, active }) {
  return (
    <div style={{
      padding: '9px 16px',
      minWidth: 72,
      textAlign: 'center',
      fontSize: 13,
      fontWeight: 600,
      borderRadius: 999,
      color: active ? '#fff' : INK_SOFT,
      background: active ? BLUE : MUTED_2,
      border: `1px solid ${active ? 'transparent' : BORDER}`,
      boxShadow: active ? `0 6px 14px -4px color-mix(in oklch, transparent, ${BLUE} 40%)` : 'none',
      transition: 'all 260ms cubic-bezier(0.22, 1, 0.36, 1)',
      letterSpacing: '-0.005em',
    }}>
      {label}
    </div>
  );
}

// Segmented start-time
function StartTimeRow({ t }) {
  const y = interpolate([0.5, 1.2], [14, 0], Easing.easeOutCubic)(t);
  const op = animate({ from: 0, to: 1, start: 0.5, end: 1.1 })(t);

  let selected = 0;
  if (t >= 3.4) selected = 1;

  return (
    <Row label="Start time" sublabel="Minimum gap between slots" y={y} op={op}>
      <div style={{
        display: 'flex',
        padding: 3,
        background: MUTED_2,
        borderRadius: 10,
        border: `1px solid ${BORDER}`,
        position: 'relative',
        width: 240,
      }}>
        <div style={{
          position: 'absolute',
          top: 3, bottom: 3,
          left: 3 + selected * (234 / 4),
          width: 234 / 4,
          background: '#fff',
          borderRadius: 7,
          boxShadow: '0 1px 3px rgba(15,26,46,0.08), 0 0 0 1px rgba(15,26,46,0.04)',
          transition: 'left 320ms cubic-bezier(0.22, 1, 0.36, 1)',
        }}/>
        {['10', '15', '30', '60'].map((v, i) => (
          <div key={v} style={{
            flex: 1,
            textAlign: 'center',
            padding: '7px 0',
            fontSize: 13,
            fontWeight: 600,
            color: selected === i ? INK : INK_MUTE,
            position: 'relative', zIndex: 1,
            letterSpacing: '-0.005em',
          }}>
            {v}
          </div>
        ))}
      </div>
    </Row>
  );
}

// Date range dropdown
function DateRangeRow({ t }) {
  const y = interpolate([0.7, 1.4], [14, 0], Easing.easeOutCubic)(t);
  const op = animate({ from: 0, to: 1, start: 0.7, end: 1.3 })(t);

  // Cycle through options so there's still some life to the animation.
  // 0..3.8s → "60 days", 3.8..4.8s → "90 days", then stays.
  const options = ['30 days', '60 days', '90 days', 'Custom range'];
  let selectedIdx = 1;
  if (t >= 4.3) selectedIdx = 2;

  const open = t >= 3.8 && t < 4.3;
  const openProg = open ? Easing.easeOutCubic(clamp((t - 3.8) / 0.25, 0, 1)) : 0;

  return (
    <Row label="Date range" sublabel="Rolling window of availability" y={y} op={op} zIndex={30}>
      <div style={{ position: 'relative', width: 260 }}>
        <div style={{
          display: 'flex', alignItems: 'center', justifyContent: 'space-between',
          padding: '10px 14px',
          background: '#fff',
          border: `1px solid ${open ? BLUE : BORDER}`,
          borderRadius: 9,
          fontSize: 13,
          fontWeight: 600,
          color: INK,
          letterSpacing: '-0.005em',
          boxShadow: open ? `0 0 0 3px color-mix(in oklch, transparent, ${BLUE} 18%)` : 'none',
          transition: 'border-color 200ms, box-shadow 200ms',
        }}>
          <span>{options[selectedIdx]}</span>
          <svg width="14" height="14" viewBox="0 0 24 24" fill="none" style={{
            transform: `rotate(${open ? 180 : 0}deg)`,
            transition: 'transform 240ms cubic-bezier(0.22, 1, 0.36, 1)',
            color: INK_MUTE,
          }}>
            <path d="M6 9l6 6 6-6" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
          </svg>
        </div>

        {/* Dropdown menu */}
        {openProg > 0 && (
          <div style={{
            position: 'absolute',
            top: 'calc(100% + 6px)', left: 0, right: 0,
            background: '#fff',
            border: `1px solid ${BORDER}`,
            borderRadius: 10,
            boxShadow: '0 18px 32px -10px rgba(15,26,46,0.18), 0 0 0 1px rgba(15,26,46,0.04)',
            padding: 4,
            zIndex: 20,
            opacity: openProg,
            transform: `translateY(${(1 - openProg) * -4}px)`,
            transformOrigin: 'top center',
          }}>
            {options.map((opt, i) => {
              // Mid-transition, the hover lands on "90 days" (idx 2)
              const hovered = i === 2;
              const isSel = i === selectedIdx;
              return (
                <div key={opt} style={{
                  padding: '9px 12px',
                  fontSize: 13,
                  fontWeight: isSel ? 600 : 500,
                  color: INK,
                  background: hovered ? MUTED_2 : 'transparent',
                  borderRadius: 7,
                  display: 'flex', alignItems: 'center', justifyContent: 'space-between',
                }}>
                  <span>{opt}</span>
                  {isSel && (
                    <svg width="14" height="14" viewBox="0 0 24 24" fill="none" style={{ color: BLUE }}>
                      <path d="M5 12l5 5L20 7" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round"/>
                    </svg>
                  )}
                </div>
              );
            })}
          </div>
        )}
      </div>
    </Row>
  );
}

// Buffer toggle
function BufferRow({ t }) {
  const y = interpolate([0.9, 1.6], [14, 0], Easing.easeOutCubic)(t);
  const op = animate({ from: 0, to: 1, start: 0.9, end: 1.5 })(t);
  const toggleOn = t >= 5.4;

  return (
    <Row label="Buffer time" sublabel="Pad before & after meetings" y={y} op={op}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 14 }}>
        <Toggle on={toggleOn} />
        <div style={{
          padding: '7px 14px',
          background: MUTED_2,
          border: `1px solid ${BORDER}`,
          borderRadius: 8,
          fontSize: 13,
          color: toggleOn ? INK : INK_MUTE,
          opacity: toggleOn ? 1 : 0.7,
          transition: 'all 260ms',
          fontWeight: 600,
          letterSpacing: '-0.005em',
        }}>
          10 min
        </div>
      </div>
    </Row>
  );
}

function Toggle({ on }) {
  return (
    <div style={{
      width: 44, height: 24,
      borderRadius: 12,
      background: on ? BLUE : BORDER,
      position: 'relative',
      transition: 'background 300ms cubic-bezier(0.22, 1, 0.36, 1)',
      padding: 2,
      boxShadow: on
        ? `inset 0 1px 2px color-mix(in oklch, transparent, ${BLUE_DEEP} 30%)`
        : 'inset 0 1px 2px rgba(15,26,46,0.08)',
    }}>
      <div style={{
        position: 'absolute',
        top: 3, left: on ? 23 : 3,
        width: 18, height: 18,
        borderRadius: 9,
        background: '#fff',
        boxShadow: '0 2px 4px rgba(15,26,46,0.18)',
        transition: 'left 300ms cubic-bezier(0.22, 1, 0.36, 1)',
      }}/>
    </div>
  );
}

// ── Preview card ──────────────────────────────────────────────────────────
function PreviewCard({ t }) {
  const slide = animate({ from: 60, to: 0, start: 1.4, end: 2.3, ease: Easing.easeOutCubic })(t);
  const op = animate({ from: 0, to: 1, start: 1.4, end: 2.1 })(t);

  return (
    <div style={{
      position: 'absolute',
      top: 290, right: 60,
      width: 340,
      transform: `translateX(${slide}px)`,
      opacity: op,
      background: BG,
      borderRadius: 16,
      boxShadow: '0 28px 56px -12px rgba(15,26,46,0.22), 0 0 0 1px rgba(15,26,46,0.05)',
      overflow: 'hidden',
      zIndex: 10,
    }}>
      {/* Header */}
      <div style={{
        padding: '16px 20px 14px',
        borderBottom: `1px solid ${MUTED}`,
        display: 'flex', alignItems: 'center', justifyContent: 'space-between',
      }}>
        <div>
          <div style={{
            fontSize: 10,
            fontWeight: 600,
            color: INK_MUTE,
            letterSpacing: '0.14em',
            textTransform: 'uppercase',
          }}>Live Preview</div>
          <div style={{ fontSize: 15, fontWeight: 600, color: INK, marginTop: 2, letterSpacing: '-0.01em' }}>
            Thursday, April 23
          </div>
        </div>
        <div style={{
          display: 'flex', alignItems: 'center', gap: 5,
          padding: '4px 8px',
          background: BLUE_SOFT,
          color: BLUE_INK,
          borderRadius: 6,
          fontSize: 10.5,
          fontWeight: 600,
          fontFamily: 'JetBrains Mono, ui-monospace, monospace',
          letterSpacing: '0.08em',
        }}>
          <span style={{ width: 5, height: 5, borderRadius: 3, background: BLUE }}/>
          LIVE
        </div>
      </div>

      <WeekStrip t={t} />
      <TimeSlots t={t} />
      <BookingFooter t={t} />
    </div>
  );
}

function WeekStrip({ t }) {
  const days = ['M', 'T', 'W', 'T', 'F', 'S', 'S'];
  const nums = [20, 21, 22, 23, 24, 25, 26];
  const activeIdx = 3;

  return (
    <div style={{
      padding: '14px 16px 10px',
      borderBottom: `1px solid ${MUTED}`,
      display: 'flex', gap: 4,
      justifyContent: 'space-between',
    }}>
      {days.map((d, i) => {
        const delay = 2.0 + i * 0.06;
        const entered = t >= delay;
        const eScale = entered ? Easing.easeOutBack(clamp((t - delay) / 0.4, 0, 1)) : 0;
        const isActive = i === activeIdx;
        const isPast = i < 2;
        return (
          <div key={i} style={{
            flex: 1,
            display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 4,
            padding: '8px 0',
            borderRadius: 10,
            background: isActive ? BLUE : 'transparent',
            color: isActive ? '#fff' : (isPast ? INK_MUTE : INK_SOFT),
            opacity: isPast ? 0.45 : (0.2 + 0.8 * eScale),
            transform: `scale(${0.6 + 0.4 * eScale})`,
            transformOrigin: 'center',
            boxShadow: isActive ? `0 6px 14px -4px color-mix(in oklch, transparent, ${BLUE} 40%)` : 'none',
          }}>
            <div style={{ fontSize: 10, fontWeight: 600, letterSpacing: '0.08em', opacity: 0.75 }}>
              {d}
            </div>
            <div style={{ fontSize: 14, fontWeight: 700, fontFamily: 'JetBrains Mono, ui-monospace, monospace', fontVariantNumeric: 'tabular-nums' }}>
              {nums[i]}
            </div>
          </div>
        );
      })}
    </div>
  );
}

function TimeSlots({ t }) {
  const slots = ['9:00 am', '9:30 am', '10:00 am', '11:00 am', '1:30 pm', '2:00 pm'];
  const pickedIdx = 3;

  return (
    <div style={{
      padding: '14px 16px',
      display: 'grid',
      gridTemplateColumns: '1fr 1fr',
      gap: 8,
    }}>
      {slots.map((time, i) => {
        const delay = 2.6 + i * 0.07;
        const entered = t >= delay;
        const progress = entered ? Easing.easeOutCubic(clamp((t - delay) / 0.4, 0, 1)) : 0;
        const picked = t >= 6.2 && i === pickedIdx;
        const hovered = t >= 5.8 && t < 6.2 && i === pickedIdx;

        return (
          <div key={i} style={{
            padding: '10px 14px',
            borderRadius: 9,
            border: `1px solid ${picked ? 'transparent' : BORDER}`,
            background: picked ? BLUE : (hovered ? BLUE_SOFT : '#fff'),
            color: picked ? '#fff' : INK,
            fontSize: 12.5,
            fontWeight: 600,
            fontFamily: 'JetBrains Mono, ui-monospace, monospace',
            fontVariantNumeric: 'tabular-nums',
            textAlign: 'center',
            opacity: progress,
            transform: `translateY(${(1 - progress) * 10}px)`,
            transition: 'background 200ms, border-color 200ms, color 200ms',
            boxShadow: picked ? `0 6px 14px -4px color-mix(in oklch, transparent, ${BLUE} 40%)` : 'none',
            letterSpacing: '-0.005em',
          }}>
            {time}
          </div>
        );
      })}
    </div>
  );
}

function BookingFooter({ t }) {
  const op = animate({ from: 0, to: 1, start: 3.4, end: 4.0 })(t);
  const y = interpolate([3.4, 4.0], [12, 0], Easing.easeOutCubic)(t);
  const booked = t >= 6.5;

  return (
    <div style={{
      padding: '14px 16px',
      borderTop: `1px solid ${MUTED}`,
      background: MUTED_2,
      display: 'flex', alignItems: 'center', justifyContent: 'space-between',
      opacity: op,
      transform: `translateY(${y}px)`,
    }}>
      <div style={{
        display: 'flex', alignItems: 'center', gap: 8,
        fontSize: 11.5, color: INK_SOFT,
      }}>
        <svg width="14" height="14" viewBox="0 0 16 16" fill="none">
          <circle cx="8" cy="8" r="6.5" stroke="currentColor" strokeWidth="1.4"/>
          <path d="M8 5v3l2 1.5" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round"/>
        </svg>
        <span style={{ fontFamily: 'JetBrains Mono, ui-monospace, monospace', fontWeight: 600, letterSpacing: '0.04em' }}>
          PST · 30 min
        </span>
      </div>
      <div style={{
        padding: '8px 16px',
        background: booked ? INK : BLUE,
        color: '#fff',
        borderRadius: 8,
        fontSize: 12,
        fontWeight: 600,
        letterSpacing: '-0.005em',
        display: 'flex', alignItems: 'center', gap: 6,
        transition: 'background 300ms',
        boxShadow: booked ? 'none' : `0 6px 14px -4px color-mix(in oklch, transparent, ${BLUE} 40%)`,
      }}>
        {booked && (
          <svg width="12" height="12" viewBox="0 0 12 12" fill="none">
            <path d="M2.5 6.5l2.5 2.5 5-5.5" stroke="#fff" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
          </svg>
        )}
        {booked ? 'Confirmed' : 'Book 11:00 am'}
      </div>
    </div>
  );
}

// ── Ribbon connector ──────────────────────────────────────────────────────
function Connector({ t }) {
  const draw = clamp((t - 2.2) / 1.0, 0, 1);
  const op = animate({ from: 0, to: 1, start: 2.2, end: 2.8 })(t);

  // From config card right edge near top header → preview card left edge
  const path = 'M 760 200 C 830 210, 830 340, 900 340';

  return (
    <svg style={{ position: 'absolute', inset: 0, width: '100%', height: '100%', pointerEvents: 'none', opacity: op, overflow: 'visible' }}>
      <defs>
        <linearGradient id="setup-ribbon" x1="0" y1="0" x2="1" y2="0">
          <stop offset="0%" stopColor={BLUE_MID} stopOpacity="0.4"/>
          <stop offset="100%" stopColor={BLUE} stopOpacity="1"/>
        </linearGradient>
      </defs>
      {/* soft glow */}
      <path d={path} fill="none" stroke={BLUE} strokeWidth="10" strokeLinecap="round"
        opacity={draw * 0.18} pathLength="1" strokeDasharray="1 1" strokeDashoffset={1 - draw}
        style={{ filter: 'blur(4px)' }} />
      {/* main */}
      <path d={path} fill="none" stroke="url(#setup-ribbon)" strokeWidth="3" strokeLinecap="round"
        pathLength="1" strokeDasharray="1 1" strokeDashoffset={1 - draw} />
      {/* dots */}
      {[0.33, 0.66].map((f, i) => {
        const vis = draw > f ? 1 : 0;
        const cx = 760 + (900 - 760) * f;
        const cy = 200 + (340 - 200) * f + Math.sin(f * Math.PI) * 8;
        return <circle key={i} cx={cx} cy={cy} r={3} fill={BLUE} opacity={vis} />;
      })}
    </svg>
  );
}

window.Scene = Scene;
