// /contact — the contact form. Posts through siteInquiry() (home-v2/Sections.jsx)
// to the OpsCenter's api/site-inquiry with kind=contact, UTMs, referrer and page.
// A contact submission mails Sam and JP and becomes a site-form lead in the OC.
// `website` is the honeypot: kept off-screen, never filled by a person.
// `heard_about` is self-reported attribution, open text on purpose (not a dropdown): it names the podcast
// or the person a preset list would file wrong. Contact form only; the OC stores it as free text, 500 chars.

function ContactPage() {
  const [f, setF] = React.useState({ name: '', email: '', company: '', role: '', message: '', heard_about: '', website: '' });
  const [state, setState] = React.useState('idle'); // idle | sending | done | error
  const [err, setErr] = React.useState('');
  const set = (k) => (e) => setF({ ...f, [k]: e.target.value });
  const emailOk = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(f.email.trim());

  const submit = (e) => {
    e.preventDefault();
    if (state === 'sending' || !emailOk) return;
    setState('sending'); setErr('');
    siteInquiry({ kind: 'contact', ...f, email: f.email.trim() })
      .then((d) => { if (d && d.ok) setState('done'); else { setState('error'); setErr((d && d.error) || 'Something went wrong.'); } })
      .catch(() => { setState('error'); setErr('Could not reach us. Email connect@eightbyzero.com instead.'); });
  };

  const field = {
    width: '100%', boxSizing: 'border-box', padding: '13px 15px',
    background: 'var(--bg)', color: 'var(--fg-1)',
    border: '1px solid var(--border-2)', borderRadius: 4, outline: 'none',
    fontFamily: 'var(--font-sans)', fontSize: 15,
  };
  const label = { display: 'flex', flexDirection: 'column', gap: 7, fontSize: 13, color: 'var(--fg-3)', fontFamily: 'var(--font-mono)', letterSpacing: '0.04em' };

  return (
    <section className="flat" style={{ padding: '112px 0 112px' }}>
      <div className="container" style={{ maxWidth: 680 }}>
        <span className="eyebrow" style={{ color: 'var(--accent)', alignSelf: 'flex-start' }}>Contact</span>
        <h1 style={{
          fontSize: 'clamp(34px, 4vw, 56px)', lineHeight: 1.07, fontWeight: 700,
          letterSpacing: '-0.025em', color: 'var(--fg-1)', margin: '18px 0 0',
        }}>Talk to us.</h1>
        <p style={{ fontSize: 17, lineHeight: 1.6, color: 'var(--fg-2)', margin: '18px 0 36px' }}>
          Tell us what you are connecting and we will be in touch today. Or email <a href="mailto:connect@eightbyzero.com" style={{ color: 'var(--accent)' }}>connect@eightbyzero.com</a>.
        </p>

        {state === 'done' ? (
          <div style={{
            padding: '16px 20px', border: '1px solid rgba(16, 200, 128, 0.35)',
            borderLeft: '2px solid var(--accent)', background: 'rgba(16, 200, 128, 0.06)', borderRadius: 4,
            fontSize: 15, color: 'var(--fg-1)',
          }}>✓ Thanks, we will be in touch today.</div>
        ) : (
          <form onSubmit={submit} style={{ display: 'flex', flexDirection: 'column', gap: 18 }}>
            <div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(240px, 1fr))', gap: 18 }}>
              <label style={label}>Name<input style={field} value={f.name} onChange={set('name')} autoComplete="name"/></label>
              <label style={label}>Work email *<input style={field} type="email" required value={f.email} onChange={set('email')} autoComplete="email"/></label>
              <label style={label}>Company<input style={field} value={f.company} onChange={set('company')} autoComplete="organization"/></label>
              <label style={label}>Role<input style={field} value={f.role} onChange={set('role')} autoComplete="organization-title"/></label>
            </div>
            <label style={label}>How can we help?<textarea style={{ ...field, minHeight: 140, resize: 'vertical' }} value={f.message} onChange={set('message')}/></label>
            <label style={label}>How did you hear about us? (optional)<input style={field} name="heard_about" maxLength={500} value={f.heard_about} onChange={set('heard_about')}/></label>
            <input name="website" tabIndex={-1} autoComplete="off" aria-hidden="true" value={f.website} onChange={set('website')}
              style={{ position: 'absolute', left: -9999, width: 1, height: 1 }}/>
            {state === 'error' && <p role="alert" style={{ margin: 0, color: 'var(--attention)', fontSize: 14 }}>{err}</p>}
            <button type="submit" disabled={state === 'sending' || !emailOk} style={{
              alignSelf: 'flex-start', padding: '13px 26px', borderRadius: 4,
              border: '1px solid var(--accent)', background: emailOk ? 'var(--accent)' : 'transparent',
              color: emailOk ? 'var(--bg)' : 'var(--fg-3)', fontFamily: 'var(--font-sans)', fontSize: 14, fontWeight: 600,
              cursor: (state === 'sending' || !emailOk) ? 'default' : 'pointer',
            }}>{state === 'sending' ? 'Sending…' : 'Send'}</button>
          </form>
        )}
      </div>
    </section>
  );
}

Object.assign(window, { ContactPage });
