/* ============================================================
   Tectus — Service detail page (CMS-driven, from Services.csv).
   Reads window.SERVICES_DETAIL[slug].
   ============================================================ */
const { Button: SdButton } = window.TectusDesignSystem_c42b34;

const SD_ICON = {
  'executive-protection': 'user-round-check', 'event-security': 'ticket', 'retail-security': 'store',
  'mobile-patrol': 'route', 'alarm-response': 'bell-ring', 'corporate-security': 'building', 'fire-watch': 'flame',
};
const SD_PROVIDE_ICON = ['shield-check', 'clipboard-list', 'activity', 'route'];

function serviceMore(slug) {
  return (window.SERVICES_DETAIL || []).filter((s) => s.slug !== slug).slice(0, 3);
}

function ServiceDetail({ slug }) {
  const svc = (window.SERVICES_DETAIL || []).find((s) => s.slug === slug);
  useEffect(() => {
    if (svc) document.title = svc.title + ' | Tectus';
    return () => { document.title = 'Tectus — Physical security, delivered faster. When it matters most.'; };
  }, [slug]);
  if (!svc) return <NotFound />;
  const icon = SD_ICON[slug] || 'shield';
  const more = serviceMore(slug);

  return (
    <div>
      {/* hero: copy + image */}
      <section style={{ position: 'relative', overflow: 'hidden', borderBottom: '1px solid var(--color-border)' }}>
        <div aria-hidden="true" style={{ position: 'absolute', top: -180, right: -120, width: 560, height: 520, background: 'radial-gradient(circle, rgba(212,175,55,0.08), transparent 64%)', pointerEvents: 'none' }} />
        <div className="wrap tt-pagehero" style={{ display: 'grid', gridTemplateColumns: svc.image ? '1.04fr 0.96fr' : '1fr', gap: 56, alignItems: 'center', padding: '52px 40px 64px', position: 'relative' }}>
          <Reveal>
            <a href="#/services" className="tt-arrow-link" style={{ display: 'inline-flex', alignItems: 'center', gap: 7, fontSize: 13.5, fontWeight: 600, color: 'var(--color-text-muted)', marginBottom: 22 }}><Icon name="arrow-left" size={15} />All services</a>
            <div style={{ display: 'flex', alignItems: 'center', gap: 13, marginBottom: 18 }}>
              <span style={{ width: 52, height: 52, borderRadius: 'var(--radius-md)', background: 'var(--color-primary-soft)', border: '1px solid rgba(212,175,55,0.26)', color: 'var(--color-primary)', display: 'flex', alignItems: 'center', justifyContent: 'center' }}><Icon name={icon} size={25} /></span>
              <span style={{ fontSize: 11, fontWeight: 700, letterSpacing: '0.12em', textTransform: 'uppercase', color: 'var(--color-primary-light)' }}>Services</span>
            </div>
            <h1 style={{ fontFamily: 'var(--font-display)', fontWeight: 800, fontSize: 'clamp(34px, 4.4vw, 54px)', lineHeight: 1.06, letterSpacing: '-0.032em', margin: '0 0 18px', maxWidth: 620, textWrap: 'balance' }}>{svc.title}</h1>
            <p style={{ fontSize: 19, lineHeight: 1.6, color: 'var(--color-text-muted)', maxWidth: 540, margin: 0, textWrap: 'pretty' }}>{svc.short}</p>
            <div style={{ display: 'flex', gap: 12, marginTop: 30, flexWrap: 'wrap' }}>
              <a href="#/get-quote"><SdButton variant="primary" size="lg" iconRight={<Icon name="arrow-right" size={17} />}>Get a quote</SdButton></a>
              <a href="#/book-now"><SdButton variant="secondary" size="lg">Book now</SdButton></a>
            </div>
          </Reveal>
          {svc.image && (
            <Reveal delay={120}>
              <img src={svc.image} alt="" loading="lazy" style={{ width: '100%', aspectRatio: '4 / 3', objectFit: 'cover', borderRadius: 'var(--radius-xl)', border: '1px solid var(--color-border)', display: 'block', boxShadow: 'var(--shadow-lg)' }} />
            </Reveal>
          )}
        </div>
      </section>

      {/* overview body */}
      {svc.body && (
        <section className="wrap" style={{ padding: '64px 40px 24px', maxWidth: 760 }}>
          <Reveal>
            <SectionLabel center={false}>Overview</SectionLabel>
            <div className="tt-prose" style={{ fontSize: 18, lineHeight: 1.7 }} dangerouslySetInnerHTML={{ __html: svc.body }} />
          </Reveal>
        </section>
      )}

      {/* what we provide */}
      {svc.provide.length > 0 && (
        <section style={{ background: 'var(--color-surface)', borderTop: '1px solid var(--color-border)', borderBottom: '1px solid var(--color-border)' }}>
          <div className="wrap" style={{ padding: '80px 40px' }}>
            <Reveal><CenterHead label="What we provide" title={`How Tectus delivers ${svc.title.toLowerCase()}`} /></Reveal>
            <div className="tt-grid-4" style={{ display: 'grid', gridTemplateColumns: `repeat(${Math.min(4, svc.provide.length)}, 1fr)`, gap: 16 }}>
              {svc.provide.map((p, i) => (
                <Reveal key={p.t} delay={i * 70}>
                  <div style={{ padding: 26, background: 'var(--color-bg)', border: '1px solid var(--color-border)', borderRadius: 'var(--radius-lg)', height: '100%' }}>
                    <span style={{ width: 44, height: 44, borderRadius: 'var(--radius-md)', background: 'var(--color-primary-soft)', border: '1px solid rgba(212,175,55,0.26)', color: 'var(--color-primary)', display: 'flex', alignItems: 'center', justifyContent: 'center', marginBottom: 18 }}><Icon name={SD_PROVIDE_ICON[i % SD_PROVIDE_ICON.length]} size={21} /></span>
                    <h3 style={{ fontFamily: 'var(--font-display)', fontSize: 17, fontWeight: 600, margin: '0 0 8px' }}>{p.t}</h3>
                    <p style={{ fontSize: 13.5, color: 'var(--color-text-muted)', lineHeight: 1.55, margin: 0 }}>{p.d}</p>
                  </div>
                </Reveal>
              ))}
            </div>
          </div>
        </section>
      )}

      {/* inclusions + who we serve */}
      <section className="wrap tt-split-2" style={{ padding: '80px 40px', display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 48, alignItems: 'start' }}>
        {svc.inclusions.length > 0 && (
          <Reveal>
            <SectionLabel center={false}>What's included</SectionLabel>
            <h2 style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 'clamp(24px, 2.6vw, 32px)', letterSpacing: '-0.02em', lineHeight: 1.12, margin: '0 0 22px' }}>Every engagement includes</h2>
            <ul style={{ listStyle: 'none', margin: 0, padding: 0, display: 'flex', flexDirection: 'column', gap: 14 }}>
              {svc.inclusions.map((it) => (
                <li key={it} style={{ display: 'flex', gap: 12, alignItems: 'flex-start', fontSize: 15.5, color: 'var(--color-text)', lineHeight: 1.5 }}>
                  <Icon name="check" size={18} color="var(--color-primary)" style={{ marginTop: 1, flex: '0 0 auto' }} />{it}
                </li>
              ))}
            </ul>
          </Reveal>
        )}
        {svc.serve.length > 0 && (
          <Reveal delay={100}>
            <div style={{ background: 'var(--color-surface)', border: '1px solid var(--color-border)', borderRadius: 'var(--radius-xl)', padding: 32 }}>
              <SectionLabel center={false}>Who we serve</SectionLabel>
              <div style={{ display: 'flex', flexDirection: 'column', gap: 12, marginTop: 8 }}>
                {svc.serve.map((s) => (
                  <div key={s} style={{ display: 'flex', gap: 12, alignItems: 'center', padding: '14px 16px', background: 'var(--color-bg)', border: '1px solid var(--color-border)', borderRadius: 'var(--radius-md)' }}>
                    <Icon name="badge-check" size={18} color="var(--color-primary)" style={{ flex: '0 0 auto' }} />
                    <span style={{ fontSize: 14.5, color: 'var(--color-text)' }}>{s}</span>
                  </div>
                ))}
              </div>
            </div>
          </Reveal>
        )}
      </section>

      {/* booking & pricing + trust (shared across all services) */}
      <section style={{ background: 'var(--color-surface)', borderTop: '1px solid var(--color-border)', borderBottom: '1px solid var(--color-border)' }}>
        <div className="wrap" style={{ padding: '80px 40px' }}>
          <div className="tt-split-2" style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 20, marginBottom: 20 }}>
            <Reveal>
              <div style={{ background: 'var(--color-bg)', border: '1px solid var(--color-border)', borderRadius: 'var(--radius-xl)', padding: 32, height: '100%' }}>
                <div style={{ display: 'flex', alignItems: 'center', gap: 11, marginBottom: 14 }}>
                  <Icon name="calendar-clock" size={20} color="var(--color-primary)" />
                  <h3 style={{ fontFamily: 'var(--font-display)', fontSize: 19, fontWeight: 600, margin: 0 }}>Booking requirements &amp; minimums</h3>
                </div>
                <ul style={{ listStyle: 'none', margin: 0, padding: 0, display: 'flex', flexDirection: 'column', gap: 11 }}>
                  {['All services are subject to availability by location.', 'Minimum service durations may apply depending on service type.', 'Advance notice may be required for certain assignments.', 'All bookings are confirmed prior to deployment.'].map((t) => (
                    <li key={t} style={{ display: 'flex', gap: 11, alignItems: 'flex-start', fontSize: 14.5, color: 'var(--color-text-muted)', lineHeight: 1.5 }}><Icon name="check" size={16} color="var(--color-primary)" style={{ marginTop: 1, flex: '0 0 auto' }} />{t}</li>
                  ))}
                </ul>
              </div>
            </Reveal>
            <Reveal delay={100}>
              <div style={{ background: 'var(--color-bg)', border: '1px solid var(--color-border)', borderRadius: 'var(--radius-xl)', padding: 32, height: '100%', display: 'flex', flexDirection: 'column' }}>
                <div style={{ display: 'flex', alignItems: 'center', gap: 11, marginBottom: 14 }}>
                  <Icon name="receipt" size={20} color="var(--color-primary)" />
                  <h3 style={{ fontFamily: 'var(--font-display)', fontSize: 19, fontWeight: 600, margin: 0 }}>Pricing</h3>
                </div>
                <p style={{ fontSize: 15, color: 'var(--color-text-muted)', lineHeight: 1.65, margin: 0 }}>Pricing varies based on location, duration, staffing needs, and service complexity. All pricing is confirmed prior to booking.</p>
                <div style={{ marginTop: 'auto', paddingTop: 22 }}>
                  <a href="#/get-quote"><SdButton variant="secondary" iconRight={<Icon name="arrow-right" size={16} />}>Get a quote</SdButton></a>
                </div>
              </div>
            </Reveal>
          </div>
          <Reveal>
            <div style={{ background: 'var(--color-bg)', border: '1px solid var(--color-border)', borderRadius: 'var(--radius-xl)', padding: 32 }}>
              <div style={{ display: 'flex', alignItems: 'center', gap: 11, marginBottom: 22 }}>
                <Icon name="shield-check" size={20} color="var(--color-primary)" />
                <h3 style={{ fontFamily: 'var(--font-display)', fontSize: 19, fontWeight: 600, margin: 0 }}>Trust &amp; compliance</h3>
              </div>
              <div className="tt-grid-3" style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 16 }}>
                {[
                  { icon: 'badge-check', t: 'Licensed & insured', d: 'Security personnel provided by licensed and insured vendors.' },
                  { icon: 'user-check', t: 'Vetted professionals', d: 'Personnel background-checked and professionally trained by their licensed vendor companies.' },
                  { icon: 'clipboard-check', t: 'Compliant operations', d: 'Services delivered in accordance with regulations.' },
                ].map((b) => (
                  <div key={b.t} style={{ padding: 22, background: 'var(--color-surface)', border: '1px solid var(--color-border)', borderRadius: 'var(--radius-lg)' }}>
                    <span style={{ width: 42, height: 42, borderRadius: 'var(--radius-md)', background: 'var(--color-primary-soft)', border: '1px solid rgba(212,175,55,0.26)', color: 'var(--color-primary)', display: 'flex', alignItems: 'center', justifyContent: 'center', marginBottom: 16 }}><Icon name={b.icon} size={20} /></span>
                    <h4 style={{ fontFamily: 'var(--font-display)', fontSize: 16, fontWeight: 600, margin: '0 0 6px' }}>{b.t}</h4>
                    <p style={{ fontSize: 13.5, color: 'var(--color-text-muted)', lineHeight: 1.5, margin: 0 }}>{b.d}</p>
                  </div>
                ))}
              </div>
            </div>
          </Reveal>
        </div>
      </section>

      {/* related services */}
      {more.length > 0 && (
        <section className="wrap" style={{ padding: '8px 40px 40px' }}>
          <h2 style={{ fontFamily: 'var(--font-display)', fontSize: 24, fontWeight: 700, margin: '0 0 24px', letterSpacing: '-0.015em' }}>Other services</h2>
          <div className="tt-grid-3" style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 20 }}>
            {more.map((m) => (
              <a key={m.slug} href={'#/services/' + m.slug} className="tt-track-card" style={{ display: 'flex', flexDirection: 'column', height: '100%', background: 'var(--color-surface)', border: '1px solid var(--color-border)', borderRadius: 'var(--radius-xl)', overflow: 'hidden', transition: 'border-color var(--dur-base), transform var(--dur-base)' }}>
                <div style={{ position: 'relative', height: 160 }}>
                  {m.image
                    ? <img src={m.image} alt="" loading="lazy" style={{ width: '100%', height: '100%', objectFit: 'cover', display: 'block' }} />
                    : <div style={{ width: '100%', height: '100%', background: 'var(--color-surface-2)' }} />}
                  <span style={{ position: 'absolute', top: 12, left: 12, width: 40, height: 40, borderRadius: 'var(--radius-md)', background: 'rgba(11,11,13,0.72)', backdropFilter: 'blur(6px)', border: '1px solid var(--color-border)', color: 'var(--color-primary)', display: 'flex', alignItems: 'center', justifyContent: 'center' }}><Icon name={SD_ICON[m.slug] || 'shield'} size={19} /></span>
                </div>
                <div style={{ padding: 24, display: 'flex', flexDirection: 'column', flex: 1 }}>
                  <h3 style={{ fontFamily: 'var(--font-display)', fontSize: 17, fontWeight: 600, margin: '0 0 8px' }}>{m.title}</h3>
                  <p style={{ fontSize: 13.5, color: 'var(--color-text-muted)', lineHeight: 1.55, margin: '0 0 14px' }}>{m.short}</p>
                  <span style={{ marginTop: 'auto', display: 'inline-flex', alignItems: 'center', gap: 7, fontSize: 13.5, fontWeight: 600, color: 'var(--color-primary-light)' }}>Learn more <Icon name="arrow-right" size={14} /></span>
                </div>
              </a>
            ))}
          </div>
        </section>
      )}

      <CTABandCenter label="Get started" title={`Request ${svc.title.toLowerCase()} coverage`} sub="Tell us the details and we'll coordinate licensed, vetted vendors for your site." primary="Get Quote" secondary="Book a Demo" />
    </div>
  );
}

window.ServiceDetail = ServiceDetail;
