// js/shared.jsx — GoRently shared design components

// ─── useInView hook ──────────────────────────────────────────────────
const useInView = (threshold = 0.15) => {
  const [inView, setInView] = React.useState(false);
  const ref = React.useRef(null);
  React.useEffect(() => {
    const obs = new IntersectionObserver(([e]) => { if (e.isIntersecting) setInView(true); }, { threshold });
    if (ref.current) obs.observe(ref.current);
    return () => obs.disconnect();
  }, []);
  return [ref, inView];
};

// ─── useTweaks hook ──────────────────────────────────────────────────
const useTweaks = () => {
  const [state, setState] = React.useState(window.tweakState || {
    accent: '#3B7BF6', glowIntensity: 60, ctaVariant: 'A', heroFontSize: 54, portalLayout: 'A'
  });
  React.useEffect(() => {
    const handler = e => setState({ ...e.detail });
    window.addEventListener('tweakchange', handler);
    return () => window.removeEventListener('tweakchange', handler);
  }, []);
  return state;
};

// ─── iPhone 15 Pro Mockup ────────────────────────────────────────────
const IPhoneMockup = ({ children, scale = 1, rotation = -10 }) => {
  const s = scale;
  return (
    <div style={{
      width: 260 * s, height: 530 * s,
      background: 'linear-gradient(145deg, #1c1c1e 0%, #2a2a2c 50%, #1c1c1e 100%)',
      borderRadius: 44 * s,
      border: `${1.5 * s}px solid rgba(255,255,255,0.18)`,
      boxShadow: `
        0 ${60*s}px ${120*s}px rgba(0,0,0,0.85),
        0 0 ${80*s}px rgba(59,123,246,0.18),
        inset 0 ${1*s}px 0 rgba(255,255,255,0.14),
        inset 0 -${1*s}px 0 rgba(0,0,0,0.4)
      `,
      position: 'relative', overflow: 'hidden',
      transform: `rotate(${rotation}deg)`,
      flexShrink: 0,
    }}>
      {/* Dynamic Island */}
      <div style={{ position:'absolute', top:12*s, left:'50%', transform:'translateX(-50%)', width:88*s, height:26*s, background:'#000', borderRadius:13*s, zIndex:20 }} />
      {/* Side buttons */}
      <div style={{ position:'absolute', right:-2*s, top:100*s, width:3*s, height:60*s, background:'linear-gradient(180deg,#3a3a3c,#2c2c2e)', borderRadius:`0 ${2*s}px ${2*s}px 0` }} />
      <div style={{ position:'absolute', left:-2*s, top:80*s,  width:3*s, height:30*s, background:'linear-gradient(180deg,#3a3a3c,#2c2c2e)', borderRadius:`${2*s}px 0 0 ${2*s}px` }} />
      <div style={{ position:'absolute', left:-2*s, top:118*s, width:3*s, height:30*s, background:'linear-gradient(180deg,#3a3a3c,#2c2c2e)', borderRadius:`${2*s}px 0 0 ${2*s}px` }} />
      <div style={{ position:'absolute', left:-2*s, top:58*s,  width:3*s, height:24*s, background:'linear-gradient(180deg,#3a3a3c,#2c2c2e)', borderRadius:`${2*s}px 0 0 ${2*s}px` }} />
      {/* Screen */}
      <div style={{ position:'absolute', inset:0, borderRadius:43*s, overflow:'hidden', background:'#050d1a' }}>
        {children}
      </div>
      {/* Glass glare */}
      <div style={{ position:'absolute', inset:0, borderRadius:43*s, background:'linear-gradient(135deg, rgba(255,255,255,0.07) 0%, transparent 55%)', pointerEvents:'none', zIndex:15 }} />
    </div>
  );
};

// ─── Portal Screen content ───────────────────────────────────────────
const PortalScreen = ({ scale = 1 }) => {
  const s = scale;
  const p = n => n * s;
  return (
    <div style={{ paddingTop: p(52), height:'100%', overflow:'hidden', background:'linear-gradient(180deg, #050d1a 0%, #0a1628 100%)', fontFamily:'Inter,sans-serif' }}>
      {/* Status bar */}
      <div style={{ display:'flex', justifyContent:'space-between', alignItems:'center', padding:`0 ${p(20)}px`, marginBottom:p(14), fontSize:p(9.5), color:'#fff' }}>
        <span style={{ fontWeight:700 }}>9:41</span>
        <div style={{ display:'flex', gap:p(4), alignItems:'center' }}>
          <span style={{ fontSize:p(8) }}>▪▪▪▪</span>
          <span style={{ fontSize:p(8) }}>WiFi</span>
          <span style={{ fontSize:p(9) }}>🔋</span>
        </div>
      </div>
      {/* Welcome */}
      <div style={{ padding:`0 ${p(14)}px`, marginBottom:p(12) }}>
        <div style={{ background:'linear-gradient(135deg,rgba(59,123,246,.18),rgba(59,123,246,.06))', border:'1px solid rgba(59,123,246,.25)', borderRadius:p(14), padding:`${p(12)}px ${p(14)}px` }}>
          <div style={{ fontSize:p(13), fontWeight:700, color:'#fff', marginBottom:p(3) }}>Bienvenue, Sophie 👋</div>
          <div style={{ fontSize:p(9.5), color:'#94A3B8' }}>Appartement Oberkampf · Paris 11e</div>
        </div>
      </div>
      {/* WiFi */}
      <div style={{ padding:`0 ${p(14)}px`, marginBottom:p(8) }}>
        <div style={{ background:'rgba(255,255,255,.04)', border:'1px solid rgba(255,255,255,.08)', borderRadius:p(12), padding:`${p(10)}px ${p(12)}px`, display:'flex', alignItems:'center', gap:p(10) }}>
          <div style={{ width:p(28), height:p(28), background:'rgba(59,123,246,.2)', borderRadius:p(8), display:'flex', alignItems:'center', justifyContent:'center', fontSize:p(14) }}>📶</div>
          <div style={{ flex:1 }}>
            <div style={{ fontSize:p(9), color:'#94A3B8' }}>WiFi</div>
            <div style={{ fontSize:p(11), color:'#fff', fontWeight:700 }}>GoRently_5G</div>
          </div>
          <div style={{ background:'rgba(59,123,246,.2)', color:'#3B7BF6', borderRadius:p(6), padding:`${p(3)}px ${p(8)}px`, fontSize:p(9), fontWeight:700 }}>Copier</div>
        </div>
      </div>
      {/* Guide local */}
      <div style={{ padding:`0 ${p(14)}px`, marginBottom:p(8) }}>
        <div style={{ fontSize:p(9), color:'#94A3B8', marginBottom:p(6), fontWeight:600, textTransform:'uppercase', letterSpacing:'0.5px' }}>Guide local</div>
        <div style={{ display:'flex', gap:p(6) }}>
          {['🍕 Restos','🍹 Cafés','🛒 Courses'].map((item,i) => (
            <div key={i} style={{ background:'rgba(255,255,255,.04)', border:'1px solid rgba(255,255,255,.08)', borderRadius:p(10), padding:`${p(6)}px ${p(9)}px`, fontSize:p(9.5), color:'#fff', whiteSpace:'nowrap' }}>{item}</div>
          ))}
        </div>
      </div>
      {/* Upsell */}
      <div style={{ padding:`0 ${p(14)}px` }}>
        <div style={{ background:'linear-gradient(135deg,rgba(59,123,246,.2),rgba(59,123,246,.08))', border:'1px solid rgba(59,123,246,.3)', borderRadius:p(12), padding:`${p(11)}px ${p(12)}px`, display:'flex', alignItems:'center', gap:p(10) }}>
          <span style={{ fontSize:p(20) }}>🚗</span>
          <div style={{ flex:1 }}>
            <div style={{ fontSize:p(11), fontWeight:700, color:'#fff' }}>Airport Transfer</div>
            <div style={{ fontSize:p(9), color:'#94A3B8' }}>Depuis 35€</div>
          </div>
          <div style={{ background:'#3B7BF6', color:'#fff', borderRadius:p(8), padding:`${p(5)}px ${p(10)}px`, fontSize:p(9.5), fontWeight:700 }}>Réserver</div>
        </div>
      </div>
    </div>
  );
};

// ─── macOS Browser Window ────────────────────────────────────────────
const BrowserWindow = ({ children, width = 360, title = 'airbnb.com', redTint = false }) => (
  <div style={{ width, background: redTint ? 'rgba(15,10,10,0.92)' : 'rgba(10,15,30,0.92)', backdropFilter:'blur(20px)', border: redTint ? '1px solid rgba(239,68,68,.22)' : '1px solid rgba(255,255,255,.1)', borderRadius:12, overflow:'hidden', boxShadow:'0 30px 80px rgba(0,0,0,.65)' }}>
    <div style={{ background: redTint ? 'rgba(239,68,68,.08)' : 'rgba(255,255,255,.04)', borderBottom:'1px solid rgba(255,255,255,.06)', padding:'10px 14px', display:'flex', alignItems:'center', gap:8 }}>
      <div style={{ display:'flex', gap:6 }}>
        <div style={{ width:12,height:12,borderRadius:'50%',background:'#ff5f57' }} />
        <div style={{ width:12,height:12,borderRadius:'50%',background:'#ffbd2e' }} />
        <div style={{ width:12,height:12,borderRadius:'50%',background:'#28c840' }} />
      </div>
      <div style={{ flex:1, textAlign:'center', fontSize:11, color:'#94A3B8', background:'rgba(255,255,255,.05)', borderRadius:6, padding:'3px 10px', marginLeft:8 }}>{title}</div>
    </div>
    {children}
  </div>
);

// ─── Airbnb Calendar visual ──────────────────────────────────────────
const AirbnbCalendar = () => {
  const days = ['L','M','M','J','V','S','D'];
  const booked = new Set([2,3,9,16,22,23,29]);
  const nums = Array.from({length:30},(_,i)=>i+1);
  return (
    <div style={{ padding:14 }}>
      <div style={{ display:'flex', justifyContent:'space-between', alignItems:'center', marginBottom:10 }}>
        <span style={{ fontSize:12, fontWeight:700, color:'#fff' }}>Juin 2025</span>
        <span style={{ fontSize:16 }}>😮</span>
      </div>
      <div style={{ display:'grid', gridTemplateColumns:'repeat(7,1fr)', gap:3 }}>
        {days.map((d,i) => <div key={i} style={{ textAlign:'center', fontSize:9, color:'#4A6080', padding:'3px 0' }}>{d}</div>)}
        {/* offset for month start (Wednesday=2) */}
        {[0,1].map(i => <div key={'e'+i}/>)}
        {nums.map(n => (
          <div key={n} style={{ textAlign:'center', fontSize:10, padding:'5px 2px', borderRadius:4, background: booked.has(n) ? 'rgba(59,123,246,.28)' : 'rgba(239,68,68,.07)', color: booked.has(n) ? '#7BA8F6' : '#4A6080', border: booked.has(n) ? '1px solid rgba(59,123,246,.3)' : '1px solid rgba(239,68,68,.12)' }}>
            {n}
          </div>
        ))}
      </div>
      <div style={{ marginTop:10, padding:'5px 10px', background:'rgba(239,68,68,.12)', borderRadius:6, fontSize:10, color:'#EF4444', textAlign:'center' }}>
        18 nuits réservées sur 30 disponibles
      </div>
    </div>
  );
};

// ─── PDF / Audit Card ────────────────────────────────────────────────
const PDFCard = ({ rotation = -3, width = 200 }) => (
  <div style={{ width, background:'#fff', borderRadius:12, overflow:'hidden', boxShadow:'0 20px 60px rgba(0,0,0,.55)', transform:`rotate(${rotation}deg)`, position:'relative', flexShrink:0 }}>
    <div style={{ background:'linear-gradient(135deg,#3B7BF6,#1a56db)', padding:'14px 16px' }}>
      <div style={{ fontSize:10, fontWeight:600, color:'rgba(255,255,255,.7)', marginBottom:2 }}>GoRently</div>
      <div style={{ fontSize:13, fontWeight:800, color:'#fff', lineHeight:1.3 }}>Audit Setup &amp;<br/>Opportunités</div>
    </div>
    <div style={{ padding:'12px 14px', background:'#fff' }}>
      {[82,60,90,52,70].map((w,i) => <div key={i} style={{ height:6, width:w+'%', background:i===0?'#94A3B8':'#E2E8F0', borderRadius:3, marginBottom:8 }} />)}
      <div style={{ display:'flex', gap:8, marginTop:10, alignItems:'center' }}>
        <div style={{ width:28,height:28, background:'#EF4444', borderRadius:6, display:'flex', alignItems:'center', justifyContent:'center', fontSize:13, color:'#fff', fontWeight:700 }}>▶</div>
        <div>
          <div style={{ fontSize:9, color:'#94A3B8', marginBottom:1 }}>Walkthrough vidéo inclus</div>
          <div style={{ fontSize:10, fontWeight:700, color:'#1a202c' }}>Analyse complète</div>
        </div>
      </div>
    </div>
    <div style={{ position:'absolute', bottom:8, right:10, background:'#EF4444', color:'#fff', fontSize:8, fontWeight:800, padding:'2px 6px', borderRadius:4, letterSpacing:'.5px' }}>PDF</div>
  </div>
);

// ─── CTA Button ──────────────────────────────────────────────────────
const CTAButton = ({ children, onClick, size = 'default', fullWidth = false, style: extra = {}, className = '' }) => {
  const [hov, setHov] = React.useState(false);
  const { accent } = useTweaks();
  const sizes = { default:{padding:'15px 34px',fontSize:16}, large:{padding:'19px 48px',fontSize:18} };
  const sz = sizes[size] || sizes.default;
  const sizeClass = size === 'large' ? 'cta-btn-large' : '';
  return (
    <button
      onClick={onClick}
      onMouseEnter={() => setHov(true)}
      onMouseLeave={() => setHov(false)}
      className={[sizeClass, className].filter(Boolean).join(' ')}
      style={{
        background:`linear-gradient(135deg, ${accent} 0%, #1a4fd6 100%)`,
        color:'#fff', border:'none', borderRadius:14,
        padding:sz.padding, fontSize:sz.fontSize,
        fontWeight:700, fontFamily:'Plus Jakarta Sans, sans-serif',
        cursor:'pointer', position:'relative', overflow:'hidden',
        width: fullWidth ? '100%' : 'auto',
        boxShadow: hov ? `0 0 70px ${accent}99, 0 8px 32px ${accent}55` : `0 0 40px ${accent}55, 0 4px 20px ${accent}33`,
        transform: hov ? 'scale(1.02) translateY(-2px)' : 'scale(1)',
        transition:'all .25s cubic-bezier(.34,1.56,.64,1)',
        ...extra,
      }}
    >
      <div style={{ position:'absolute', top:0, left:0, right:0, bottom:0, background:'linear-gradient(90deg,transparent 0%,rgba(255,255,255,.18) 50%,transparent 100%)', animation:'shimmer 3s infinite', pointerEvents:'none' }} />
      <span style={{ position:'relative', zIndex:1 }}>{children}</span>
    </button>
  );
};

// ─── Pill Badge ──────────────────────────────────────────────────────
const PillBadge = ({ children }) => {
  const { accent } = useTweaks();
  return (
    <div style={{ display:'inline-flex', alignItems:'center', gap:8, background:`${accent}18`, border:`1px solid ${accent}40`, borderRadius:100, padding:'6px 16px', fontSize:12, fontWeight:600, color:`${accent}DD`, letterSpacing:'.3px' }}>
      <div style={{ width:6, height:6, borderRadius:'50%', background:accent, animation:'pulseDot 2s ease-in-out infinite' }} />
      {children}
    </div>
  );
};

// ─── Section wrapper ─────────────────────────────────────────────────
const Section = ({ children, id, bg = '#0A1628', style: extra = {} }) => (
  <section id={id} data-screen-label={id} style={{ background:bg, position:'relative', ...extra }}>
    {children}
  </section>
);

// ─── Glow Orb ────────────────────────────────────────────────────────
const GlowOrb = ({ color = '#3B7BF6', size = 400, opacity = 0.12, top, left, right, bottom }) => (
  <div style={{ position:'absolute', width:size, height:size, borderRadius:'50%', background:`radial-gradient(circle, ${color} 0%, transparent 70%)`, opacity, top, left, right, bottom, filter:`blur(${size/4}px)`, pointerEvents:'none', zIndex:0 }} />
);

// ─── Dot grid background ─────────────────────────────────────────────
const DotGrid = () => (
  <div style={{ position:'absolute', inset:0, backgroundImage:'radial-gradient(circle, rgba(59,123,246,.12) 1px, transparent 1px)', backgroundSize:'32px 32px', pointerEvents:'none', zIndex:0 }} />
);

// ─── Radar animation ─────────────────────────────────────────────────
const RadarIcon = () => {
  const { accent } = useTweaks();
  return (
    <div style={{ position:'relative', width:64, height:64, margin:'0 auto 20px' }}>
      {[0,1,2].map(i => (
        <div key={i} style={{ position:'absolute', inset:0, border:`1.5px solid ${accent}`, borderRadius:'50%', animation:`radarRing ${1.8+i*.6}s ease-out infinite`, animationDelay:`${i*.6}s` }} />
      ))}
      <div style={{ position:'absolute', inset:'50%', transform:'translate(-50%,-50%)', width:20, height:20, background:`linear-gradient(135deg,${accent},#93B8FF)`, borderRadius:'50%', display:'flex', alignItems:'center', justifyContent:'center', fontSize:10 }}>📡</div>
    </div>
  );
};

Object.assign(window, {
  useInView, useTweaks,
  IPhoneMockup, PortalScreen,
  BrowserWindow, AirbnbCalendar,
  PDFCard, CTAButton, PillBadge,
  Section, GlowOrb, DotGrid, RadarIcon,
});
