// Signup — 3-step stepper. State lives in parent App; we receive ctx + setCtx + go.
const STEPS = ['DETAILS', 'WAIVER', 'CONFIRM'];

const Stepper = ({ step }) => (
  <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
    {STEPS.map((s, i) => (
      <React.Fragment key={s}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
          <div style={{
            width: 32, height: 32,
            background: i < step ? T.gold : (i === step ? T.blue2 : 'transparent'),
            border: `1px solid ${i <= step ? (i < step ? T.gold : T.blue2) : T.rule}`,
            display: 'flex', alignItems: 'center', justifyContent: 'center',
            color: i < step ? T.bg : T.bone,
          }}>
            <span style={{ fontFamily: "'JetBrains Mono',monospace", fontSize: 12 }}>{i < step ? '✓' : i + 1}</span>
          </div>
          <Bebas size={15} color={i === step ? T.bone : T.mute}>{s}</Bebas>
        </div>
        {i < STEPS.length - 1 && <div style={{ flex: 1, height: 1, background: i < step ? T.gold : T.rule }} />}
      </React.Fragment>
    ))}
  </div>
);

const calcAge = (mmddyyyy) => {
  const m = mmddyyyy?.match(/^(\d{2})\/(\d{2})\/(\d{4})$/);
  if (!m) return null;
  const [_, mm, dd, yyyy] = m;
  const bd = new Date(+yyyy, +mm - 1, +dd);
  // Validate the date didn't roll over (e.g. "02/30/2000" parses to March 2)
  if (bd.getFullYear() !== +yyyy || bd.getMonth() !== +mm - 1 || bd.getDate() !== +dd) return null;
  const today = new Date(2026, 4, 7); // local-midnight May 7, 2026 — avoids ISO-string UTC parsing

  let age = today.getFullYear() - bd.getFullYear();
  const md = today.getMonth() - bd.getMonth();
  if (md < 0 || (md === 0 && today.getDate() < bd.getDate())) age--;
  return age;
};

// Field-level validators. All return true when the field is OK to submit.
// Empty values short-circuit to false so they're not "valid" until typed.
const isValidEmail = (s) => /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test((s || '').trim());
const isValidPhone = (s) => (s || '').replace(/\D/g, '').length === 10;
const isValidBirthday = (s) => {
  const age = calcAge(s);
  return age !== null && age >= 14 && age <= 80;
};

const SignupDetails = ({ ctx, setCtx, go, event }) => {
  const age = calcAge(ctx.birthday);
  const isMinor = age !== null && age < 18;
  const careers = Array.isArray(ctx.careers) ? ctx.careers : [];
  const toggleCareer = (code) => {
    setCtx(prev => {
      const list = Array.isArray(prev.careers) ? prev.careers : [];
      return { careers: list.includes(code) ? list.filter(c => c !== code) : [...list, code] };
    });
  };
  // Per-field validity flags also drive inline error captions on the inputs.
  const emailOk = isValidEmail(ctx.email);
  const phoneOk = isValidPhone(ctx.phone);
  const birthdayOk = isValidBirthday(ctx.birthday);
  const guardianEmailOk = isValidEmail(ctx.guardianEmail);
  const guardianPhoneOk = !ctx.guardianPhone || isValidPhone(ctx.guardianPhone); // optional
  const canContinue = !!(
    ctx.first?.trim() && ctx.last?.trim() &&
    emailOk && phoneOk && birthdayOk &&
    careers.length > 0 &&
    (!isMinor || (ctx.guardianName?.trim() && guardianEmailOk && guardianPhoneOk))
  );

  return (
    <div style={{ maxWidth: 880, margin: '0 auto', padding: '24px 32px 48px' }}>
      <Mono size={11} color={T.mute} style={{ display: 'block' }}>// STEP 01 OF 03</Mono>
      <Bebas size={42} style={{ marginTop: 6, display: 'block', lineHeight: 1.05 }}>Your details.</Bebas>
      <Body size={13} color={T.mute} style={{ marginTop: 6, display: 'block' }}>We never share your information. * = required.</Body>

      <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr 1fr', gap: 14, marginTop: 24 }}>
        <Field label="FIRST NAME *" value={ctx.first} onChange={v => setCtx({ first: v })} />
        <Field label="LAST NAME *" value={ctx.last} onChange={v => setCtx({ last: v })} />
        <Field label="EMAIL *" value={ctx.email} onChange={v => setCtx({ email: v })} type="email"
          error={ctx.email && !emailOk ? 'INVALID' : undefined} />
      </div>
      <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 14, marginTop: 14 }}>
        <Field label="BIRTHDAY *" value={ctx.birthday} onChange={v => setCtx({ birthday: formatDate(v) })} ph="MM/DD/YYYY"
          error={ctx.birthday && !birthdayOk ? 'CHECK DATE / AGE 14–80' : undefined} />
        <Field label="PHONE *" value={ctx.phone} onChange={v => setCtx({ phone: formatPhone(v) })} ph="(555) 555-5555"
          error={ctx.phone && !phoneOk ? 'INCOMPLETE' : undefined} />
      </div>

      {isMinor && (
        <div style={{ marginTop: 22, padding: 18, border: `1px solid ${T.gold}`, background: 'rgba(200,168,78,0.06)' }}>
          <Mono size={10} color={T.gold}>// PARTICIPANT IS UNDER 18 · GUARDIAN INFO REQUIRED</Mono>
          <Body size={12} color={T.bone} style={{ marginTop: 6 }}>
            A parent or guardian must co-sign the waiver. They'll receive a copy at the email below.
          </Body>
          <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr 1fr', gap: 14, marginTop: 14 }}>
            <Field label="GUARDIAN NAME *" value={ctx.guardianName} onChange={v => setCtx({ guardianName: v })} />
            <Field label="GUARDIAN EMAIL *" value={ctx.guardianEmail} onChange={v => setCtx({ guardianEmail: v })}
              error={ctx.guardianEmail && !guardianEmailOk ? 'INVALID' : undefined} />
            <Field label="GUARDIAN PHONE" value={ctx.guardianPhone} onChange={v => setCtx({ guardianPhone: formatPhone(v) })} ph="(555) 555-5555"
              error={ctx.guardianPhone && !guardianPhoneOk ? 'INCOMPLETE' : undefined} />
          </div>
        </div>
      )}

      <div style={{ marginTop: 28 }}>
        <Mono size={11} color={T.mute}>CAREER INTEREST · pick all that apply{careers.length > 0 ? ` · ${careers.length} selected` : ''}</Mono>
        <div style={{ marginTop: 10, display: 'flex', flexWrap: 'wrap', gap: 8 }}>
          {CAREER_FIELDS.map(c => {
            const active = careers.includes(c.code);
            return (
              <button key={c.code} onClick={() => toggleCareer(c.code)} style={{
                padding: '8px 14px',
                border: `1px solid ${active ? T.gold : T.rule}`,
                background: active ? 'rgba(200,168,78,0.12)' : 'transparent',
                color: active ? T.gold : T.bone,
                fontFamily: "'JetBrains Mono',monospace",
                fontSize: 12,
                letterSpacing: 1,
                cursor: 'pointer',
                display: 'flex', flexDirection: 'column', alignItems: 'flex-start', gap: 2,
                minWidth: 100,
              }}>
                <span style={{ fontWeight: 700 }}>{active ? '✓ ' : ''}{c.code}</span>
                <span style={{ fontSize: 9, color: T.mute, fontWeight: 400 }}>{c.name}</span>
              </button>
            );
          })}
        </div>
      </div>

      <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginTop: 36 }}>
        <button onClick={() => go('landing')} style={{ background: 'transparent', border: 'none', color: T.mute, fontFamily: "'JetBrains Mono',monospace", fontSize: 11, letterSpacing: 1, cursor: 'pointer' }}>
          ← Back to events
        </button>
        <PrimaryCTA blue disabled={!canContinue} onClick={() => go('signup', { step: 1 })}>Continue to waiver →</PrimaryCTA>
      </div>
    </div>
  );
};

const Signup = ({ ctx, setCtx, go, step = 0 }) => {
  const [allEvents] = useEventsStore();
  const active = allEvents.filter(e => !e.archived);
  const event = allEvents.find(e => e.code === ctx.eventCode) || active[active.length - 1] || allEvents[0];

  return (
    <div style={{ height: '100%', overflowY: 'auto', background: T.bg, color: T.bone, position: 'relative' }}>
      <Watermark opacity={0.06} />
      {/* Header */}
      <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', padding: '16px 32px', borderBottom: `1px solid ${T.rule}` }}>
        <SWLockup size={0.95} />
        <Mono size={10} color={T.mute}>SECURE FORM · SSL</Mono>
      </div>

      {/* Event banner */}
      <div style={{ background: T.bg2, padding: '14px 32px', display: 'flex', justifyContent: 'space-between', alignItems: 'center', borderBottom: `1px solid ${T.rule}`, gap: 24 }}>
        <div style={{ display: 'flex', alignItems: 'baseline', gap: 14, flexWrap: 'wrap' }}>
          <Mono size={10} color={T.gold}>// SIGNING UP FOR</Mono>
          <Bebas size={22} style={{ lineHeight: 1.1 }}>{event.city}, {event.state} · {event.date}</Bebas>
        </div>
        <Mono size={11} color={T.mute} style={{ whiteSpace: 'nowrap' }}>{event.venue.toUpperCase()}</Mono>
      </div>

      {/* Stepper */}
      <div style={{ padding: '24px 60px 12px' }}>
        <Stepper step={step} />
      </div>

      {/* Step content */}
      {step === 0 && <SignupDetails ctx={ctx} setCtx={setCtx} go={go} event={event} />}
      {step === 1 && <Waiver ctx={ctx} setCtx={setCtx} go={go} event={event} />}
      {step === 2 && <Confirm ctx={ctx} setCtx={setCtx} go={go} event={event} />}
    </div>
  );
};

window.Signup = Signup;
window.calcAge = calcAge;
window.isValidEmail = isValidEmail;
window.isValidPhone = isValidPhone;
window.isValidBirthday = isValidBirthday;
