// RDG — Sections: Services grid, About, Cases, Testimonials, Stack, Pricing, FAQ, Contact
const { useState: useStateS, useEffect: useEffectS } = React;

// ---------- SERVICES ----------
function Services({ t }) {
  return (
    <section id="services" className="rdg-services">
      <div className="container">
        <div className="section-title-block reveal">
          <span className="eyebrow">{t.services.eyebrow}</span>
          <h2 className="h-section">{t.services.title}</h2>
          <p className="lead">{t.services.subtitle}</p>
        </div>

        <div className="rdg-services-grid reveal-stagger">
          {t.services.list.map((s, i) => <ServiceCard key={s.id} s={s} idx={i} t={t} />)}
        </div>
      </div>
    </section>
  );
}

const SERVICE_GLYPHS = {
  aurarh: (
    <svg viewBox="0 0 48 48" fill="none" stroke="currentColor" strokeWidth="1.5">
      <circle cx="24" cy="18" r="6" /><path d="M12 38c0-6.6 5.4-12 12-12s12 5.4 12 12" strokeLinecap="round" />
      <circle cx="36" cy="14" r="2.5" fill="currentColor" />
    </svg>
  ),
  easyserve: (
    <svg viewBox="0 0 48 48" fill="none" stroke="currentColor" strokeWidth="1.5">
      <path d="M10 14h28l-2 24H12z" strokeLinejoin="round" />
      <path d="M16 14V9a8 8 0 0 1 16 0v5" strokeLinecap="round" />
    </svg>
  ),
  noptico: (
    <svg viewBox="0 0 48 48" fill="none" stroke="currentColor" strokeWidth="1.5">
      <path d="M8 32l8-10 8 6 8-14 8 10" strokeLinecap="round" strokeLinejoin="round" />
      <circle cx="40" cy="24" r="2" fill="currentColor" />
    </svg>
  ),
  mecaniquea: (
    <svg viewBox="0 0 48 48" fill="none" stroke="currentColor" strokeWidth="1.5">
      <circle cx="24" cy="24" r="10" />
      <path d="M24 14v-4M24 38v-4M14 24h-4M38 24h-4M16.9 16.9l-2.8-2.8M33.9 33.9l-2.8-2.8M16.9 31.1l-2.8 2.8M33.9 14.1l-2.8 2.8" strokeLinecap="round" />
      <circle cx="24" cy="24" r="3" fill="currentColor" stroke="none" />
    </svg>
  ),
  aivoz: (
    <svg viewBox="0 0 48 48" fill="none" stroke="currentColor" strokeWidth="1.5">
      <path d="M24 8v32M16 14v20M32 14v20M8 20v8M40 20v8" strokeLinecap="round" />
    </svg>
  ),
  clinic: (
    <svg viewBox="0 0 48 48" fill="none" stroke="currentColor" strokeWidth="1.5">
      <path d="M24 10v28M10 24h28" strokeLinecap="round" />
      <rect x="8" y="8" width="32" height="32" rx="4" />
    </svg>
  ),
  entrenard: (
    <svg viewBox="0 0 48 48" fill="none" stroke="currentColor" strokeWidth="1.5">
      <path d="M6 24h4M38 24h4M14 18v12M34 18v12M14 24h20" strokeLinecap="round" />
      <rect x="10" y="20" width="4" height="8" rx="1" fill="currentColor" stroke="none" />
      <rect x="34" y="20" width="4" height="8" rx="1" fill="currentColor" stroke="none" />
    </svg>
  ),
  infragestor: (
    <svg viewBox="0 0 48 48" fill="none" stroke="currentColor" strokeWidth="1.5">
      <rect x="8" y="10" width="32" height="8" rx="2" />
      <rect x="8" y="22" width="32" height="8" rx="2" />
      <rect x="8" y="34" width="32" height="6" rx="2" />
      <circle cx="13" cy="14" r="1" fill="currentColor" />
      <circle cx="13" cy="26" r="1" fill="currentColor" />
      <circle cx="13" cy="37" r="1" fill="currentColor" />
    </svg>
  ),
  manybase: (
    <svg viewBox="0 0 48 48" fill="none" stroke="currentColor" strokeWidth="1.5">
      <ellipse cx="24" cy="12" rx="14" ry="4" />
      <path d="M10 12v10c0 2.2 6.3 4 14 4s14-1.8 14-4V12M10 22v10c0 2.2 6.3 4 14 4s14-1.8 14-4V22" />
    </svg>
  ),
};

function ServiceCard({ s, idx, t }) {
  const num = String(idx + 1).padStart(2, '0');
  return (
    <a
      href={`https://${s.domain}`}
      target="_blank"
      rel="noopener"
      className="rdg-service"
      data-cursor="hover"
    >
      <div className="rdg-service-top">
        <span className="rdg-service-num">{num}</span>
        <span className="rdg-service-tag">{s.tag}</span>
      </div>
      <div className="rdg-service-glyph">{SERVICE_GLYPHS[s.id]}</div>
      <div className="rdg-service-body">
        <h3 className="h-card rdg-service-name">{s.name}</h3>
        <p className="rdg-service-desc">{s.desc}</p>
      </div>
      <div className="rdg-service-foot">
        <span className="rdg-service-metric">{s.metric}</span>
        <span className="rdg-service-link">
          {t.services.visit} <ArrowIcon />
        </span>
      </div>
      <div className="rdg-service-domain">{s.domain}</div>
    </a>
  );
}

// ---------- ABOUT ----------
function About({ t }) {
  return (
    <section id="about" className="rdg-about">
      <div className="container rdg-about-inner">
        <div className="rdg-about-left reveal">
          <span className="eyebrow">{t.about.eyebrow}</span>
          <h2 className="h-section">{t.about.title}</h2>
          <p className="lead">{t.about.body}</p>
        </div>
        <div className="rdg-about-right reveal-stagger">
          {t.about.pillars.map((p, i) => (
            <div key={p.t} className="rdg-pillar">
              <span className="rdg-pillar-num">/{String(i + 1).padStart(2, '0')}</span>
              <h4 className="rdg-pillar-t">{p.t}</h4>
              <p className="rdg-pillar-d">{p.d}</p>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// ---------- CASES ----------
function Cases({ t }) {
  return (
    <section id="cases" className="rdg-cases">
      <div className="container">
        <div className="section-title-block reveal">
          <span className="eyebrow">{t.cases.eyebrow}</span>
          <h2 className="h-section">{t.cases.title}</h2>
        </div>
        <div className="rdg-cases-grid reveal-stagger">
          {t.cases.list.map(c => (
            <div key={c.logo} className="rdg-case">
              <div className="rdg-case-logo">{c.logo}</div>
              <div className="rdg-case-sector">{c.sector}</div>
              <div className="rdg-case-metric">{c.metric}</div>
              <div className="rdg-case-label">{c.metricLabel}</div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// ---------- TESTIMONIALS ----------
function Testimonials({ t }) {
  return (
    <section className="rdg-testimonials">
      <div className="container">
        <div className="section-title-block reveal">
          <span className="eyebrow">{t.testimonials.eyebrow}</span>
          <h2 className="h-section">{t.testimonials.title}</h2>
        </div>
        <div className="rdg-testimonials-grid reveal-stagger">
          {t.testimonials.list.map((q, i) => (
            <figure key={i} className="rdg-quote">
              <svg className="rdg-quote-mark" width="32" height="24" viewBox="0 0 32 24" fill="currentColor"><path d="M0 24V12C0 5 5 0 12 0v4c-3.5 0-6 2.5-7 6h7v14H0zm17 0V12c0-7 5-12 12-12v4c-3.5 0-6 2.5-7 6h7v14H17z"/></svg>
              <blockquote>{q.q}</blockquote>
              <figcaption>
                <strong>{q.a}</strong>
                <span>{q.r}</span>
              </figcaption>
            </figure>
          ))}
        </div>
      </div>
    </section>
  );
}

// ---------- STACK ----------
function Stack({ t }) {
  return (
    <section className="rdg-stack">
      <div className="container rdg-stack-inner">
        <div className="reveal">
          <span className="eyebrow">{t.stack.eyebrow}</span>
          <h2 className="h-section">{t.stack.title}</h2>
          <p className="lead">{t.stack.body}</p>
        </div>
        <div className="rdg-stack-tags reveal-stagger">
          {t.stack.items.map(item => (
            <span key={item} className="rdg-stack-tag">{item}</span>
          ))}
        </div>
      </div>
    </section>
  );
}

// ---------- PRICING ----------
function Pricing({ t }) {
  return (
    <section id="pricing" className="rdg-pricing">
      <div className="container">
        <div className="section-title-block reveal">
          <span className="eyebrow">{t.pricing.eyebrow}</span>
          <h2 className="h-section">{t.pricing.title}</h2>
          <p className="lead">{t.pricing.subtitle}</p>
        </div>
        <div className="rdg-pricing-grid reveal-stagger">
          {t.pricing.plans.map(p => (
            <div key={p.name} className={`rdg-plan ${p.highlight ? 'highlight' : ''}`}>
              {p.highlight && <span className="rdg-plan-badge">★ Recommended</span>}
              <h3 className="rdg-plan-name">{p.name}</h3>
              <div className="rdg-plan-price">
                <span className="rdg-plan-amount">{p.price}</span>
                <span className="rdg-plan-period">{p.period}</span>
              </div>
              <p className="rdg-plan-desc">{p.desc}</p>
              <ul className="rdg-plan-features">
                {p.features.map(f => (
                  <li key={f}>
                    <svg width="16" height="16" viewBox="0 0 16 16" fill="none"><path d="M3 8.5L6.5 12 13 4" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/></svg>
                    {f}
                  </li>
                ))}
              </ul>
              <a href="#contact" className={`btn ${p.highlight ? 'btn-accent' : 'btn-ghost'} rdg-plan-cta`}>{p.cta}</a>
            </div>
          ))}
        </div>
        <p className="rdg-pricing-foot reveal">{t.pricing.footnote}</p>
      </div>
    </section>
  );
}

// ---------- FAQ ----------
function FAQ({ t }) {
  const [open, setOpen] = useStateS(0);
  return (
    <section className="rdg-faq">
      <div className="container rdg-faq-inner">
        <div className="rdg-faq-left reveal">
          <span className="eyebrow">{t.faq.eyebrow}</span>
          <h2 className="h-section">{t.faq.title}</h2>
        </div>
        <div className="rdg-faq-right reveal">
          {t.faq.items.map((it, i) => (
            <div key={i} className={`rdg-faq-item ${open === i ? 'open' : ''}`}>
              <button onClick={() => setOpen(open === i ? -1 : i)} className="rdg-faq-q">
                <span>{it.q}</span>
                <span className="rdg-faq-icon" aria-hidden="true">{open === i ? '−' : '+'}</span>
              </button>
              <div className="rdg-faq-a-wrap">
                <p className="rdg-faq-a">{it.a}</p>
              </div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// ---------- CONTACT ----------
function Contact({ t }) {
  const [state, setState] = useStateS('idle');
  const [form, setForm] = useStateS({ name: '', email: '', company: '', product: '', message: '' });

  const submit = (e) => {
    e.preventDefault();
    setState('sending');
    setTimeout(() => setState('sent'), 1100);
  };

  return (
    <section id="contact" className="rdg-contact">
      <div className="container rdg-contact-inner">
        <div className="rdg-contact-left reveal">
          <span className="eyebrow">{t.contact.eyebrow}</span>
          <h2 className="h-section">{t.contact.title}</h2>
          <p className="lead">{t.contact.subtitle}</p>

          <div className="rdg-contact-info">
            <InfoRow label="General" value={t.contact.info.email} href={`mailto:${t.contact.info.email}`} />
            <InfoRow label="Sales" value={t.contact.info.sales} href={`mailto:${t.contact.info.sales}`} />
            <InfoRow label="Phone" value={t.contact.info.phone} href={`tel:${t.contact.info.phone.replace(/\s/g, '')}`} />
            <InfoRow label="HQ" value={t.contact.info.address} />
          </div>
        </div>

        <form className="rdg-contact-form reveal" onSubmit={submit}>
          {state === 'sent' ? (
            <div className="rdg-contact-success">
              <svg width="48" height="48" viewBox="0 0 48 48" fill="none" stroke="currentColor" strokeWidth="2"><circle cx="24" cy="24" r="22" /><path d="M14 24l8 8 12-16" strokeLinecap="round" strokeLinejoin="round" /></svg>
              <p>{t.contact.sent}</p>
            </div>
          ) : (
            <>
              <Field label={t.contact.name} value={form.name} onChange={v => setForm({ ...form, name: v })} required />
              <div className="rdg-field-row">
                <Field label={t.contact.email} type="email" value={form.email} onChange={v => setForm({ ...form, email: v })} required />
                <Field label={t.contact.company} value={form.company} onChange={v => setForm({ ...form, company: v })} required />
              </div>
              <SelectField label={t.contact.product} value={form.product} onChange={v => setForm({ ...form, product: v })} options={t.services.list.map(s => s.name)} />
              <Field label={t.contact.message} value={form.message} onChange={v => setForm({ ...form, message: v })} textarea />
              <button type="submit" className="btn btn-primary rdg-contact-submit" disabled={state === 'sending'}>
                {state === 'sending' ? t.contact.sending : t.contact.send} <ArrowIcon />
              </button>
            </>
          )}
        </form>
      </div>
    </section>
  );
}

function InfoRow({ label, value, href }) {
  return (
    <div className="rdg-info-row">
      <span className="rdg-info-label">{label}</span>
      {href ? <a href={href}>{value}</a> : <span>{value}</span>}
    </div>
  );
}

function Field({ label, value, onChange, type = 'text', textarea, required }) {
  const [focused, setFocused] = useStateS(false);
  const filled = value && value.length > 0;
  const Tag = textarea ? 'textarea' : 'input';
  return (
    <label className={`rdg-field ${focused ? 'focused' : ''} ${filled ? 'filled' : ''}`}>
      <span className="rdg-field-label">{label}{required && ' *'}</span>
      <Tag
        type={type}
        value={value}
        required={required}
        rows={textarea ? 4 : undefined}
        onChange={e => onChange(e.target.value)}
        onFocus={() => setFocused(true)}
        onBlur={() => setFocused(false)}
      />
    </label>
  );
}

function SelectField({ label, value, onChange, options }) {
  return (
    <label className={`rdg-field rdg-field-select ${value ? 'filled' : ''}`}>
      <span className="rdg-field-label">{label}</span>
      <select value={value} onChange={e => onChange(e.target.value)}>
        <option value="">—</option>
        {options.map(o => <option key={o} value={o}>{o}</option>)}
      </select>
    </label>
  );
}

// ---------- LEGAL ----------
function Legal({ t }) {
  return (
    <section id="legal" className="rdg-legal">
      <div className="container rdg-legal-inner">
        <div className="rdg-legal-left reveal">
          <span className="eyebrow">{t.legal.eyebrow}</span>
          <h2 className="h-section">{t.legal.title}</h2>
        </div>
        <div className="rdg-legal-right reveal-stagger">
          {t.legal.items.map((item, i) => (
            <div key={item.t} className="rdg-legal-item">
              <h4 className="rdg-legal-t">{item.t}</h4>
              <p className="rdg-legal-d">{item.d}</p>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

Object.assign(window, { Services, About, Cases, Testimonials, Stack, Pricing, FAQ, Legal, Contact });
