// Trainin Center — Page « Contact » (/contact)
// Réutilise le design system (tc-system) + NavBar/Footer (tc-sections).
// Sections : Hero · Formulaire + coordonnées (encart vert profond) · Orientation.

const isMobCt = (d) => d === 'mobile';

const CT_PROGRAMME_URL = '#'; // opens popup

const CT_COORDS = [
  { k: 'Téléphone', v: '01 86 26 19 48', href: 'tel:0186261948', icon: 'phone' },
  { k: 'WhatsApp',  v: '06 44 64 35 37', href: 'https://wa.me/33644643537', icon: 'whatsapp' },
  { k: 'Email',     v: 'contact@trainin-center.com', href: 'mailto:contact@trainin-center.com', icon: 'mail' },
];

const CT_SOCIALS = [
  { name: 'Facebook',  href: 'https://facebook.com',  svg: '<svg viewBox="0 0 24 24" fill="currentColor" width="100%" height="100%"><path d="M14 8.5V6.8c0-.8.2-1.3 1.4-1.3H17V2.7c-.3 0-1.3-.1-2.4-.1-2.4 0-4 1.5-4 4.1v1.8H8.2v3h2.4V21H14v-9.5h2.5l.4-3H14z"/></svg>' },
  { name: 'Instagram', href: 'https://instagram.com', svg: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="100%" height="100%"><rect x="3" y="3" width="18" height="18" rx="5"/><circle cx="12" cy="12" r="4"/><circle cx="17.5" cy="6.5" r="1.2" fill="currentColor" stroke="none"/></svg>' },
  { name: 'TikTok',    href: 'https://tiktok.com',    svg: '<svg viewBox="0 0 24 24" fill="currentColor" width="100%" height="100%"><path d="M16.5 2h-3v13.2a2.7 2.7 0 1 1-2.2-2.65V9.5a5.85 5.85 0 1 0 5.2 5.8V8.65a6.7 6.7 0 0 0 4 1.3V6.9a3.85 3.85 0 0 1-4-3.9V2z"/></svg>' },
  { name: 'YouTube',   href: 'https://youtube.com',   svg: '<svg viewBox="0 0 24 24" fill="currentColor" width="100%" height="100%"><path d="M22.5 7.2c-.25-1-1-1.7-2-2C18.7 4.8 12 4.8 12 4.8s-6.7 0-8.5.4c-1 .3-1.75 1-2 2C1.1 9 1.1 12 1.1 12s0 3 .4 4.8c.25 1 1 1.7 2 2 1.8.4 8.5.4 8.5.4s6.7 0 8.5-.4c1-.3 1.75-1 2-2 .4-1.8.4-4.8.4-4.8s0-3-.4-4.8zM9.9 15.3V8.7l5.7 3.3-5.7 3.3z"/></svg>' },
];

function CtIcon({ name, size = 18, color }) {
  const c = color || TC.white;
  const common = { width: size, height: size, viewBox: '0 0 24 24', fill: 'none' };
  if (name === 'phone') return (
    <svg {...common}><path d="M6.5 3.5h3l1.4 4-2 1.4a12 12 0 005.2 5.2l1.4-2 4 1.4v3a2 2 0 01-2.2 2A16 16 0 014.5 5.7a2 2 0 012-2.2z" stroke={c} strokeWidth="1.5" strokeLinejoin="round"/></svg>
  );
  if (name === 'whatsapp') return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill={c}><path d="M12.04 2C6.58 2 2.13 6.45 2.13 11.91c0 1.75.46 3.45 1.32 4.95L2 22l5.25-1.38a9.9 9.9 0 004.79 1.22h.01c5.46 0 9.91-4.45 9.91-9.91A9.83 9.83 0 0019.07 4.9 9.83 9.83 0 0012.04 2zm4.5 11.98c-.25-.12-1.46-.72-1.69-.8-.23-.08-.39-.12-.56.12-.16.25-.64.8-.79.97-.14.16-.29.18-.54.06-.25-.12-1.04-.38-1.98-1.22-.73-.65-1.23-1.46-1.37-1.71-.14-.25-.01-.38.11-.5.11-.11.25-.29.37-.43.12-.14.16-.25.25-.41.08-.16.04-.31-.02-.43-.06-.12-.56-1.35-.77-1.85-.2-.49-.41-.42-.56-.43-.14-.01-.31-.01-.48-.01-.16 0-.43.06-.66.31-.23.25-.86.85-.86 2.07 0 1.22.89 2.4 1.01 2.56.12.16 1.74 2.66 4.22 3.74.59.25 1.05.41 1.41.52.59.19 1.13.16 1.56.1.47-.07 1.46-.6 1.66-1.17.21-.58.21-1.07.14-1.18-.06-.1-.22-.16-.47-.28z"/></svg>
  );
  if (name === 'mail') return (
    <svg {...common}><rect x="3" y="5" width="18" height="14" rx="2.5" stroke={c} strokeWidth="1.5"/><path d="M4 7l8 5.5L20 7" stroke={c} strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"/></svg>
  );
  return (
    <svg {...common}><circle cx="12" cy="12" r="9" stroke={c} strokeWidth="1.5"/><path d="M3 12h18M12 3c2.6 2.4 2.6 15.6 0 18M12 3c-2.6 2.4-2.6 15.6 0 18" stroke={c} strokeWidth="1.5"/></svg>
  );
}

// ════════════════════════════════════════════════════════════
// HERO
// ════════════════════════════════════════════════════════════
function ContactHero({ device }) {
  const m = isMobCt(device);
  return (
    <section style={{ background: TC.off, position: 'relative', overflow: 'hidden' }} data-screen-label="Contact · hero">
      <div style={{
        position: 'absolute', top: m ? -130 : -260, right: m ? -170 : -180,
        width: m ? 400 : 740, height: m ? 400 : 740, borderRadius: '50%',
        background: `radial-gradient(circle, ${TC.sageSoft} 0%, ${TC.off} 65%)`,
        pointerEvents: 'none',
      }} />
      <div style={m
        ? { position: 'relative', padding: '44px 22px 16px' }
        : { position: 'relative', maxWidth: TC_CONTAINER, margin: '0 auto', width: '100%', padding: '60px 80px 44px' }}>
        <div style={{ maxWidth: 880 }}>
          <div style={{ marginBottom: m ? 18 : 22 }}>
            <Eyebrow variant="sage" size={m ? 'sm' : 'md'}>Contact</Eyebrow>
          </div>
          <h1 style={{
            fontFamily: fontSans, fontSize: m ? 40 : 64, fontWeight: 500,
            letterSpacing: m ? -1.4 : -2.2, lineHeight: m ? 1.08 : 1.04,
            margin: '0 0 22px', color: TC.ink, textWrap: 'balance',
          }}>
            Contactez l'école{m ? ' ' : <br />}
            <span style={{
              fontFamily: fontSerif, fontWeight: 400, fontStyle: 'italic',
              color: TC.sageDeep, fontSize: m ? 44 : 72, letterSpacing: m ? -1.4 : -2,
            }}>Trainin Center.</span>
          </h1>
          <p style={{
            fontFamily: fontSans, fontSize: m ? 16.5 : 19, lineHeight: 1.6,
            color: TC.ink2, margin: 0, maxWidth: 640, letterSpacing: -0.1,
          }}>
            Vous avez une question sur une formation, un parcours, une inscription
            ou le programme ? L'équipe Trainin Center vous répond et vous oriente
            vers la solution la plus adaptée à votre projet.
          </p>
        </div>
      </div>
    </section>
  );
}

// ════════════════════════════════════════════════════════════
// FORMULAIRE + COORDONNÉES
// ════════════════════════════════════════════════════════════
function ContactField({ label, type = 'text', placeholder, textarea, name, id, required, rows = 5 }) {
  return (
    <div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
      <label style={{ fontFamily: fontSans, fontSize: 12.5, fontWeight: 500, color: TC.ink2, letterSpacing: 0.1 }}>{label}</label>
      {textarea ? (
        <textarea name={name} id={id} placeholder={placeholder} rows={rows} style={{
          padding: '14px 16px', background: TC.white,
          border: `1px solid ${TC.hairStrong}`, borderRadius: 14,
          fontFamily: fontSans, fontSize: 15, color: TC.ink, outline: 'none', resize: 'none',
        }} />
      ) : (
        <input type={type} name={name} id={id} required={required} placeholder={placeholder} style={{
          height: 50, padding: '0 16px', background: TC.white,
          border: `1px solid ${TC.hairStrong}`, borderRadius: 999,
          fontFamily: fontSans, fontSize: 15, color: TC.ink, outline: 'none',
        }} />
      )}
    </div>
  );
}

const CT_ACTION = 'https://trainin-center.learnybox.com/form/6a4028343d620meoghjbmgk4k7bw6m3fzp/';
const CT_RGPD_NOTICE = "En indiquant votre adresse mail, vous acceptez de recevoir une réponse à votre demande ainsi que des informations de la part de Trainin Center. Vous pouvez vous désinscrire à tout moment via les liens de désinscription présents dans nos emails.";

function ContactMain({ device }) {
  const m = isMobCt(device);
  return (
    <Section tone="off" padY={m ? [20, 56] : [80, 88]} padX={m ? 22 : 80} id="formulaire" style={{ scrollMarginTop: m ? 76 : 96 }}>
      <div style={{
        display: m ? 'flex' : 'grid',
        flexDirection: m ? 'column' : undefined,
        gridTemplateColumns: m ? undefined : '1.1fr 0.9fr',
        gap: m ? 22 : 40, alignItems: 'stretch',
        maxWidth: 1180, margin: '0 auto',
      }}>
        {/* FORMULAIRE — fond clair, bordures fines */}
        <form method="post" acceptCharset="UTF-8" action={CT_ACTION} data-cta="contact:submit" style={{
          background: TC.white, borderRadius: 24, border: `1px solid ${TC.hair}`,
          padding: m ? '24px 22px' : '40px 40px',
          boxShadow: '0 1px 2px rgba(0,0,0,0.03), 0 18px 50px -24px rgba(26,31,28,0.18)',
          display: 'flex', flexDirection: 'column',
        }}>
          <h2 style={{
            fontFamily: fontSans, fontSize: m ? 22 : 26, fontWeight: 600,
            color: TC.ink, letterSpacing: -0.5, lineHeight: 1.2, margin: '0 0 6px',
          }}>Écrivez-nous</h2>
          <p style={{ fontFamily: fontSans, fontSize: m ? 14.5 : 15, lineHeight: 1.55, color: TC.ink2, margin: '0 0 24px', letterSpacing: -0.05 }}>
            Remplissez ce formulaire, l'équipe vous recontacte rapidement.
          </p>
          <input type="hidden" name="form_name" value="Formulaire Demande d’informations formation" />
          <div style={{ display: 'grid', gridTemplateColumns: '1fr', gap: 16 }}>
            <ContactField label="Nom / prénom" name="fname" id="form_input572050" placeholder="Votre nom et prénom *" required />
            <ContactField label="Téléphone" type="text" name="tel" id="form_input572055" placeholder="Votre numéro de téléphone *" required />
            <ContactField label="Email" type="email" name="email" id="form_input572051" placeholder="Votre adresse email *" required />
            <ContactField label="Message" textarea name="custom[message]" id="form_input572057" placeholder="Parlez-nous de votre projet…" />
          </div>
          <label htmlFor="rgpd572052" style={{ display: 'flex', alignItems: 'flex-start', gap: 10, cursor: 'pointer', marginTop: 16 }}>
            <input type="checkbox" value="1" name="rgpd" id="rgpd572052" data-no-uniform="true" required style={{ marginTop: 3, width: 16, height: 16, accentColor: TC.sage, flexShrink: 0 }} />
            <span style={{ fontFamily: fontSans, fontSize: 12.5, lineHeight: 1.5, color: TC.ink3, letterSpacing: -0.02 }}>{CT_RGPD_NOTICE}</span>
            <input type="hidden" name="rgpd_notice" value={CT_RGPD_NOTICE} />
          </label>
          <button type="submit" id="btn_submit_572053" data-cta="contact:submit" style={{
            height: 54, marginTop: 24, border: 'none', borderRadius: 999,
            background: TC.sage, color: TC.white, cursor: 'pointer',
            fontFamily: fontSans, fontSize: 15.5, fontWeight: 600, letterSpacing: -0.15,
            display: 'inline-flex', alignItems: 'center', justifyContent: 'center', gap: 9,
            boxShadow: '0 1px 2px rgba(0,0,0,0.06), 0 8px 22px rgba(90,145,105,0.28)',
          }}>
            Envoyer ma demande
            <svg width="15" height="15" viewBox="0 0 14 14" fill="none">
              <path d="M3 7h8M8 4l3 3-3 3" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"/>
            </svg>
          </button>
          <div style={{ display: 'flex', alignItems: 'flex-start', gap: 9, marginTop: 16 }}>
            <span style={{ marginTop: 1, flexShrink: 0 }}><IconCheck size={15} color={TC.sage} /></span>
            <span style={{ fontFamily: fontSans, fontSize: 12.5, lineHeight: 1.5, color: TC.ink3, letterSpacing: -0.02 }}>
              Vos informations restent confidentielles. L'équipe Trainin Center vous
              recontactera dans les meilleurs délais.
            </span>
          </div>
        </form>

        {/* COORDONNÉES — encart vert profond (cf. programme PDF) */}
        <div style={{
          position: 'relative', overflow: 'hidden',
          borderRadius: 24,
          background: `linear-gradient(155deg, ${TC.sage} 0%, ${TC.sageInk} 100%)`,
          padding: m ? '28px 24px' : '40px 38px',
          display: 'flex', flexDirection: 'column',
        }}>
          <div style={{ position: 'absolute', top: -90, right: -90, width: 240, height: 240, borderRadius: '50%', background: 'radial-gradient(circle, rgba(255,255,255,0.12) 0%, transparent 70%)' }} />
          <div style={{ position: 'relative', display: 'flex', flexDirection: 'column', height: '100%' }}>
            <div style={{ fontFamily: fontMono, fontSize: 11, letterSpacing: 1.4, textTransform: 'uppercase', color: 'rgba(255,255,255,0.7)', marginBottom: 14 }}>
              Coordonnées
            </div>
            <h2 style={{
              fontFamily: fontSans, fontSize: m ? 22 : 26, fontWeight: 600,
              color: TC.white, letterSpacing: -0.5, lineHeight: 1.2, margin: '0 0 8px',
            }}>Contacter l'école</h2>
            <p style={{ fontFamily: fontSans, fontSize: m ? 14.5 : 15, lineHeight: 1.55, color: 'rgba(255,255,255,0.8)', margin: '0 0 26px', letterSpacing: -0.05 }}>
              Par téléphone, WhatsApp ou email choisissez le canal qui vous convient.
            </p>

            <div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
              {CT_COORDS.map((c) => (
                <a key={c.k} href={c.href} style={{
                  display: 'flex', alignItems: 'center', gap: 14,
                  padding: m ? '13px 15px' : '14px 18px',
                  background: TC.white, borderRadius: 14,
                  boxShadow: '0 1px 2px rgba(0,0,0,0.05), 0 6px 18px -10px rgba(0,0,0,0.25)',
                  textDecoration: 'none',
                }}>
                  <span style={{
                    flexShrink: 0, width: 42, height: 42, borderRadius: 12,
                    background: TC.sageMist,
                    display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
                  }}>
                    <CtIcon name={c.icon} size={19} color={TC.sageDeep} />
                  </span>
                  <span style={{ minWidth: 0, flex: 1 }}>
                    <span style={{ display: 'block', fontFamily: fontMono, fontSize: 10.5, letterSpacing: 0.8, textTransform: 'uppercase', color: TC.ink3 }}>{c.k}</span>
                    <span style={{ display: 'block', fontFamily: fontSans, fontSize: c.icon === 'mail' ? (m ? 14 : 15.5) : (m ? 16 : 17.5), fontWeight: 600, color: TC.ink, letterSpacing: -0.3, marginTop: 3, lineHeight: 1.2, whiteSpace: 'nowrap' }}>{c.v}</span>
                  </span>
                  <svg width="13" height="13" viewBox="0 0 14 14" fill="none" style={{ flexShrink: 0 }}>
                    <path d="M5 3l4 4-4 4" stroke={TC.ink3} strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"/>
                  </svg>
                </a>
              ))}
              {/* Réseaux sociaux — à la place du lien « Site » */}
              <div style={{
                display: 'flex', alignItems: 'center', gap: 14,
                padding: m ? '16px 0 4px' : '18px 0 4px',
                borderTop: '1px solid rgba(255,255,255,0.14)',
              }}>
                <span style={{
                  flexShrink: 0, width: 42, height: 42, borderRadius: 12,
                  background: 'rgba(255,255,255,0.12)',
                  display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
                }}>
                  <CtIcon name="globe" size={19} color={TC.white} />
                </span>
                <span style={{ minWidth: 0 }}>
                  <span style={{ display: 'block', fontFamily: fontMono, fontSize: 10.5, letterSpacing: 0.8, textTransform: 'uppercase', color: 'rgba(255,255,255,0.62)', marginBottom: 8 }}>Suivez-nous</span>
                  <span style={{ display: 'flex', gap: 9 }}>
                    {CT_SOCIALS.map((s) => (
                      <a key={s.name} href={s.href} aria-label={s.name} style={{
                        width: 38, height: 38, borderRadius: 19,
                        background: 'rgba(255,255,255,0.12)', color: TC.white,
                        display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
                        textDecoration: 'none',
                      }}>
                        <span style={{ width: 18, height: 18, display: 'block' }} dangerouslySetInnerHTML={{ __html: s.svg }} />
                      </a>
                    ))}
                  </span>
                </span>
              </div>
            </div>

            <div style={{ flex: 1, minHeight: m ? 18 : 24 }} />
            <div style={{ display: 'inline-flex', alignItems: 'center', gap: 9, padding: '9px 14px', background: 'rgba(255,255,255,0.12)', borderRadius: 999, alignSelf: 'flex-start' }}>
              <span style={{ width: 7, height: 7, borderRadius: 4, background: '#8fe0a3', display: 'inline-block' }} />
              <span style={{ fontFamily: fontSans, fontSize: 12.5, color: TC.white, fontWeight: 500 }}>Réponse rapide · du lundi au vendredi, 9h–18h</span>
            </div>
          </div>
        </div>
      </div>
    </Section>
  );
}

// ════════════════════════════════════════════════════════════
// ORIENTATION
// ════════════════════════════════════════════════════════════
function ContactOrientation({ device }) {
  const m = isMobCt(device);
  return (
    <Section tone="white" padY={m ? [48, 64] : [40, 96]} padX={m ? 22 : 80}>
      <div style={{
        maxWidth: 1180, margin: '0 auto',
        background: TC.off, borderRadius: 24, border: `1px solid ${TC.hair}`,
        padding: m ? '30px 24px' : '52px 56px',
        display: m ? 'flex' : 'grid',
        flexDirection: m ? 'column' : undefined,
        gridTemplateColumns: m ? undefined : '1fr auto',
        gap: m ? 22 : 48, alignItems: 'center',
      }}>
        <div style={{ maxWidth: 640 }}>
          <h2 style={{
            fontFamily: fontSans, fontSize: m ? 24 : 30, fontWeight: 600,
            color: TC.ink, letterSpacing: -0.7, lineHeight: 1.18, margin: '0 0 12px', textWrap: 'balance',
          }}>Vous ne savez pas quelle formation choisir ?</h2>
          <p style={{ fontFamily: fontSans, fontSize: m ? 15.5 : 16.5, lineHeight: 1.6, color: TC.ink2, margin: 0, letterSpacing: -0.05 }}>
            Chaque projet est différent. Vous pouvez nous écrire pour être
            orienté·e vers le parcours le plus adapté : formation Praticien·ne
            naturopathe, Fréquences énergétiques ou modules à la carte.
          </p>
        </div>
        <a href="#" onClick={(e) => { e.preventDefault(); window.dispatchEvent(new Event('tc-open-programme')); }} style={{ textDecoration: 'none', display: m ? 'block' : 'inline-block' }}>
          <span style={{
            height: 54, padding: '0 28px', boxSizing: 'border-box',
            background: TC.sage, color: TC.white, borderRadius: 999,
            fontFamily: fontSans, fontSize: 15.5, fontWeight: 600, letterSpacing: -0.15,
            display: 'inline-flex', alignItems: 'center', justifyContent: 'center', gap: 9,
            width: m ? '100%' : 'auto',
            boxShadow: '0 1px 2px rgba(0,0,0,0.06), 0 8px 22px rgba(90,145,105,0.28)',
          }} data-cta="goto:programme">
            Recevoir le programme complet
            <svg width="15" height="15" viewBox="0 0 14 14" fill="none" style={{ flexShrink: 0 }}>
              <path d="M3 7h8M8 4l3 3-3 3" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"/>
            </svg>
          </span>
        </a>
      </div>
    </Section>
  );
}

// ════════════════════════════════════════════════════════════
// PAGE
// ════════════════════════════════════════════════════════════
function ContactPage({ device }) {
  // Le contenu est rendu côté client (Babel in-browser) : au chargement direct
  // de contact.html#formulaire, l'ancre native du navigateur se déclenche
  // avant que #formulaire n'existe dans le DOM et échoue silencieusement.
  // On rejoue le scroll une fois le composant monté.
  React.useEffect(() => {
    if (!location.hash) return;
    const el = document.getElementById(location.hash.slice(1));
    if (el) el.scrollIntoView({ block: 'start' });
  }, []);
  return (
    <div style={{ background: TC.white, width: '100%', fontFamily: fontSans, color: TC.ink }}>
      <NavBar device={device} />
      <ContactHero device={device} />
      <ContactMain device={device} />
      <ContactOrientation device={device} />
      <Footer device={device} />
    </div>
  );
}

Object.assign(window, { ContactPage });
