// screens_study.jsx — usability-study harness UI: sits *around* the Perform
// prototype rather than inside it. Deliberately styled differently from the
// product screens (no IOSDevice chrome, no BrandBar) so a participant can
// always tell "this is the test wrapper" apart from "this is the app being
// tested." Uses the same design tokens as the product for a consistent feel,
// just arranged in its own layout.

const STUDY_FEEDBACK_URL = 'https://forms.cloud.microsoft/r/AU8eu4zcYL';

// Single source of truth for the task prompt — shown on the task screen and
// repeated in the End Study banner, so it's always visible during the study.
const STUDY_TASK_TEXT = 'Complete the performance review.';

// Study-wrapper purple — the brand secondary color, used for every purple
// element on these two screens (header band, card border, buttons, bullet
// numbers). Intentionally the literal brand hex rather than the design
// system's `--prod-purple` token of the same value, since that token is also
// one of the four selectable accent themes in the Tweaks panel — using the
// token here could visually match whatever the tester has the *product* set
// to, whereas this hex is scoped to just these two screens.
const STUDY_PURPLE = '#632D8A';

// Shared full-viewport shell + bordered card with a solid-color header band
// (heading in white) over a plain white body — visually distinct from every
// product screen (which use the blue --primary-main/--accent family).
function StudyOverlay({ scrim, zIndex = 200, children }) {
  return (
    <div style={{
      position: 'fixed', inset: 0, zIndex, background: scrim,
      display: 'flex', alignItems: 'center', justifyContent: 'center', padding: 'var(--sp-6)',
      boxSizing: 'border-box', fontFamily: 'var(--font-sans)',
    }}>
      <div style={{
        width: '100%', maxWidth: 360, background: 'var(--bg-default)', borderRadius: 'var(--radius-xl)',
        border: `1px solid ${STUDY_PURPLE}`, boxShadow: '0 12px 32px rgba(16,24,40,0.18)',
        boxSizing: 'border-box', overflow: 'hidden',
      }}>
        {children}
      </div>
    </div>
  );
}

function StudyHeaderBand({ children }) {
  return (
    <div style={{ background: STUDY_PURPLE, padding: 'var(--sp-6) var(--sp-6) var(--sp-5)' }}>
      {children}
    </div>
  );
}

// Shared primary-button look for every study-wrapper screen (Start, Task,
// Study Complete) so they stay visually identical without copy-pasting.
const studyPrimaryButtonStyle = {
  display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 8, width: '100%', boxSizing: 'border-box',
  background: STUDY_PURPLE, color: '#fff', border: 'none', borderRadius: 'var(--radius-md)',
  padding: '13px', fontSize: 15, fontWeight: 600, cursor: 'pointer', fontFamily: 'var(--font-sans)',
};

// ── PART 1: pre-study screen, shown before the Perform prototype loads ──
function StartScreen({ onStart }) {
  const bullets = [
    'There are no right or wrong answers—we’re interested in what feels natural to you.',
    'It’s okay to get stuck. That’s helpful feedback for us.',
    'You’ll be using a prototype, so some features may not work as expected.',
    'You can end the study at any time.',
  ];
  return (
    <StudyOverlay scrim="var(--grey-100)">
      <StudyHeaderBand>
        <div style={{ fontSize: 12, fontWeight: 600, letterSpacing: 0.6, textTransform: 'uppercase', color: 'rgba(255,255,255,0.75)', marginBottom: 8 }}>
          Mitratech Perform Product Research
        </div>
        <h1 style={{ fontSize: 21, fontWeight: 600, lineHeight: 1.35, color: '#fff', margin: 0 }}>
          Before you get started
        </h1>
      </StudyHeaderBand>
      <div style={{ padding: 'var(--sp-6)', boxSizing: 'border-box' }}>
        <p style={{ fontSize: 14, lineHeight: 1.55, color: 'var(--fg-secondary)', margin: '0 0 18px' }}>
          This study should be completed on a mobile device and will take about 10 minutes to complete.
        </p>
        <p style={{ fontSize: 14, fontWeight: 600, color: 'var(--grey-900)', margin: '0 0 12px' }}>
          A few things to keep in mind:
        </p>
        <ul style={{ margin: '0 0 24px', padding: 0, listStyle: 'none', display: 'flex', flexDirection: 'column', gap: 12 }}>
          {bullets.map((b, i) => (
            <li key={i} style={{ display: 'flex', gap: 10, alignItems: 'flex-start' }}>
              <span style={{
                flexShrink: 0, width: 20, height: 20, display: 'flex', alignItems: 'center',
                justifyContent: 'center', marginTop: 1,
              }}>
                <span style={{ width: 6, height: 6, borderRadius: '50%', background: STUDY_PURPLE, display: 'block' }} />
              </span>
              <span style={{ fontSize: 13.5, lineHeight: 1.5, color: 'var(--grey-900)' }}>{b}</span>
            </li>
          ))}
        </ul>
        <button onClick={onStart} style={studyPrimaryButtonStyle}>
          Got it
        </button>
      </div>
    </StudyOverlay>
  );
}

// ── PART 1.5: task-prompt screen, shown after "Before you get started" and
// before the review flow begins. Reuses StartScreen's exact shell/styles
// (StudyOverlay + StudyHeaderBand + studyPrimaryButtonStyle) — only the
// header-band copy and the single button differ.
function StudyTaskScreen({ onStart }) {
  return (
    <StudyOverlay scrim="var(--grey-100)">
      <StudyHeaderBand>
        <div style={{ fontSize: 12, fontWeight: 600, letterSpacing: 0.6, textTransform: 'uppercase', color: 'rgba(255,255,255,0.75)', marginBottom: 8 }}>
          Your task
        </div>
        <h1 style={{ fontSize: 21, fontWeight: 600, lineHeight: 1.35, color: '#fff', margin: 0 }}>
          {STUDY_TASK_TEXT}
        </h1>
      </StudyHeaderBand>
      <div style={{ padding: 'var(--sp-6)', boxSizing: 'border-box' }}>
        <p style={{ fontSize: 14, lineHeight: 1.55, color: 'var(--fg-secondary)', margin: '0 0 18px' }}>
          Imagine you are an employee, and you've just logged into Perform on mobile to complete your performance review.
        </p>
        <button onClick={onStart} style={studyPrimaryButtonStyle}>
          Start
          <Icon name="arrowR" size={17} color="#fff" />
        </button>
      </div>
    </StudyOverlay>
  );
}

// ── PART 2: "study complete" overlay, shown 3s after #/review-complete ──
// Uses a dark scrim (matching the app's own dialog backdrops) rather than an
// opaque one, so the confirmation screen stays visible-but-dimmed underneath
// — present, but not clickable; the only live control is "Complete survey".
function StudyCompleteModal() {
  return (
    <StudyOverlay scrim="rgba(10,25,41,0.55)">
      <StudyHeaderBand>
        <div style={{ display: 'flex', justifyContent: 'center', marginBottom: 12 }}>
          <div style={{
            width: 48, height: 48, borderRadius: '50%', background: 'rgba(255,255,255,0.18)',
            display: 'flex', alignItems: 'center', justifyContent: 'center', animation: 'pop .3s cubic-bezier(0.16,1,0.3,1)',
          }}>
            <Icon name="checkCircle" size={26} color="#fff" />
          </div>
        </div>
        <h1 style={{ fontSize: 20, fontWeight: 600, textAlign: 'center', color: '#fff', margin: 0 }}>
          Study complete!
        </h1>
      </StudyHeaderBand>
      <div style={{ padding: 'var(--sp-6)', boxSizing: 'border-box' }}>
        <p style={{ fontSize: 14, lineHeight: 1.55, textAlign: 'center', color: 'var(--fg-secondary)', margin: '0 0 22px' }}>
          You’ve finished this session. One last thing — tell us how it went so we can keep improving Perform.
        </p>
        <a href={STUDY_FEEDBACK_URL} style={{
          display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 8, width: '100%', boxSizing: 'border-box',
          background: STUDY_PURPLE, color: '#fff', border: 'none', borderRadius: 'var(--radius-md)',
          padding: '13px', fontSize: 14.5, fontWeight: 600, cursor: 'pointer', fontFamily: 'var(--font-sans)',
          textAlign: 'center', textDecoration: 'none',
        }}>
          Complete survey
        </a>
      </div>
    </StudyOverlay>
  );
}

// ── PART 3: persistent, global "end study early" control ──
// Collapsed = a small tab flush with the very top edge (rounded bottom
// corners, chevron pointing down), positioned in the gap between the
// product header's logo and hamburger menu (not centered under either) so
// it never overlaps them. Expanded = a full-width banner sliding down below
// it with an explicit "End study" action and a separate way to collapse
// without ending anything. Also opens itself (via the confirm dialog only —
// the banner doesn't force open) when the 20-minute session timer in
// app.jsx fires the 'om-end-study-timeout' event.
function EndStudyControl({ onEndStudy }) {
  const [open, setOpen] = React.useState(false);
  const [confirm, setConfirm] = React.useState(false);

  React.useEffect(() => {
    const onTimeout = () => setConfirm(true);
    window.addEventListener('om-end-study-timeout', onTimeout);
    return () => window.removeEventListener('om-end-study-timeout', onTimeout);
  }, []);

  return (
    <div style={{ position: 'fixed', top: 0, left: 0, right: 0, zIndex: 300, fontFamily: 'var(--font-sans)' }}>
      {open && (
        <div style={{
          width: '100%', boxSizing: 'border-box', background: STUDY_PURPLE, color: '#fff',
          padding: '8px 14px', display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 10,
          boxShadow: '0 6px 20px rgba(0,0,0,0.22)', animation: 'studyBannerIn .22s cubic-bezier(0.16,1,0.3,1)',
        }}>
          <div style={{ flex: 1, minWidth: 0, display: 'flex', flexDirection: 'column', gap: 2 }}>
            <span style={{ fontSize: 12.5, fontWeight: 500, opacity: 0.85 }}>Usability study in progress</span>
            <span style={{ fontSize: 11.5, fontWeight: 600, opacity: 0.95, lineHeight: 1.3 }}>Task: {STUDY_TASK_TEXT}</span>
          </div>
          <div style={{ display: 'flex', alignItems: 'center', gap: 8, flexShrink: 0 }}>
            {/* ≥44x44pt touch targets per standard mobile guidance */}
            <button onClick={() => setConfirm(true)} style={{
              minHeight: 44, boxSizing: 'border-box', background: 'rgba(255,255,255,0.16)', color: '#fff', border: '1px solid rgba(255,255,255,0.4)',
              borderRadius: 'var(--radius-pill)', padding: '0 18px', fontSize: 13.5, fontWeight: 600,
              cursor: 'pointer', fontFamily: 'var(--font-sans)', whiteSpace: 'nowrap',
              display: 'flex', alignItems: 'center', justifyContent: 'center',
            }}>End study</button>
            <button onClick={() => setOpen(false)} aria-label="Collapse" style={{
              width: 44, height: 44, borderRadius: '50%', background: 'rgba(255,255,255,0.16)', border: 'none',
              cursor: 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0, padding: 0,
            }}><Icon name="chevronUp" size={18} color="#fff" /></button>
          </div>
        </div>
      )}
      {!open && (
        <button onClick={() => setOpen(true)} aria-label="Study options" style={{
          position: 'absolute', top: 0, left: '50%', transform: 'translateX(60px)',
          width: 64, height: 28, borderBottomLeftRadius: 'var(--radius-lg)', borderBottomRightRadius: 'var(--radius-lg)',
          background: STUDY_PURPLE, border: 'none', borderTop: 'none', cursor: 'pointer',
          display: 'flex', alignItems: 'center', justifyContent: 'center', padding: 0, opacity: 0.95,
        }}><Icon name="chevronDown" size={18} color="#fff" /></button>
      )}

      {confirm && (
        <StudyOverlay scrim="rgba(10,25,41,0.4)" zIndex={310}>
          <StudyHeaderBand>
            <h1 style={{ fontSize: 18, fontWeight: 600, textAlign: 'center', color: '#fff', margin: 0 }}>End the study?</h1>
          </StudyHeaderBand>
          <div style={{ padding: 'var(--sp-6)', boxSizing: 'border-box' }}>
            <p style={{ fontSize: 13.5, lineHeight: 1.5, textAlign: 'center', color: 'var(--fg-secondary)', margin: '0 0 20px' }}>
              You’ll be taken to the end of the session, and this can’t be undone.
            </p>
            <div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
              <button onClick={() => { setConfirm(false); setOpen(false); onEndStudy(); }} style={{ background: STUDY_PURPLE, color: '#fff', border: 'none', borderRadius: 'var(--radius-md)', padding: '12px', fontSize: 14.5, fontWeight: 600, cursor: 'pointer', fontFamily: 'var(--font-sans)' }}>End study</button>
              <button onClick={() => setConfirm(false)} style={{ background: '#fff', color: 'var(--grey-800)', border: '1px solid var(--grey-300)', borderRadius: 'var(--radius-md)', padding: '12px', fontSize: 14.5, fontWeight: 600, cursor: 'pointer', fontFamily: 'var(--font-sans)' }}>Cancel</button>
            </div>
          </div>
        </StudyOverlay>
      )}
    </div>
  );
}

Object.assign(window, { StartScreen, StudyTaskScreen, StudyCompleteModal, EndStudyControl });
