// mockups.jsx — Real app screenshots in browser-style frames.
// All screenshots are landscape/square — no phone frame.

const SCREENSHOTS = {
  dashboard:         '../assets/img/app-screenshots/Dashboard.jpg',
  operations:        '../assets/img/app-screenshots/Operations.jpg',
  pointer:           '../assets/img/app-screenshots/Pointer.jpg',
  mescomptes:        '../assets/img/app-screenshots/MesComptes.jpg',
  gestionapp:        '../assets/img/app-screenshots/GestionApp.jpg',
  parametre:         '../assets/img/app-screenshots/Parametre.jpg',
  nouvelleoperation: '../assets/img/app-screenshots/NouvelleOperation.jpg',
  recurrences:       '../assets/img/app-screenshots/Recurrences.jpg',
  budget:            '../assets/img/app-screenshots/Budget.jpg',
};

// Real pixel ratios (w/h) per screenshot
const RATIOS = {
  dashboard:         3526 / 2246,   // 1.570
  operations:        2788 / 2546,   // 1.095
  pointer:           2656 / 1760,   // 1.509
  mescomptes:        2718 / 2550,   // 1.066
  gestionapp:        2304 / 2302,   // 1.001
  parametre:         2706 / 1360,   // 1.990
  nouvelleoperation: 2624 / 2390,   // 1.098
  recurrences:       2722 / 1282,   // 2.123
  budget:            694  / 1052,   // 0.660 — portrait mobile
};

// ── Browser/app frame ───────────────────────────────────────────
// Shows a screenshot at its natural ratio inside a macOS-style chrome.
function AppFrame({ src, alt, width, style }) {
  const key = Object.keys(SCREENSHOTS).find(k => SCREENSHOTS[k] === src) || 'dashboard';
  const ratio = RATIOS[key] || 1.5;
  const w = width || '100%';
  return (
    <div style={{
      width: w,
      background: '#1a1b1f',
      borderRadius: 12,
      overflow: 'hidden',
      boxShadow: [
        '0 40px 80px -30px rgba(10,15,40,0.5)',
        '0 8px 24px -8px rgba(10,15,40,0.25)',
        '0 0 0 1px rgba(255,255,255,0.06) inset',
      ].join(', '),
      flexShrink: 0,
      ...style,
    }}>
      {/* Chrome bar */}
      <div style={{
        height: 28,
        background: '#26272c',
        borderBottom: '1px solid rgba(255,255,255,0.06)',
        display: 'flex',
        alignItems: 'center',
        padding: '0 12px',
        gap: 6,
        flexShrink: 0,
      }}>
        <div style={{ width: 10, height: 10, borderRadius: '50%', background: '#ff5f57' }} />
        <div style={{ width: 10, height: 10, borderRadius: '50%', background: '#febc2e' }} />
        <div style={{ width: 10, height: 10, borderRadius: '50%', background: '#28c840' }} />
        <div style={{
          flex: 1,
          marginLeft: 10,
          height: 16,
          background: 'rgba(255,255,255,0.06)',
          borderRadius: 4,
          maxWidth: 220,
        }} />
      </div>
      {/* Screenshot at natural ratio */}
      <div style={{
        width: '100%',
        aspectRatio: String(ratio),
        background: '#0e0f12',
        overflow: 'hidden',
      }}>
        {src && (
          <img
            src={src}
            alt={alt || ''}
            style={{
              width: '100%',
              height: '100%',
              objectFit: 'contain',
              objectPosition: 'top center',
              display: 'block',
            }}
          />
        )}
      </div>
    </div>
  );
}

// ── Budget feature — shows Budget.jpg (portrait mobile) centred ──
function BudgetZoomFrame() {
  return (
    <div style={{
      display: 'flex',
      justifyContent: 'center',
      alignItems: 'center',
      padding: '16px 0',
    }}>
      {/* Phone shell around the portrait screenshot */}
      <div style={{
        width: 'clamp(200px, 28%, 280px)',
        background: '#1a1a1a',
        borderRadius: 40,
        padding: 8,
        boxShadow: [
          '0 40px 80px -20px rgba(10,15,40,0.55)',
          '0 0 0 1px rgba(255,255,255,0.05) inset',
        ].join(', '),
      }}>
        <div style={{
          borderRadius: 32,
          overflow: 'hidden',
          aspectRatio: '694/1052',
          position: 'relative',
          background: '#0e0f12',
        }}>
          {/* Dynamic island */}
          <div style={{
            position: 'absolute', top: '2.5%', left: '50%',
            transform: 'translateX(-50%)',
            width: '28%', height: '2.2%',
            background: '#1a1a1a',
            borderRadius: 999,
            zIndex: 5,
          }} />
          <img
            src={SCREENSHOTS.budget}
            alt="Budgets & alertes"
            style={{ width: '100%', height: '100%', objectFit: 'cover', display: 'block' }}
          />
        </div>
      </div>
    </div>
  );
}

// ── Feature mockup (Anticipation tabs) ─────────────────────────
function FeatureMockup({ id }) {
  if (id === 'budgets') return <BudgetZoomFrame />;
  const map = {
    projection:  SCREENSHOTS.dashboard,
    pointage:    SCREENSHOTS.pointer,
    multi:       SCREENSHOTS.mescomptes,
    recurrences: SCREENSHOTS.recurrences,
    import:      SCREENSHOTS.parametre,
  };
  const src = map[id] || SCREENSHOTS.dashboard;
  return <AppFrame src={src} alt={id} />;
}

// ── Accordion showcase — exact Apple mechanism ───────────────────
//
// How Apple keeps the section height stable:
//   • The grid uses align-items: stretch — both columns are the same height
//   • The RIGHT column has a fixed height (the image frame) → that sets the row height
//   • The LEFT column gets that same height and has overflow:hidden → accordion
//     can animate freely inside without ever making the section taller
//
// Text animation: grid-template-rows 0fr→1fr (smoother than max-height)
// Image animation: pure opacity crossfade, zero movement, fully independent

const SC_ITEMS = [
  {
    key:   'dashboard',
    label: 'Tableau de bord',
    desc:  'Solde consolidé, projection 60 jours et budgets du mois sur un seul écran. Toujours disponible, même sans connexion.',
  },
  {
    key:   'operations',
    label: 'Opérations',
    desc:  'Toutes vos opérations catégorisées, filtrables, recherchables. Importées depuis vos relevés CSV en quelques secondes.',
  },
  {
    key:   'pointer',
    label: 'Pointage',
    desc:  'Rapprochez vos opérations avec votre relevé bancaire. BudGi calcule l\'écart en temps réel — zéro si tout est en ordre.',
  },
  {
    key:   'mescomptes',
    label: 'Mes comptes',
    desc:  'Courant, livret, carte, joint — tous consolidés dans une vue unique. Les virements internes ne polluent pas vos analyses.',
  },
];

// The image frame height drives the section height.
// Left column is constrained to the same height via stretch.
const IMG_H = 'clamp(300px, 36vw, 500px)';

function ScreenshotsShowcase() {
  const [activeIdx, setActiveIdx] = React.useState(0);

  return (
    <section style={{
      background: 'var(--paper-2)',
      padding: 'clamp(64px, 9vw, 112px) 0',
    }}>
      <div
        className="container sc-grid"
        style={{
          display: 'grid',
          gridTemplateColumns: '1fr 1.55fr',
          gap: 'clamp(40px, 6vw, 96px)',
          // stretch → both columns share the same height (set by the right column)
          alignItems: 'stretch',
        }}
      >

        {/* ══ LEFT : title + accordion ══════════════════════════════
            overflow:hidden is the lock — the accordion animates freely
            inside but cannot push the section taller than the image.    */}
        <div style={{
          overflow: 'hidden',
          display: 'flex',
          flexDirection: 'column',
          justifyContent: 'center',
        }}>
          {/* Eyebrow */}
          <div style={{
            fontSize: 11, fontWeight: 600,
            letterSpacing: '0.18em', textTransform: 'uppercase',
            color: 'var(--navy)', marginBottom: 14,
            display: 'flex', alignItems: 'center', gap: 8,
          }}>
            <span style={{
              width: 5, height: 5, borderRadius: '50%',
              background: 'currentColor', display: 'inline-block',
            }} />
            L'application
          </div>

          {/* Section title */}
          <h2 style={{
            fontFamily: 'var(--serif)',
            fontSize: 'clamp(26px, 3vw, 40px)',
            fontWeight: 400, letterSpacing: '-0.024em', lineHeight: 1.08,
            color: 'var(--ink)',
            margin: '0 0 clamp(20px, 3vw, 36px)',
          }}>
            Tout ce dont<br />
            vous avez <em style={{ fontStyle: 'italic', color: 'var(--navy)' }}>besoin.</em>
          </h2>

          {/* Accordion list */}
          <div>
            {SC_ITEMS.map((item, i) => {
              const isOpen = i === activeIdx;
              return (
                <div key={item.key}>
                  <div style={{ borderTop: '1px solid var(--ink-line)' }} />

                  {/* Title row — fixed height, never changes */}
                  <button
                    onClick={() => setActiveIdx(i)}
                    aria-expanded={isOpen}
                    style={{
                      width: '100%',
                      display: 'flex',
                      justifyContent: 'space-between',
                      alignItems: 'center',
                      height: 'clamp(48px, 5.5vw, 64px)',
                      padding: 0,
                      background: 'transparent',
                      border: 'none',
                      cursor: 'pointer',
                      gap: 14,
                    }}
                  >
                    <span style={{
                      fontFamily: 'var(--serif)',
                      fontSize: 'clamp(16px, 1.7vw, 21px)',
                      fontWeight: 400,
                      letterSpacing: '-0.013em',
                      lineHeight: 1.2,
                      color: isOpen ? 'var(--ink)' : 'var(--ink-3)',
                      transition: 'color .3s ease',
                    }}>
                      {item.label}
                    </span>

                    {/* Chevron circle */}
                    <span style={{
                      width: 28, height: 28, flexShrink: 0,
                      borderRadius: '50%',
                      border: `1.5px solid ${isOpen ? 'var(--navy)' : 'var(--ink-line)'}`,
                      display: 'flex', alignItems: 'center', justifyContent: 'center',
                      color: isOpen ? 'var(--navy)' : 'var(--ink-3)',
                      transition: 'border-color .3s ease, color .3s ease',
                    }}>
                      <svg width="11" height="11" viewBox="0 0 12 12" fill="none"
                        style={{
                          transform: isOpen ? 'rotate(180deg)' : 'none',
                          transition: 'transform .38s cubic-bezier(.2,.7,.2,1)',
                        }}
                      >
                        <path d="M2 4L6 8L10 4" stroke="currentColor"
                          strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/>
                      </svg>
                    </span>
                  </button>

                  {/* Description — grid-template-rows trick (Apple's actual technique).
                      Outer div: rows 0fr→1fr drives the height animation smoothly.
                      Inner div: overflow:hidden clips during animation.
                      Opacity is independent — fades after height opens,
                      fades before height closes.                                    */}
                  <div style={{
                    display: 'grid',
                    gridTemplateRows: isOpen ? '1fr' : '0fr',
                    transition: 'grid-template-rows .38s cubic-bezier(.2,.7,.2,1)',
                  }}>
                    <div style={{ overflow: 'hidden' }}>
                      <p style={{
                        fontSize: 'clamp(13px, 1.1vw, 15px)',
                        lineHeight: 1.7,
                        color: 'var(--ink-2)',
                        margin: '0 0 clamp(12px, 1.5vw, 18px)',
                        paddingRight: 42,
                        // Opacity independent of height — fades in/out separately
                        opacity: isOpen ? 1 : 0,
                        transition: isOpen
                          ? 'opacity .28s ease .18s'   // delay: height opens first
                          : 'opacity .16s ease',        // immediate: fades before height closes
                      }}>
                        {item.desc}
                      </p>
                    </div>
                  </div>
                </div>
              );
            })}
            <div style={{ borderTop: '1px solid var(--ink-line)' }} />
          </div>
        </div>

        {/* ══ RIGHT : image frame ══════════════════════════════════
            Fixed height → drives the entire section height.
            Images crossfade with opacity only — zero movement.          */}
        <div className="sc-img-col" style={{ display: 'flex', flexDirection: 'column' }}>
          {/* macOS chrome */}
          <div style={{
            background: '#22242a',
            borderRadius: '12px 12px 0 0',
            height: 30, flexShrink: 0,
            borderBottom: '1px solid rgba(255,255,255,0.06)',
            display: 'flex', alignItems: 'center',
            padding: '0 12px', gap: 7,
            boxShadow: '0 0 0 1px rgba(255,255,255,0.05) inset',
          }}>
            <div style={{ width: 10, height: 10, borderRadius: '50%', background: '#ff5f57' }} />
            <div style={{ width: 10, height: 10, borderRadius: '50%', background: '#febc2e' }} />
            <div style={{ width: 10, height: 10, borderRadius: '50%', background: '#28c840' }} />
            <div style={{
              flex: 1, marginLeft: 10, height: 15,
              background: 'rgba(255,255,255,0.05)', borderRadius: 4, maxWidth: 200,
            }} />
          </div>

          {/* Image stage — FIXED height, images stack and crossfade, nothing moves */}
          <div style={{
            position: 'relative',
            width: '100%',
            height: IMG_H,
            flexShrink: 0,
            background: '#111318',
            borderRadius: '0 0 12px 12px',
            overflow: 'hidden',
            boxShadow: [
              '0 32px 72px -20px rgba(4,6,18,0.82)',
              '0 0 0 1px rgba(255,255,255,0.05) inset',
            ].join(', '),
          }}>
            {SC_ITEMS.map((item, i) => {
              // Wide screenshots (ratio > 1.4) fit naturally → contain
              // Nearly-square screenshots leave black bars → cover
              const fit = (RATIOS[item.key] || 1.5) > 1.4 ? 'contain' : 'cover';
              return (
                <img
                  key={item.key}
                  src={SCREENSHOTS[item.key]}
                  alt={item.label}
                  style={{
                    position: 'absolute', inset: 0,
                    width: '100%', height: '100%',
                    objectFit: fit,
                    objectPosition: 'top center',
                    display: 'block',
                    opacity: i === activeIdx ? 1 : 0,
                    transition: 'opacity .48s ease',
                  }}
                />
              );
            })}
          </div>

          {/* Dots */}
          <div style={{
            display: 'flex', justifyContent: 'center',
            gap: 7, marginTop: 16, flexShrink: 0,
          }}>
            {SC_ITEMS.map((item, i) => (
              <button
                key={item.key}
                onClick={() => setActiveIdx(i)}
                aria-label={item.label}
                style={{
                  padding: 0, border: 'none', cursor: 'pointer',
                  height: 5,
                  width: i === activeIdx ? 22 : 5,
                  borderRadius: 3,
                  background: i === activeIdx ? 'var(--navy)' : 'var(--ink-line)',
                  transition: 'width .32s cubic-bezier(.2,.7,.2,1), background .22s ease',
                }}
              />
            ))}
          </div>
        </div>

      </div>

      <style>{`
        @media (max-width: 740px) {
          .sc-grid {
            grid-template-columns: 1fr !important;
            align-items: start !important;
          }
          .sc-img-col { order: -1; }
        }
      `}</style>
    </section>
  );
}

Object.assign(window, {
  SCREENSHOTS,
  RATIOS,
  AppFrame,
  FeatureMockup,
  ScreenshotsShowcase,
});
