// ios-frame.jsx — iOS device wrapper. Only renders the chrome on wider screens
// (prototype testing). On a real phone the app fills the viewport directly.

function useIsMobile() {
  const [is, setIs] = React.useState(() =>
    typeof window !== 'undefined' && window.matchMedia('(max-width: 480px)').matches
  );
  React.useEffect(() => {
    const m = window.matchMedia('(max-width: 480px)');
    const onChange = () => setIs(m.matches);
    m.addEventListener?.('change', onChange);
    return () => m.removeEventListener?.('change', onChange);
  }, []);
  return is;
}

function IOSStatusBar({ dark = false, time = '9:41' }) {
  const c = dark ? '#fff' : '#000';
  return (
    <div style={{
      display: 'flex', alignItems: 'center', justifyContent: 'space-between',
      padding: '17px 32px 11px', boxSizing: 'border-box',
      position: 'relative', zIndex: 20, width: '100%', pointerEvents: 'none',
    }}>
      <span style={{
        fontFamily: '-apple-system, "SF Pro", system-ui', fontWeight: 590,
        fontSize: 17, lineHeight: '22px', color: c,
      }}>{time}</span>
      <div style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
        <svg width="19" height="12" viewBox="0 0 19 12">
          <rect x="0"   y="7.5" width="3.2" height="4.5" rx="0.7" fill={c}/>
          <rect x="4.8" y="5"   width="3.2" height="7"   rx="0.7" fill={c}/>
          <rect x="9.6" y="2.5" width="3.2" height="9.5" rx="0.7" fill={c}/>
          <rect x="14.4" y="0"  width="3.2" height="12"  rx="0.7" fill={c}/>
        </svg>
        <svg width="17" height="12" viewBox="0 0 17 12">
          <path d="M8.5 3.2C10.8 3.2 12.9 4.1 14.4 5.6L15.5 4.5C13.7 2.7 11.2 1.5 8.5 1.5C5.8 1.5 3.3 2.7 1.5 4.5L2.6 5.6C4.1 4.1 6.2 3.2 8.5 3.2Z" fill={c}/>
          <path d="M8.5 6.8C9.9 6.8 11.1 7.3 12 8.2L13.1 7.1C11.8 5.9 10.2 5.1 8.5 5.1C6.8 5.1 5.2 5.9 3.9 7.1L5 8.2C5.9 7.3 7.1 6.8 8.5 6.8Z" fill={c}/>
          <circle cx="8.5" cy="10.5" r="1.5" fill={c}/>
        </svg>
        <svg width="27" height="13" viewBox="0 0 27 13">
          <rect x="0.5" y="0.5" width="23" height="12" rx="3.5" stroke={c} strokeOpacity="0.4" fill="none"/>
          <rect x="2"   y="2"   width="20" height="9"  rx="2"   fill={c}/>
          <path d="M25 4.5V8.5C25.8 8.2 26.5 7.2 26.5 6.5C26.5 5.8 25.8 4.8 25 4.5Z" fill={c} fillOpacity="0.4"/>
        </svg>
      </div>
    </div>
  );
}

function IOSDevice({ children, width = 390, height = 844, dark = false }) {
  return (
    <div style={{
      width, height, borderRadius: 52, overflow: 'hidden', position: 'relative',
      background: dark ? '#000' : '#F2F2F7',
      boxShadow: '0 40px 80px rgba(0,0,0,0.22), 0 0 0 1px rgba(0,0,0,0.12), 0 0 0 8px #1a1a1a, 0 0 0 10px #444',
      fontFamily: '-apple-system, system-ui, sans-serif',
      WebkitFontSmoothing: 'antialiased',
    }}>
      <div style={{
        position: 'absolute', top: 11, left: '50%', transform: 'translateX(-50%)',
        width: 126, height: 37, borderRadius: 24, background: '#000', zIndex: 50,
      }} />
      <div style={{ position: 'absolute', top: 0, left: 0, right: 0, zIndex: 10 }}>
        <IOSStatusBar dark={dark} />
      </div>
      <div style={{ width: '100%', height: '100%' }}>
        {children}
      </div>
      <div style={{
        position: 'absolute', bottom: 0, left: 0, right: 0, zIndex: 60,
        height: 34, display: 'flex', justifyContent: 'center', alignItems: 'flex-end',
        paddingBottom: 8, pointerEvents: 'none',
      }}>
        <div style={{
          width: 139, height: 5, borderRadius: 100,
          background: dark ? 'rgba(255,255,255,0.7)' : 'rgba(0,0,0,0.25)',
        }} />
      </div>
    </div>
  );
}

// Top-level shell. On mobile: app fills the viewport directly. On desktop: wrap
// in iOS frame for prototype testing.
function AppShell({ theme, children }) {
  const isMobile = useIsMobile();

  // Faux status bar inside the app — needed on web (where we don't get a real one).
  // We still render it on mobile so the design lines up.
  const StatusBar = (
    <div style={{
      position: 'absolute', top: 0, left: 0, right: 0, zIndex: 10,
      pointerEvents: 'none',
    }}>
      <IOSStatusBar dark={theme.mode === 'dark'} />
    </div>
  );

  if (isMobile) {
    return (
      <div style={{
        position: 'fixed', inset: 0, background: theme.bg,
        color: theme.fg,
      }}>
        {StatusBar}
        <div style={{ position: 'absolute', inset: 0 }}>{children}</div>
      </div>
    );
  }

  // Desktop: iOS frame on a stage, scaled to fit small viewports.
  return (
    <div style={{
      minHeight: '100vh',
      background:
        theme.mode === 'dark'
          ? `radial-gradient(circle at 30% 0%, #1a1a22 0%, #0a0a0d 60%)`
          : `radial-gradient(circle at 30% 0%, #f7ecc8 0%, #e3d2a8 60%)`,
      display: 'flex', alignItems: 'center', justifyContent: 'center',
      padding: 24, boxSizing: 'border-box',
    }}>
      <div style={{ position: 'relative' }}>
        <IOSDevice dark={theme.mode === 'dark'}>
          <div style={{ position: 'relative', width: '100%', height: '100%', overflow: 'hidden' }}>
            {StatusBar}
            <div style={{ position: 'absolute', inset: 0 }}>{children}</div>
          </div>
        </IOSDevice>
        <div style={{
          position: 'absolute', top: -22, left: 0,
          fontFamily: '"DM Mono", monospace', fontSize: 11, letterSpacing: '0.15em',
          color: theme.mode === 'dark' ? '#fff9' : '#1a13059b', textTransform: 'uppercase',
        }}>chopstick — prototype</div>
      </div>
    </div>
  );
}

Object.assign(window, { IOSDevice, IOSStatusBar, AppShell, useIsMobile });
