/* ============================================================
   Tectus — services · how it works · compliance
   ============================================================ */
const { Card: S2Card, Eyebrow: S2Eyebrow, Button: S2Button } = window.TectusDesignSystem_c42b34;

/* ---------- 6. Services ---------- */
const SVC_ITEMS = [
  { slug: 'executive-protection', t: 'Executive Protection', d: 'Professional executive protection for individuals, executives, and high-risk engagements.', img: 'https://framerusercontent.com/images/Naw0JAW2iTrkROKMOJt1RvFL46E.jpeg' },
  { slug: 'event-security', t: 'Event Security', d: 'Reliable, professional security for private, corporate, and public events.', img: 'https://framerusercontent.com/images/BD8PblIINhZwvzAA65yf9NJeS08.jpg' },
  { slug: 'retail-security', t: 'Retail Security', d: 'On-site personnel who deter theft and maintain safety.', img: 'https://framerusercontent.com/images/4ucyOnyNNnGNO5NmEf9LxGRHbTA.jpg' },
  { slug: 'mobile-patrol', t: 'Mobile Patrol', d: 'Scheduled vehicle patrols and site checks.', img: 'https://framerusercontent.com/images/ZLJLne5Li3MsTstyNYPw0kyHaII.jpg' },
  { slug: 'alarm-response', t: 'Alarm Response', d: 'Rapid dispatch to verify and respond to alarm activations.', img: 'https://framerusercontent.com/images/LpbehQ6CwFFGgUq0WMKct5DTk.jpg' },
  { slug: 'corporate-security', t: 'Corporate Security', d: 'Professional security for corporate environments and sensitive situations.', img: 'https://framerusercontent.com/images/BJ6ZfrYVdeqlnoZuBqxqrgj6VKw.jpg' },
];

const SVC_GAP = 20;

function Services() {
  const [idx, setIdx] = useState(0);
  const [cardW, setCardW] = useState(0);
  const [vis, setVis] = useState(3);
  const containerRef = useRef(null);
  const touchX = useRef(null);

  useEffect(() => {
    const measure = () => {
      if (containerRef.current) {
        const w = containerRef.current.offsetWidth;
        const newVis = w < 560 ? 1 : w < 860 ? 2 : 3;
        setVis(newVis);
        setCardW((w - SVC_GAP * (newVis - 1)) / newVis);
        setIdx(i => Math.min(i, SVC_ITEMS.length - newVis));
      }
    };
    measure();
    window.addEventListener('resize', measure);
    return () => window.removeEventListener('resize', measure);
  }, []);

  const svcMax = SVC_ITEMS.length - vis;
  const go = (n) => setIdx(Math.max(0, Math.min(svcMax, n)));

  const onTouchStart = (e) => { touchX.current = e.touches[0].clientX; };
  const onTouchEnd = (e) => {
    if (touchX.current === null) return;
    const dx = e.changedTouches[0].clientX - touchX.current;
    if (Math.abs(dx) > 50) go(dx < 0 ? idx + 1 : idx - 1);
    touchX.current = null;
  };

  const step = cardW + SVC_GAP;

  return (
    <section id="services" style={{ background: 'var(--color-bg)', borderTop: '1px solid var(--color-border)', borderBottom: '1px solid var(--color-border)' }}>
      <div className="wrap" style={{ padding: '96px 40px' }}>
        <Reveal>
          <div style={{ marginBottom: 48 }}>
            <div style={{ marginBottom: 16 }}><S2Eyebrow tick tone="gold">Services</S2Eyebrow></div>
            <div style={{ display: 'flex', alignItems: 'flex-end', justifyContent: 'space-between', gap: 32, flexWrap: 'wrap' }}>
              <div>
                <h2 style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 'clamp(29px, 3.6vw, 44px)', letterSpacing: '-0.025em', lineHeight: 1.08, margin: '0 0 18px', textWrap: 'balance' }}>Security Solutions For Every Need</h2>
                <div style={{ display: 'flex', gap: 10 }}>
                  <a href="#/get-quote"><S2Button variant="secondary" size="md" iconRight={<Icon name="arrow-right" size={16} />}>Get Quote</S2Button></a>
                  <a href="#/get-started"><S2Button variant="primary" size="md" iconRight={<Icon name="arrow-right" size={16} />}>Book Now</S2Button></a>
                </div>
              </div>
              <p style={{ fontSize: 16, color: 'var(--color-text-muted)', lineHeight: 1.6, margin: 0, maxWidth: 340, flexShrink: 0 }}>Across the full range of physical security coverage — each request staffed by licensed, vetted vendors.</p>
            </div>
          </div>
        </Reveal>

        <div style={{ position: 'relative' }}>
          {/* Overflow clip */}
          <div ref={containerRef} style={{ overflow: 'hidden', borderRadius: 'var(--radius-lg)' }}>
            {/* Sliding track */}
            <div onTouchStart={onTouchStart} onTouchEnd={onTouchEnd}
              style={{ display: 'flex', gap: `${SVC_GAP}px`, transform: `translateX(${-idx * step}px)`, transition: cardW > 0 ? 'transform 0.48s cubic-bezier(0.22,1,0.36,1)' : 'none', willChange: 'transform' }}>
              {SVC_ITEMS.map((s) => (
                <div key={s.slug} style={{ flex: `0 0 ${cardW}px`, width: cardW, height: 480, borderRadius: 'var(--radius-lg)', overflow: 'hidden', position: 'relative', flexShrink: 0, background: '#111' }}>
                  <img src={s.img} alt={s.t} loading="lazy" style={{ position: 'absolute', inset: 0, width: '100%', height: '100%', objectFit: 'cover', display: 'block' }} />
                  <div style={{ position: 'absolute', inset: 0, background: 'linear-gradient(to top, rgba(0,0,0,0.88) 0%, rgba(0,0,0,0.38) 50%, rgba(0,0,0,0.06) 100%)' }} />
                  <div style={{ position: 'absolute', bottom: 0, left: 0, right: 0, padding: '24px 26px 28px' }}>
                    <h3 style={{ fontFamily: 'var(--font-display)', fontSize: 22, fontWeight: 700, color: '#fff', margin: '0 0 8px', lineHeight: 1.2 }}>{s.t}</h3>
                    <p style={{ fontSize: 13.5, color: 'rgba(255,255,255,0.72)', lineHeight: 1.5, margin: '0 0 18px' }}>{s.d}</p>
                    <a href={`#/services/${s.slug}`} className="tt-svc-learn" style={{ display: 'inline-flex', alignItems: 'center', gap: 7, fontSize: 13.5, fontWeight: 700, color: 'var(--color-primary)', border: '1.5px solid var(--color-primary)', padding: '9px 18px', borderRadius: 'var(--radius-pill)', textDecoration: 'none', background: 'rgba(0,0,0,0.45)', backdropFilter: 'blur(4px)', transition: 'background 0.2s, color 0.2s' }}>
                      Learn More <Icon name="arrow-right" size={14} />
                    </a>
                  </div>
                </div>
              ))}
            </div>
          </div>

          {/* Arrows — vertically centered always; inside card on mobile, outside on desktop */}
          <button onClick={() => go(idx - 1)} aria-label="Previous"
            style={{ position: 'absolute', left: vis === 1 ? 14 : -22, top: '50%', transform: 'translateY(-50%)', width: 48, height: 48, borderRadius: '50%', background: vis === 1 ? 'rgba(18,16,12,0.82)' : 'var(--color-surface)', border: `1px solid ${vis === 1 ? 'rgba(212,175,55,0.4)' : 'var(--color-border-strong)'}`, display: 'flex', alignItems: 'center', justifyContent: 'center', cursor: idx === 0 ? 'default' : 'pointer', opacity: idx === 0 ? 0.28 : 1, transition: 'opacity 0.2s', zIndex: 10, outline: 'none' }}>
            <Icon name="arrow-left" size={18} color="var(--color-primary)" />
          </button>
          <button onClick={() => go(idx + 1)} aria-label="Next"
            style={{ position: 'absolute', right: vis === 1 ? 14 : -22, top: '50%', transform: 'translateY(-50%)', width: 48, height: 48, borderRadius: '50%', background: vis === 1 ? 'rgba(18,16,12,0.82)' : 'var(--color-surface)', border: `1px solid ${vis === 1 ? 'rgba(212,175,55,0.4)' : 'var(--color-border-strong)'}`, display: 'flex', alignItems: 'center', justifyContent: 'center', cursor: idx === svcMax ? 'default' : 'pointer', opacity: idx === svcMax ? 0.28 : 1, transition: 'opacity 0.2s', zIndex: 10, outline: 'none' }}>
            <Icon name="arrow-right" size={18} color="var(--color-primary)" />
          </button>
        </div>
      </div>
    </section>
  );
}

/* ---------- 7. How it works ---------- */
const STEPS = [
  { n: '01', icon: 'clipboard-list', t: 'Submit request details', d: 'Set time, location, armed or unarmed preference, personnel, and special notes.' },
  { n: '02', icon: 'git-compare-arrows', t: 'Match with vetted vendors', d: 'Your request is matched to licensed vendors with the right credentials.' },
  { n: '03', icon: 'messages-square', t: 'Coordinate through the platform', d: 'Confirm details, message the vendor, and align on requirements in one place.' },
  { n: '04', icon: 'activity', t: 'Track delivery and next steps', d: 'Follow status from assigned to on site to completed — every step logged.' },
];
function HowItWorks() {
  return (
    <section id="how-it-works" className="wrap" style={{ padding: '96px 40px' }}>
      <Reveal><SectionHead center eyebrow="How it works" title="A structured workflow, end to end" sub="Four steps from request to completion — coordinated, accountable, and visible the whole way through." /></Reveal>
      <div className="tt-steps" style={{ display: 'grid', gridTemplateColumns: 'repeat(4, 1fr)', gap: 0, position: 'relative' }}>
        {STEPS.map((s, i) => (
          <Reveal key={s.n} delay={i * 90}>
            <div style={{ padding: '0 26px', borderLeft: i === 0 ? 'none' : '1px solid var(--color-divider)', height: '100%' }}>
              <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 20 }}>
                <span style={{ width: 48, height: 48, borderRadius: 'var(--radius-md)', background: 'var(--color-surface-2)', border: '1px solid var(--color-border)', color: 'var(--color-primary)', display: 'flex', alignItems: 'center', justifyContent: 'center' }}><Icon name={s.icon} size={22} /></span>
                <span style={{ fontFamily: 'var(--font-mono)', fontSize: 13, color: 'var(--color-text-faint)', fontWeight: 500 }}>{s.n}</span>
              </div>
              <h3 style={{ fontFamily: 'var(--font-display)', fontSize: 18.5, margin: '0 0 9px', fontWeight: 600, lineHeight: 1.25 }}>{s.t}</h3>
              <p style={{ fontSize: 14, lineHeight: 1.6, color: 'var(--color-text-muted)', margin: 0 }}>{s.d}</p>
            </div>
          </Reveal>
        ))}
      </div>
    </section>
  );
}

/* ---------- 8. Compliance & trust ---------- */
const PILLARS = [
  { icon: 'file-check', t: 'Licensing validation', d: 'State licenses verified before any vendor joins the network.' },
  { icon: 'shield-check', t: 'Insurance verification', d: 'Active coverage confirmed and kept current for every vendor.' },
  { icon: 'clipboard-check', t: 'Compliance checks', d: 'Armed and unarmed requirements enforced per request.' },
  { icon: 'radar', t: 'Ongoing monitoring', d: 'Credentials and performance reviewed on a recurring basis.' },
  { icon: 'user-plus', t: 'Structured onboarding', d: 'A documented vetting process for every company we approve.' },
];
function Compliance() {
  return (
    <section id="compliance" style={{ background: 'var(--color-surface)', borderTop: '1px solid var(--color-border)', borderBottom: '1px solid var(--color-border)' }}>
      <div className="wrap tt-compliance" style={{ padding: '96px 40px', display: 'grid', gridTemplateColumns: '0.92fr 1.08fr', gap: 60, alignItems: 'center' }}>
        <Reveal>
          <div style={{ marginBottom: 18 }}><S2Eyebrow tick tone="gold">Compliance & trust</S2Eyebrow></div>
          <h2 style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 'clamp(29px, 3.4vw, 42px)', letterSpacing: '-0.025em', lineHeight: 1.08, margin: '0 0 16px', textWrap: 'balance' }}>Built around verification and accountability</h2>
          <p style={{ fontSize: 17, color: 'var(--color-text-muted)', lineHeight: 1.62, margin: '0 0 28px', maxWidth: 460 }}>Tectus is compliance-focused by design. Every vendor is licensed and vetted, every requirement is enforced, and every job is documented.</p>
          <div style={{ display: 'flex', gap: 12, flexWrap: 'wrap' }}>
            <a href="#/vendor-standards"><S2Button variant="gold" iconRight={<Icon name="arrow-right" size={16} />}>Vendor standards</S2Button></a>
            <a href="#/how-it-works"><S2Button variant="secondary">How it works</S2Button></a>
          </div>
        </Reveal>
        <Reveal delay={100}>
          <div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
            {PILLARS.map((p) => (
              <div key={p.t} className="tt-pillar" style={{ display: 'flex', gap: 16, alignItems: 'center', padding: '18px 20px', background: 'var(--color-bg)', border: '1px solid var(--color-border)', borderRadius: 'var(--radius-lg)', transition: 'border-color var(--dur-base)' }}>
                <span style={{ flex: '0 0 auto', width: 44, height: 44, borderRadius: 'var(--radius-md)', background: 'var(--color-primary-soft)', border: '1px solid rgba(212,175,55,0.24)', color: 'var(--color-primary)', display: 'flex', alignItems: 'center', justifyContent: 'center' }}><Icon name={p.icon} size={20} /></span>
                <div>
                  <h3 style={{ fontSize: 15.5, fontWeight: 600, margin: '0 0 3px' }}>{p.t}</h3>
                  <p style={{ fontSize: 13.5, color: 'var(--color-text-faint)', margin: 0, lineHeight: 1.45 }}>{p.d}</p>
                </div>
                <Icon name="check" size={18} color="var(--color-success)" style={{ marginLeft: 'auto', flex: '0 0 auto' }} />
              </div>
            ))}
          </div>
        </Reveal>
      </div>
    </section>
  );
}

Object.assign(window, { Services, HowItWorks, Compliance });
