// contact.jsx — Contact

const { useState: useStateC } = React;

function PlatformIcon({ kind, size = 20 }) {
  const paths = {
    tt: 'M12.525.02c1.31-.02 2.61-.01 3.91-.02.08 1.53.63 3.09 1.75 4.17 1.12 1.11 2.7 1.62 4.24 1.79v4.03c-1.44-.05-2.89-.35-4.2-.97-.57-.26-1.1-.59-1.62-.93-.01 2.92.01 5.84-.02 8.75-.08 1.4-.54 2.79-1.35 3.94-1.31 1.92-3.58 3.17-5.91 3.21-1.43.08-2.86-.31-4.08-1.03-2.02-1.19-3.44-3.37-3.65-5.71-.02-.5-.03-1-.01-1.49.18-1.9 1.12-3.72 2.58-4.96 1.66-1.44 3.98-2.13 6.15-1.72.02 1.48-.04 2.96-.04 4.44-.99-.32-2.15-.23-3.02.37-.63.41-1.11 1.04-1.36 1.75-.21.51-.15 1.07-.14 1.61.24 1.64 1.82 3.02 3.5 2.87 1.12-.01 2.19-.66 2.77-1.61.19-.33.4-.67.41-1.06.1-1.79.06-3.57.07-5.36.01-4.03-.01-8.05.02-12.07z',
    ig: 'M12 2.163c3.204 0 3.584.012 4.85.07 3.252.148 4.771 1.691 4.919 4.919.058 1.265.069 1.645.069 4.849 0 3.205-.012 3.584-.069 4.849-.149 3.225-1.664 4.771-4.919 4.919-1.266.058-1.644.07-4.85.07-3.204 0-3.584-.012-4.849-.07-3.26-.149-4.771-1.699-4.919-4.92-.058-1.265-.07-1.644-.07-4.849 0-3.204.013-3.583.07-4.849.149-3.227 1.664-4.771 4.919-4.919 1.266-.057 1.645-.069 4.849-.069zm0-2.163C8.741 0 8.333.014 7.053.072 2.695.272.273 2.69.073 7.052.014 8.333 0 8.741 0 12c0 3.259.014 3.668.072 4.948.2 4.358 2.618 6.78 6.98 6.98C8.333 23.986 8.741 24 12 24c3.259 0 3.668-.014 4.948-.072 4.354-.2 6.782-2.618 6.979-6.98.059-1.28.073-1.689.073-4.948 0-3.259-.014-3.667-.072-4.947-.196-4.354-2.617-6.78-6.979-6.98C15.668.014 15.259 0 12 0zm0 5.838a6.162 6.162 0 1 0 0 12.324 6.162 6.162 0 0 0 0-12.324zM12 16a4 4 0 1 1 0-8 4 4 0 0 1 0 8zm6.406-11.845a1.44 1.44 0 1 0 0 2.881 1.44 1.44 0 0 0 0-2.881z',
    yt: 'M23.498 6.186a3.016 3.016 0 0 0-2.122-2.136C19.505 3.545 12 3.545 12 3.545s-7.505 0-9.377.505A3.017 3.017 0 0 0 .502 6.186C0 8.07 0 12 0 12s0 3.93.502 5.814a3.016 3.016 0 0 0 2.122 2.136c1.871.505 9.376.505 9.376.505s7.505 0 9.377-.505a3.015 3.015 0 0 0 2.122-2.136C24 15.93 24 12 24 12s0-3.93-.502-5.814zM9.545 15.568V8.432L15.818 12l-6.273 3.568z',
    fb: 'M9.101 23.691v-7.98H6.627v-3.667h2.474v-1.58c0-4.085 1.848-5.978 5.858-5.978.401 0 .955.042 1.468.103a8.68 8.68 0 0 1 1.141.195v3.325a8.623 8.623 0 0 0-.653-.036 26.805 26.805 0 0 0-.733-.009c-.707 0-1.259.096-1.675.309a1.686 1.686 0 0 0-.679.622c-.258.42-.374.995-.374 1.752v1.297h3.919l-.386 2.103-.287 1.564h-3.246v8.245C19.396 23.238 24 18.179 24 12.044c0-6.627-5.373-12-12-12s-12 5.373-12 12c0 5.628 3.874 10.35 9.101 11.647Z',
  };
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
      <path d={paths[kind]} />
    </svg>
  );
}

function ContactPage({ onNav }) {
  return (
    <div className="page">
      <ContactHero onNav={onNav} />
      <ContactMain onNav={onNav} />
      <ContactFAQ onNav={onNav} />
    </div>
  );
}

function ContactHero({ onNav }) {
  return (
    <section style={{ minHeight: '40vh', position: 'relative', overflow: 'hidden', display: 'flex', alignItems: 'center', paddingTop: 120, paddingBottom: 40 }}>
      <Mesh />
      <Particles count={30} />
      <div className="container" style={{ position: 'relative', zIndex: 2 }}>
        <Reveal>
          <div className="pill" style={{ marginBottom: 24 }}>
            <span className="dot" />
            <span className="mono" style={{ fontSize: 11, letterSpacing: '0.05em' }}>CONTACT</span>
          </div>
        </Reveal>
        <Reveal delay={120}>
          <h1 className="display" style={{ fontSize: 'clamp(44px, 6.5vw, 92px)', margin: 0 }}>
            Hai să <span style={{ background: 'linear-gradient(135deg, var(--accent-1), var(--accent-2))', WebkitBackgroundClip: 'text', WebkitTextFillColor: 'transparent', backgroundClip: 'text' }}>vorbim</span>.
          </h1>
        </Reveal>
        <Reveal delay={220}>
          <p style={{ fontSize: 'clamp(18px, 1.4vw, 22px)', color: 'var(--txt-2)', marginTop: 24, maxWidth: 620, lineHeight: 1.55 }}>
            Răspundem la fiecare mesaj în <strong style={{ color: 'var(--txt-0)' }}>maxim 24h</strong>.
          </p>
        </Reveal>
        <Reveal delay={300}>
          <div style={{ marginTop: 28 }}><BackButton onNav={onNav} /></div>
        </Reveal>
      </div>
    </section>
  );
}

function ContactMain({ onNav }) {
  const [form, setForm] = useStateC({ name: '', email: '', subject: 'Vreau să mă înscriu', message: '', website_url: '' });
  const [sent, setSent] = useStateC(false);
  const [loading, setLoading] = useStateC(false);
  const [errorMsg, setErrorMsg] = useStateC('');

  const submit = async (e) => {
    e.preventDefault();
    // Honeypot — bot a completat câmpul invizibil; afișăm success silent fără să trimitem nimic
    if (form.website_url) {
      setSent(true);
      setTimeout(() => setSent(false), 3500);
      setForm({ name: '', email: '', subject: 'Vreau să mă înscriu', message: '', website_url: '' });
      return;
    }
    setLoading(true);
    setErrorMsg('');
    try {
      const { error } = await window.sb.from('contact_messages').insert({
        name: form.name.trim(),
        email: form.email.trim().toLowerCase(),
        subject: form.subject,
        message: form.message.trim(),
      });
      if (error) {
        console.error('[4U Media] Eroare submit contact:', error);
        setErrorMsg('A apărut o eroare la trimitere. Te rugăm să încerci din nou sau să ne contactezi pe email.');
        setLoading(false);
        return;
      }
      setLoading(false);
      setSent(true);
      setTimeout(() => setSent(false), 3500);
      setForm({ name: '', email: '', subject: 'Vreau să mă înscriu', message: '', website_url: '' });
    } catch (err) {
      console.error('[4U Media] Network error submit contact:', err);
      setErrorMsg('Conexiune eșuată. Verifică internetul și încearcă din nou.');
      setLoading(false);
    }
  };

  const items = [
    { ico: '✉', label: 'Email', value: 'contact@4uagency.ro', sub: 'Răspuns în <24h' },
    { ico: '📍', label: 'Sediu', value: 'Cluj-Napoca & București, România', sub: 'Sediile 4U Agency' },
    { ico: '⏰', label: 'Program', value: 'Luni — Vineri', sub: '09:00 — 18:00 (EET)' },
  ];

  return (
    <section style={{ padding: '40px 0 100px', position: 'relative' }}>
      <div className="container">
        <div style={{ display: 'grid', gridTemplateColumns: '1fr 1.2fr', gap: 32 }} className="ct-grid">
          {/* LEFT — contact info */}
          <Reveal>
            <div style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
              <div className="card-glass" style={{ padding: 28 }}>
                <div className="mono" style={{ fontSize: 11, color: 'var(--accent-1)', letterSpacing: '0.15em', marginBottom: 20 }}>● DATE CONTACT</div>
                <div style={{ display: 'flex', flexDirection: 'column', gap: 18 }}>
                  {items.map(it => (
                    <div key={it.label} style={{ display: 'flex', alignItems: 'center', gap: 14 }}>
                      <div style={{ width: 44, height: 44, borderRadius: 10, background: 'rgba(82,242,15,.08)', border: '.5px solid rgba(82,242,15,.2)', display: 'grid', placeItems: 'center', fontSize: 18, color: 'var(--accent-1)', flexShrink: 0 }}>{it.ico}</div>
                      <div>
                        <div className="mono" style={{ fontSize: 10, color: 'var(--txt-3)', letterSpacing: '0.1em', marginBottom: 4 }}>{it.label.toUpperCase()}</div>
                        <div style={{ fontSize: 15, fontWeight: 500 }}>{it.value}</div>
                        <div style={{ fontSize: 12, color: 'var(--txt-3)' }}>{it.sub}</div>
                      </div>
                    </div>
                  ))}
                </div>
              </div>
              {/* Social */}
              <div className="card-glass" style={{ padding: 24 }}>
                <div className="mono" style={{ fontSize: 11, color: 'var(--accent-1)', letterSpacing: '0.15em', marginBottom: 16 }}>● URMĂREȘTE-NE</div>
                <div style={{ display: 'flex', gap: 8 }}>
                  {[['tt','TikTok'],['ig','Instagram'],['yt','YouTube'],['fb','Facebook']].map(([s, t]) => (
                    <a key={s} className="hov" title={t} style={{ flex: 1, height: 44, borderRadius: 10, display: 'grid', placeItems: 'center', background: 'rgba(255,255,255,.03)', border: '.5px solid var(--line)', color: 'var(--txt-2)', cursor: 'pointer', transition: 'all .2s' }}
                       onMouseEnter={(e) => { e.currentTarget.style.color = 'var(--accent-1)'; e.currentTarget.style.borderColor = 'var(--accent-1)'; }}
                       onMouseLeave={(e) => { e.currentTarget.style.color = 'var(--txt-2)'; e.currentTarget.style.borderColor = 'var(--line)'; }}>
                      <PlatformIcon kind={s} size={20} />
                    </a>
                  ))}
                </div>
              </div>
              {/* Map placeholder */}
              <div className="card-glass" style={{ padding: 0, overflow: 'hidden', height: 200, position: 'relative' }}>
                <div style={{ position: 'absolute', inset: 0, background: 'linear-gradient(135deg, rgba(82,242,15,.10), rgba(255,215,0,.06)), repeating-linear-gradient(0deg, transparent, transparent 19px, rgba(82,242,15,.08) 19px, rgba(82,242,15,.08) 20px), repeating-linear-gradient(90deg, transparent, transparent 19px, rgba(82,242,15,.08) 19px, rgba(82,242,15,.08) 20px)' }} />
                {/* Pin */}
                <div style={{ position: 'absolute', top: '45%', left: '52%', transform: 'translate(-50%, -100%)' }}>
                  <div style={{ width: 14, height: 14, borderRadius: 999, background: 'var(--accent-1)', boxShadow: '0 0 0 6px rgba(82,242,15,.25), 0 0 24px rgba(82,242,15,.6)', animation: 'pulse 1.6s infinite' }} />
                </div>
                <div className="mono" style={{ position: 'absolute', bottom: 12, left: 16, fontSize: 10, color: 'var(--txt-2)', letterSpacing: '0.15em' }}>● ROMÂNIA</div>
              </div>
            </div>
          </Reveal>

          {/* RIGHT — form */}
          <Reveal delay={150}>
            <form onSubmit={submit} className="card-glass" style={{ padding: 36, position: 'relative' }}>
              <div className="mono" style={{ fontSize: 11, color: 'var(--accent-1)', letterSpacing: '0.15em', marginBottom: 8 }}>● TRIMITE UN MESAJ</div>
              <h2 className="display" style={{ fontSize: 28, margin: '0 0 28px' }}>Spune-ne ce te aduce aici.</h2>
              <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 14, marginBottom: 14 }} className="ct-form-row">
                <div>
                  <label className="mono" style={{ fontSize: 10, color: 'var(--txt-3)', letterSpacing: '0.1em', marginBottom: 6, display: 'block' }}>NUME COMPLET</label>
                  <input className="input" required value={form.name} onChange={e => setForm({...form, name: e.target.value})} placeholder="Ion Popescu" />
                </div>
                <div>
                  <label className="mono" style={{ fontSize: 10, color: 'var(--txt-3)', letterSpacing: '0.1em', marginBottom: 6, display: 'block' }}>EMAIL</label>
                  <input className="input" type="email" required value={form.email} onChange={e => setForm({...form, email: e.target.value})} placeholder="ion@email.ro" />
                </div>
              </div>
              <div style={{ marginBottom: 14 }}>
                <label className="mono" style={{ fontSize: 10, color: 'var(--txt-3)', letterSpacing: '0.1em', marginBottom: 6, display: 'block' }}>SUBIECT</label>
                <select className="input" value={form.subject} onChange={e => setForm({...form, subject: e.target.value})} style={{ appearance: 'none', color: '#FFFFFF', backgroundColor: '#1a1a1a', backgroundImage: 'linear-gradient(45deg, transparent 50%, var(--accent-1) 50%), linear-gradient(135deg, var(--accent-1) 50%, transparent 50%)', backgroundPosition: 'calc(100% - 18px) 50%, calc(100% - 14px) 50%', backgroundSize: '4px 4px, 4px 4px', backgroundRepeat: 'no-repeat' }}>
                  <option style={{ color: '#FFFFFF', backgroundColor: '#1a1a1a' }}>Vreau să mă înscriu</option>
                  <option style={{ color: '#FFFFFF', backgroundColor: '#1a1a1a' }}>Sunt brand și vreau o campanie</option>
                  <option style={{ color: '#FFFFFF', backgroundColor: '#1a1a1a' }}>Solicitare de parteneriat</option>
                  <option style={{ color: '#FFFFFF', backgroundColor: '#1a1a1a' }}>Altceva</option>
                </select>
              </div>
              <div style={{ marginBottom: 24 }}>
                <label className="mono" style={{ fontSize: 10, color: 'var(--txt-3)', letterSpacing: '0.1em', marginBottom: 6, display: 'block' }}>MESAJ</label>
                <textarea className="input" required rows={5} value={form.message} onChange={e => setForm({...form, message: e.target.value})} placeholder="Spune-ne pe scurt despre tine, ce conținut faci și cum te putem ajuta…" style={{ height: 'auto', padding: 14, fontFamily: 'inherit', resize: 'vertical' }} />
              </div>
              {/* Honeypot — câmp invizibil pentru detectare boți */}
              <input
                type="text"
                name="website_url"
                value={form.website_url}
                onChange={e => setForm({...form, website_url: e.target.value})}
                tabIndex={-1}
                autoComplete="off"
                aria-hidden="true"
                style={{ position: 'absolute', left: -9999, width: 1, height: 1, opacity: 0, pointerEvents: 'none' }}
              />
              {/* Error message */}
              {errorMsg && (
                <div style={{ marginBottom: 16, padding: '12px 14px', borderRadius: 10, background: 'rgba(255,90,90,.06)', border: '.5px solid rgba(255,100,100,.3)', display: 'flex', gap: 10, alignItems: 'flex-start' }}>
                  <span style={{ fontSize: 16, lineHeight: 1.4 }}>⚠️</span>
                  <span style={{ fontSize: 13, color: 'var(--txt-2)', lineHeight: 1.5 }}>{errorMsg}</span>
                </div>
              )}
              <button type="submit" className="btn btn-primary btn-lg" disabled={loading}
                style={{ width: '100%', opacity: loading ? 0.6 : 1, cursor: loading ? 'not-allowed' : 'pointer' }}>
                {sent ? '✓ Mesaj trimis cu succes!' : loading ? (
                  <span style={{ display: 'inline-flex', alignItems: 'center', gap: 8 }}>
                    <span style={{ width: 14, height: 14, border: '2px solid rgba(0,0,0,.2)', borderTopColor: 'currentColor', borderRadius: '50%', animation: 'ct-spin .8s linear infinite', display: 'inline-block' }} />
                    Se trimite...
                  </span>
                ) : 'Trimite mesajul →'}
              </button>
              <style>{`@keyframes ct-spin { to { transform: rotate(360deg); } }`}</style>
              <p style={{ fontSize: 12, color: 'var(--txt-3)', marginTop: 16, textAlign: 'center' }}>
                Prin trimitere ești de acord cu <a className="ulink" style={{ cursor: 'pointer' }}>politica de confidențialitate</a>.
              </p>
            </form>
          </Reveal>
        </div>
      </div>
      <style>{`
        @media (max-width: 900px) { .ct-grid { grid-template-columns: 1fr !important; } .ct-form-row { grid-template-columns: 1fr !important; } }
      `}</style>
    </section>
  );
}

function ContactFAQ({ onNav }) {
  const qs = [
    { q: 'Cât de repede primesc răspuns?', a: 'În maxim 24h în zilele lucrătoare. De obicei mult mai repede.' },
    { q: 'Ce trebuie să trimit pentru înscriere?', a: 'Doar formularul de pe pagina de înscriere — restul detaliilor le aliniem la primul call.' },
    { q: 'Lucrați și cu branduri?', a: 'Da. Conectăm branduri serioase cu vocile potrivite din comunitatea 4U. Trimite mesaj cu subiectul „Pentru companii".' },
  ];
  return (
    <section style={{ padding: '80px 0 120px', borderTop: '.5px solid var(--line-soft)' }}>
      <div className="container">
        <Reveal>
          <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-end', flexWrap: 'wrap', gap: 16, marginBottom: 32 }}>
            <h2 className="display" style={{ fontSize: 'clamp(28px, 3.5vw, 44px)', margin: 0 }}>Întrebări frecvente</h2>
            <button className="btn btn-glass btn-sm" onClick={() => onNav('faq')}>Vezi toate →</button>
          </div>
        </Reveal>
        <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 16 }} className="ct-faq-grid">
          {qs.map((q, i) => (
            <Reveal key={i} delay={i * 80}>
              <div className="card" style={{ padding: 24, height: '100%' }}>
                <div style={{ fontSize: 15, fontWeight: 500, marginBottom: 12, color: 'var(--txt-0)' }}>{q.q}</div>
                <div style={{ fontSize: 13, color: 'var(--txt-2)', lineHeight: 1.6 }}>{q.a}</div>
              </div>
            </Reveal>
          ))}
        </div>
      </div>
      <style>{`@media (max-width: 900px) { .ct-faq-grid { grid-template-columns: 1fr !important; } }`}</style>
    </section>
  );
}

window.ContactPage = ContactPage;
