// ============================================================
// DEARLY — case study support components.
// Copy source of truth: dearly-brief.md — wording is verbatim.
// Depends on globals from bubbles.jsx (useState) and
// rio-tinto-screens.jsx (ScreenPlaceholder) / imprint-screens.jsx (Mockup).
// Names are prefixed DL/Dearly to avoid clobbering imprint-case-parts.jsx's
// globals (e.g. Personas) — these are plain scripts sharing one window scope,
// not modules, so identical top-level names collide.
// ============================================================
const { useState: useStateDL } = React;

// ---- persona doodles (jade / cool blue, echoing the project's own palette) ----
function EllieDoodle() {
  return (
    <svg className="cs-doodle" viewBox="0 0 120 120" aria-hidden="true">
      <circle cx="60" cy="60" r="56" fill="#E3F3EC"></circle>
      <path d="M26 110 C32 90 45 83 60 83 C75 83 88 90 94 110" fill="#FFFFFF" stroke="#2A2433" strokeWidth="3" strokeLinecap="round"></path>
      <circle cx="60" cy="56" r="25" fill="#FFFFFF" stroke="#2A2433" strokeWidth="3"></circle>
      <path d="M35 58 C32 33 45 24 60 24 C75 24 88 33 85 58 C82 42 73 38 60 38 C47 38 38 42 35 58 Z" fill="#1F7A5C" stroke="#2A2433" strokeWidth="3" strokeLinejoin="round"></path>
      <path d="M33 46 C33 30 45 20 60 20 C75 20 87 30 87 46" fill="none" stroke="#2A2433" strokeWidth="3" strokeLinecap="round"></path>
      <circle cx="52" cy="57" r="2.6" fill="#2A2433"></circle>
      <circle cx="69" cy="57" r="2.6" fill="#2A2433"></circle>
      <path d="M54 68 C58 72 64 72 68 68" fill="none" stroke="#2A2433" strokeWidth="3" strokeLinecap="round"></path>
      <circle cx="46" cy="63" r="3" fill="#4C7EA8" opacity="0.5"></circle>
      <circle cx="75" cy="63" r="3" fill="#4C7EA8" opacity="0.5"></circle>
    </svg>
  );
}

function SamDoodle() {
  return (
    <svg className="cs-doodle" viewBox="0 0 120 120" aria-hidden="true">
      <circle cx="60" cy="60" r="56" fill="#E5EEF5"></circle>
      <path d="M26 110 C32 90 45 83 60 83 C75 83 88 90 94 110" fill="#FFFFFF" stroke="#2A2433" strokeWidth="3" strokeLinecap="round"></path>
      <circle cx="60" cy="56" r="25" fill="#FFFFFF" stroke="#2A2433" strokeWidth="3"></circle>
      <path d="M36 52 C35 33 45 25 60 25 C75 25 85 33 84 52 C80 42 72 39 60 39 C48 39 40 42 36 52 Z" fill="#4C7EA8" stroke="#2A2433" strokeWidth="3" strokeLinejoin="round"></path>
      <rect x="43" y="50" width="14" height="12" rx="5" fill="none" stroke="#2A2433" strokeWidth="3"></rect>
      <rect x="63" y="50" width="14" height="12" rx="5" fill="none" stroke="#2A2433" strokeWidth="3"></rect>
      <path d="M57 55 L63 55" stroke="#2A2433" strokeWidth="3" strokeLinecap="round"></path>
      <circle cx="50" cy="56" r="2.4" fill="#2A2433"></circle>
      <circle cx="70" cy="56" r="2.4" fill="#2A2433"></circle>
      <path d="M54 70 C58 73 64 73 68 70" fill="none" stroke="#2A2433" strokeWidth="3" strokeLinecap="round"></path>
      <circle cx="44" cy="64" r="3" fill="#1F7A5C" opacity="0.4"></circle>
      <circle cx="76" cy="64" r="3" fill="#1F7A5C" opacity="0.4"></circle>
    </svg>
  );
}

// ---- persona data (verbatim from brief) ----
const DL_PERSONAS = {
  ellie: {
    name: 'Ellie',
    short: 'The outcome-first shopper',
    bio: "She doesn't enjoy researching or comparing options, and finds traditional shopping overwhelming when faced with too many choices. If she needs a gift for her gardening-loving mum, she won't browse categories, she'll describe what she wants and expect the system to return ready-to-use options.",
  },
  sam: {
    name: 'Sam',
    short: 'The thoughtful but time-poor giver',
    bio: 'Sam doesn’t always know what he’s looking for at the start. He knows the person and the occasion, but not how to translate that into a product, the "blank page" problem. He doesn’t want endless browsing either, he wants help shaping vague intent into something concrete.',
  },
};

// ---- interactive persona switcher ----
function DearlyPersonas() {
  const [who, setWho] = useStateDL('ellie');
  const d = DL_PERSONAS[who];
  return (
    <div className="reveal">
      <div className="cs-ptabs">
        {['ellie', 'sam'].map(k => (
          <button key={k} className={'cs-ptab' + (who === k ? ' on' : '')} onClick={() => setWho(k)}>
            {k === 'ellie' ? <EllieDoodle /> : <SamDoodle />}
            <span className="pt-txt">
              <span className="pt-name">{DL_PERSONAS[k].name}</span>
              <span className="pt-role">{DL_PERSONAS[k].short}</span>
            </span>
          </button>
        ))}
      </div>
      <div className="cs-persona" key={who}>
        <div className="cs-persona-top">
          {who === 'ellie' ? <EllieDoodle /> : <SamDoodle />}
          <div className="cs-persona-id dl-persona-id">
            <div className="cs-persona-name">{d.name}</div>
            <div className="cs-persona-role">{d.short}</div>
          </div>
        </div>
        <p className="cs-persona-bio">{d.bio}</p>
      </div>
    </div>
  );
}

// ---- "at a glance" stat strip (hero) ----
function DLAtAGlance() {
  const stats = [
    { v: '1 week', l: 'From first observation to a testable prototype' },
    { v: '2', l: 'Questions tested at once, product and process' },
    { v: '3', l: 'Directions explored' },
    { v: '2', l: 'High-fidelity prototypes, from one structured prompt' },
    { v: '2', l: 'Personas defined and designed against' },
  ];
  return (
    <div className="dl-glance reveal">
      {stats.map((s, i) => (
        <div key={i} className="dl-glance-cell">
          <div className="dl-glance-v">{s.v}</div>
          <div className="dl-glance-l">{s.l}</div>
        </div>
      ))}
    </div>
  );
}

// ---- real prototype screenshot frame (screens already include their own
// phone bezel/notch, so this is a plain image card, not the desktop-style
// Mockup browser chrome used elsewhere) ----
function DLShot({ src, alt, caption, hero = false }) {
  return (
    <div className={'dl-shot' + (hero ? ' hero' : '')}>
      <img src={src} alt={alt} loading="lazy" />
      {caption && <div className="cs-shot-cap">{caption}</div>}
    </div>
  );
}

// ---- live, clickable embed of the actual React prototype (built from the
// Creative Text Review source, dropped in as a static site at
// dearly-prototype/) — a real iframe, not a screenshot, so it can be
// clicked through end to end. The prototype's own page lays itself out as
// phone-mock + a "now showing" sidebar side by side, which needs ~900px to
// look right — an iframe can't reflow that on its own, so on narrow
// screens it just clipped. Scaled the whole thing down to fit instead,
// the same measure-and-transform-scale trick the desktop Mockup uses. ----
const NATIVE_W = 900;
const NATIVE_H = 860;
function DLPrototypeEmbed() {
  const frameRef = React.useRef(null);
  const [scale, setScale] = React.useState(1);
  React.useEffect(() => {
    const el = frameRef.current; if (!el) return;
    const measure = () => { const w = el.clientWidth; if (w) setScale(Math.min(1, w / NATIVE_W)); };
    measure();
    const ro = new ResizeObserver(measure); ro.observe(el);
    return () => ro.disconnect();
  }, []);
  return (
    <div className="dl-embed reveal">
      <div className="dl-embed-frame" ref={frameRef} style={{ height: NATIVE_H * scale }}>
        <div style={{ width: NATIVE_W, height: NATIVE_H, transform: `scale(${scale})`, transformOrigin: 'top left' }}>
          <iframe
            src="dearly-prototype/index.html"
            title="Dearly interactive prototype"
            loading="lazy"
            style={{ width: NATIVE_W, height: NATIVE_H, border: 'none', display: 'block' }}
          />
        </div>
      </div>
      <div className="dl-embed-cap">Live prototype, click through the real thing, start to finish.</div>
    </div>
  );
}

Object.assign(window, { EllieDoodle, SamDoodle, DearlyPersonas, DLAtAGlance, DLShot, DLPrototypeEmbed });
