/* ──────────────────────────────────────────────────────────
   CTA + Process — final section combining the call-to-action
   with the 5-step engagement flow.
   Contact form powered by Formspree (mdavoadp).
   ────────────────────────────────────────────────────────── */
const { useState, useEffect, useRef, useMemo, useLayoutEffect } = React;

/* ─── Process icons ──────────────────────────────────────── */
const ICON_INK  = 'var(--ink)';
const ICON_GOLD = 'var(--gold-deep)';
const ICON_SOFT = 'var(--gold-soft)';

function PhoneIcon({ size = 28 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 48 48" fill="none">
      <path d="M 14 10 L 21 10 L 24 18 L 20 21 C 22 26 26 30 31 32 L 34 28 L 42 31 L 42 38 C 42 40 40 42 38 42 C 23 41 11 29 10 14 C 10 12 12 10 14 10 Z"
        fill="none" stroke={ICON_INK} strokeWidth="1.3" strokeLinejoin="round"/>
      <circle cx="36" cy="12" r="2.5" fill={ICON_GOLD}/>
    </svg>
  );
}

function ObserveIcon({ size = 28 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 48 48" fill="none">
      <circle cx="24" cy="24" r="14" fill="none" stroke={ICON_INK}
        strokeWidth="0.8" strokeDasharray="1.5 3" opacity="0.5"/>
      <circle cx="24" cy="13" r="3" fill={ICON_INK}/>
      <path d="M 19 22 C 19 19 21.5 17 24 17 C 26.5 17 29 19 29 22"
        fill="none" stroke={ICON_INK} strokeWidth="1.3" strokeLinecap="round"/>
      <circle cx="14" cy="30" r="3" fill={ICON_INK}/>
      <path d="M 9 39 C 9 36 11.5 34 14 34 C 16.5 34 19 36 19 39"
        fill="none" stroke={ICON_INK} strokeWidth="1.3" strokeLinecap="round"/>
      <circle cx="34" cy="30" r="3" fill={ICON_GOLD}/>
      <path d="M 29 39 C 29 36 31.5 34 34 34 C 36.5 34 39 36 39 39"
        fill="none" stroke={ICON_GOLD} strokeWidth="1.3" strokeLinecap="round"/>
    </svg>
  );
}

function ShieldDesignIcon({ size = 28 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 48 48" fill="none">
      <path d="M 24 6 L 38 11 V 24 C 38 33 24 42 24 42 C 24 42 10 33 10 24 V 11 Z"
        fill="rgba(184,153,104,0.08)" stroke={ICON_INK} strokeWidth="1.3"
        strokeLinejoin="round"/>
      <path d="M 18 30 L 30 30 L 30 18 Z"
        fill="none" stroke={ICON_GOLD} strokeWidth="1.2" strokeLinejoin="round"/>
      <circle cx="30" cy="18" r="1.6" fill={ICON_GOLD}/>
    </svg>
  );
}

function DeployIcon({ size = 28 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 48 48" fill="none">
      <path d="M 24 6 L 38 14 L 38 32 L 24 40 L 10 32 L 10 14 Z"
        fill="rgba(184,153,104,0.10)" stroke={ICON_INK} strokeWidth="1.2"
        strokeLinejoin="round"/>
      <path d="M 24 6 L 24 22 L 38 30" stroke={ICON_INK} strokeWidth="0.9" opacity="0.7"/>
      <path d="M 24 22 L 10 30" stroke={ICON_INK} strokeWidth="0.9" opacity="0.7"/>
      <circle cx="24" cy="22" r="2.2" fill={ICON_GOLD}/>
    </svg>
  );
}

function ScaleIcon({ size = 28 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 48 48" fill="none">
      <rect x="8"  y="30" width="6" height="12" rx="1" fill={ICON_INK}/>
      <rect x="18" y="24" width="6" height="18" rx="1" fill={ICON_INK}/>
      <rect x="28" y="18" width="6" height="24" rx="1" fill={ICON_GOLD}/>
      <path d="M 8 22 L 18 17 L 28 13 L 38 8"
        stroke={ICON_GOLD} strokeWidth="1.3" fill="none" strokeLinecap="round"/>
      <path d="M 35 6 L 40 8 L 38 13"
        stroke={ICON_GOLD} strokeWidth="1.3" fill="none"
        strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  );
}

/* ─── Chip row ───────────────────────────────────────────── */
function CtaChip({ icon, label }) {
  return (
    <div style={{ display:'inline-flex', alignItems:'center', gap:'10px',
      fontFamily:"'Inter',sans-serif", fontWeight:400, fontSize:'13px',
      color:'var(--ink-2)', letterSpacing:'0.01em' }}>
      <span style={{ color:'var(--gold-deep)', display:'inline-flex' }}>{icon}</span>
      {label}
    </div>
  );
}

const ChipClock = (
  <svg width="16" height="16" viewBox="0 0 20 20" fill="none">
    <circle cx="10" cy="10" r="7.5" stroke="currentColor" strokeWidth="1.2"/>
    <path d="M 10 6 V 10 L 13 12" stroke="currentColor" strokeWidth="1.2" strokeLinecap="round"/>
  </svg>
);
const ChipUser = (
  <svg width="16" height="16" viewBox="0 0 20 20" fill="none">
    <circle cx="10" cy="7" r="3" stroke="currentColor" strokeWidth="1.2"/>
    <path d="M 4 17 C 4 13.5 6.7 11 10 11 C 13.3 11 16 13.5 16 17"
      stroke="currentColor" strokeWidth="1.2" strokeLinecap="round"/>
  </svg>
);
const ChipLock = (
  <svg width="16" height="16" viewBox="0 0 20 20" fill="none">
    <rect x="5" y="9" width="10" height="8" rx="1.4" stroke="currentColor" strokeWidth="1.2"/>
    <path d="M 7 9 V 6.5 C 7 4.8 8.3 3.5 10 3.5 C 11.7 3.5 13 4.8 13 6.5 V 9"
      stroke="currentColor" strokeWidth="1.2" strokeLinecap="round"/>
  </svg>
);

/* ─── Shared input styles ────────────────────────────────── */
const inputStyle = {
  width: '100%',
  padding: '11px 16px',
  borderRadius: '10px',
  border: '1px solid var(--line)',
  background: 'var(--bg)',
  color: 'var(--ink)',
  fontFamily: "'Inter', sans-serif",
  fontSize: '14px',
  fontWeight: 300,
  outline: 'none',
  transition: 'border-color .2s',
};

/* ─── Contact Form (Formspree) ───────────────────────────── */
function ContactForm() {
  const [form, setForm] = useState({ name:'', email:'', company:'', message:'' });
  const [status, setStatus] = useState('idle'); // idle | submitting | success | error

  const update = (k) => (e) => setForm(p => ({ ...p, [k]: e.target.value }));

  const handleSubmit = async (e) => {
    e.preventDefault();
    setStatus('submitting');
    try {
      const res = await fetch('https://formspree.io/f/mdavoadp', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' },
        body: JSON.stringify(form),
      });
      setStatus(res.ok ? 'success' : 'error');
    } catch { setStatus('error'); }
  };

  if (status === 'success') {
    return (
      <div style={{ padding:'48px 0', textAlign:'center' }}>
        <div style={{ fontFamily:"'Bodoni Moda', serif", fontVariationSettings:"'opsz' 14",
          fontSize:'52px', fontWeight:300, color:'var(--gold-deep)', marginBottom:'16px' }}>
          ✓
        </div>
        <h3 style={{ fontFamily:"'Bodoni Moda', serif", fontVariationSettings:"'opsz' 14",
          fontWeight:400, fontSize:'26px', color:'var(--ink)', marginBottom:'12px',
          letterSpacing:'-0.01em' }}>
          Message sent.
        </h3>
        <p style={{ fontFamily:"'Inter',sans-serif", fontWeight:300, fontSize:'14.5px',
          color:'var(--ink-2)', lineHeight:1.65 }}>
          We'll be in touch at <span style={{ color:'var(--gold-deep)' }}>{form.email}</span> shortly.
        </p>
      </div>
    );
  }

  return (
    <form onSubmit={handleSubmit}
      style={{ display:'flex', flexDirection:'column', gap:'12px' }}>
      <div style={{ display:'grid', gridTemplateColumns:'1fr 1fr', gap:'12px' }}>
        <input style={inputStyle} type="text" placeholder="Full Name" required
          value={form.name} onChange={update('name')}
          onFocus={e => e.target.style.borderColor='var(--gold)'}
          onBlur={e => e.target.style.borderColor='var(--line)'} />
        <input style={inputStyle} type="email" placeholder="Email Address" required
          value={form.email} onChange={update('email')}
          onFocus={e => e.target.style.borderColor='var(--gold)'}
          onBlur={e => e.target.style.borderColor='var(--line)'} />
      </div>
      <input style={inputStyle} type="text" placeholder="Company"
        value={form.company} onChange={update('company')}
        onFocus={e => e.target.style.borderColor='var(--gold)'}
        onBlur={e => e.target.style.borderColor='var(--line)'} />
      <textarea style={{ ...inputStyle, resize:'none' }}
        placeholder="What are you interested in? (optional)"
        rows={4} value={form.message} onChange={update('message')}
        onFocus={e => e.target.style.borderColor='var(--gold)'}
        onBlur={e => e.target.style.borderColor='var(--line)'} />
      {status === 'error' && (
        <p style={{ fontFamily:"'Inter',sans-serif", fontSize:'13px',
          color:'#b84a4a', lineHeight:1.5 }}>
          Something went wrong. Please try again or email us at{' '}
          <a href="mailto:info@mataiconsulting.com"
            style={{ color:'var(--gold-deep)' }}>info@mataiconsulting.com</a>
        </p>
      )}
      <button type="submit" disabled={status === 'submitting'}
        style={{
          padding:'16px 32px', borderRadius:'100px', cursor:'pointer',
          border:'none', background:'var(--ink)', color:'var(--paper)',
          fontFamily:"'Inter',sans-serif", fontWeight:500, fontSize:'14px',
          letterSpacing:'0.02em', opacity: status === 'submitting' ? 0.7 : 1,
          transition:'opacity .2s', marginTop:'4px',
          display:'inline-flex', alignItems:'center', gap:'12px',
        }}>
        {status === 'submitting' ? 'Sending…' : <>Book a Strategy Session <span style={{ color:'var(--gold-soft)' }}>→</span></>}
      </button>
    </form>
  );
}

/* ─── Section ───────────────────────────────────────────── */
function CTASection() {
  const isMobile = useIsMobile();
  const steps = [
    { Icon: PhoneIcon,        label: 'Discovery Call',
      desc: 'Align on business goals, priorities, and opportunities for Agentic AI.' },
    { Icon: ObserveIcon,      label: 'Readiness Assessment',
      desc: 'Assess current capabilities, identify gaps, and determine the path to successful adoption.' },
    { Icon: ShieldDesignIcon, label: 'Strategy & Roadmap',
      desc: 'Develop a transformation strategy, prioritize initiatives, and establish a roadmap for execution.' },
    { Icon: DeployIcon,       label: 'Implementation',
      desc: 'Design and deploy trusted Agentic AI solutions with governance, oversight, and scalability built in.' },
    { Icon: ScaleIcon,        label: 'Optimization & Scale',
      desc: 'Continuously improve performance, expand capabilities, and accelerate value realization across the enterprise.' },
  ];

  if (isMobile) {
    return (
      <section id="contact" data-theme="light" style={{
        padding:'72px 24px 56px', background:'var(--paper)',
        borderTop:'1px solid var(--line-2)', position:'relative',
      }}>
        {/* Headline + subtext */}
        <Reveal style={{ marginBottom:'40px' }}>
          <Eyebrow tone="gold" style={{ marginBottom:'18px' }}>Get Started</Eyebrow>
          <h2 style={{ fontFamily:"'Bodoni Moda', serif", fontVariationSettings:"'opsz' 14", fontWeight:500,
            fontSize:'clamp(40px, 11vw, 60px)', lineHeight:1.0,
            letterSpacing:'-0.025em', marginBottom:'20px', color:'var(--ink)' }}>
            Ready to build your{' '}
            <span className="accent-italic">Agentic AI</span> advantage?
          </h2>
          <p style={{ fontFamily:"'Inter',sans-serif", fontWeight:400, fontSize:'15px',
            color:'var(--ink-2)', lineHeight:1.75, marginBottom:'24px' }}>
            Book a strategic conversation with our team.
          </p>
          <div style={{ display:'flex', flexWrap:'wrap', gap:'12px 20px' }}>
            <CtaChip icon={ChipClock} label="30-minute consultation" />
            <CtaChip icon={ChipUser}  label="Executive-led" />
            <CtaChip icon={ChipLock}  label="No obligation" />
          </div>
        </Reveal>

        {/* 5-step approach — horizontal swipe */}
        <Reveal style={{ marginBottom:'40px' }}>
          <div style={{ marginBottom:'16px', display:'flex', alignItems:'center', gap:'12px' }}>
            <div style={{ fontFamily:"'Inter',sans-serif", fontWeight:600, fontSize:'11px',
              letterSpacing:'0.22em', textTransform:'uppercase', color:'var(--gold-deep)' }}>
              Our Approach
            </div>
            <span style={{ flex:'1', height:'1px', background:'var(--line-2)' }} />
          </div>
          <div style={{ display:'flex', overflowX:'auto', gap:'12px',
            scrollSnapType:'x mandatory', WebkitOverflowScrolling:'touch',
            scrollbarWidth:'none', paddingBottom:'8px', margin:'0 -24px', padding:'0 24px 8px' }}>
            {steps.map((p, i) => (
              <div key={p.label} style={{ flex:'0 0 56vw', maxWidth:'200px',
                scrollSnapAlign:'start', display:'flex', flexDirection:'column',
                alignItems:'center', textAlign:'center', gap:'10px',
                padding:'16px 12px', borderRadius:'12px',
                background:'rgba(20,24,31,0.025)', border:'1px solid var(--line-2)' }}>
                <div style={{ width:'44px', height:'44px', borderRadius:'50%',
                  border:'1px solid var(--line)', background:'var(--paper)',
                  display:'flex', alignItems:'center', justifyContent:'center' }}>
                  <p.Icon size={22} />
                </div>
                <div style={{ fontFamily:"'Inter',sans-serif", fontSize:'10px', fontWeight:600,
                  color:'var(--gold-deep)', letterSpacing:'0.16em', textTransform:'uppercase',
                  lineHeight:1.35 }}>{p.label}</div>
                <p style={{ fontFamily:"'Inter',sans-serif", fontWeight:400, fontSize:'11.5px',
                  color:'var(--ink-2)', lineHeight:1.5 }}>{p.desc}</p>
              </div>
            ))}
          </div>
        </Reveal>

        {/* Form */}
        <Reveal>
          <ContactForm />
        </Reveal>
      </section>
    );
  }

  return (
    <section id="contact" data-theme="light" className="grain" style={{
      padding:'120px 40px 60px', background:'var(--paper)',
      borderTop:'1px solid var(--line-2)', overflow:'hidden', position:'relative',
    }}>
      <div style={{ maxWidth:'1320px', margin:'0 auto', position:'relative' }}>

        {/* ─── Top: headline left, form right ──────────── */}
        <div style={{ display:'grid', gridTemplateColumns:'1fr 1fr', gap:'80px',
          alignItems:'start' }}>

          <Reveal>
            <Eyebrow tone="gold" style={{ marginBottom:'22px' }}>Get Started</Eyebrow>
            <h2 style={{ fontFamily:"'Bodoni Moda', serif",
              fontVariationSettings:"'opsz' 14", fontWeight:500,
              fontSize:'clamp(48px, 5.6vw, 78px)', lineHeight:1.0,
              letterSpacing:'-0.025em', marginBottom:'28px', color:'var(--ink)',
              textWrap:'balance' }}>
              Ready to build your<br/>
              <span className="accent-italic">Agentic AI</span> advantage?
            </h2>
            <p style={{ fontFamily:"'Inter',sans-serif", fontWeight:400, fontSize:'15.5px',
              color:'var(--ink-2)', lineHeight:1.75, maxWidth:'460px',
              marginBottom:'40px' }}>
              Book a strategic conversation with our team. We'll help you
              identify where AI creates the most value for your business — and
              what it takes to get there.
            </p>

            <div style={{ display:'flex', alignItems:'center', gap:'28px', flexWrap:'wrap' }}>
              <CtaChip icon={ChipClock} label="30-minute consultation" />
              <span style={{ width:'1px', height:'14px', background:'var(--line)' }} />
              <CtaChip icon={ChipUser}  label="Executive-led" />
              <span style={{ width:'1px', height:'14px', background:'var(--line)' }} />
              <CtaChip icon={ChipLock}  label="No obligation" />
            </div>
          </Reveal>

          <Reveal delay={0.1}>
            <ContactForm />
          </Reveal>
        </div>

        {/* ─── Bottom: 5-step engagement flow ──────────── */}
        <Reveal>
          <div style={{ marginTop:'72px', padding:'30px 32px 28px',
            background:'rgba(20,24,31,0.025)',
            border:'1px solid var(--line-2)', borderRadius:'16px' }}>

            <div style={{ display:'flex', alignItems:'center',
              gap:'14px', marginBottom:'22px' }}>
              <div style={{ fontFamily:"'Inter',sans-serif", fontWeight:600,
                fontSize:'11px', letterSpacing:'0.22em', textTransform:'uppercase',
                color:'var(--gold-deep)' }}>Our Approach</div>
              <span style={{ flex:'1', height:'1px', background:'var(--line-2)' }} />
            </div>

            <div style={{ display:'grid',
              gridTemplateColumns:'1fr auto 1fr auto 1fr auto 1fr auto 1fr',
              alignItems:'start', gap:'8px' }}>
              {steps.map((p, i) => (
                <React.Fragment key={p.label}>
                  <div style={{ display:'flex', flexDirection:'column',
                    alignItems:'center', textAlign:'center', gap:'10px',
                    padding:'0 4px' }}>
                    <div style={{ width:'52px', height:'52px', borderRadius:'50%',
                      border:'1px solid var(--line)', background:'var(--paper)',
                      display:'flex', alignItems:'center', justifyContent:'center' }}>
                      <p.Icon size={24} />
                    </div>
                    <div style={{ fontFamily:"'Inter',sans-serif", fontSize:'10.5px',
                      fontWeight:600, color:'var(--gold-deep)',
                      letterSpacing:'0.18em', textTransform:'uppercase',
                      marginTop:'2px', textWrap:'balance', maxWidth:'150px',
                      lineHeight:1.35 }}>
                      {p.label}
                    </div>
                    <p style={{ fontFamily:"'Inter',sans-serif", fontWeight:400,
                      fontSize:'12px', color:'var(--ink-2)', lineHeight:1.5,
                      maxWidth:'180px', textWrap:'balance' }}>
                      {p.desc}
                    </p>
                  </div>
                  {i < steps.length - 1 && (
                    <div style={{ display:'flex', alignItems:'center',
                      justifyContent:'center', height:'52px', minWidth:'30px' }}>
                      <svg width="100%" height="2" viewBox="0 0 100 2"
                        preserveAspectRatio="none">
                        <line x1="0" y1="1" x2="42" y2="1"
                          stroke="var(--line)" strokeWidth="1" strokeDasharray="2 3"/>
                        <circle cx="50" cy="1" r="1.6" fill="var(--gold)"/>
                        <line x1="58" y1="1" x2="100" y2="1"
                          stroke="var(--line)" strokeWidth="1" strokeDasharray="2 3"/>
                      </svg>
                    </div>
                  )}
                </React.Fragment>
              ))}
            </div>
          </div>
        </Reveal>

      </div>
    </section>
  );
}

Object.assign(window, { CTASection });
