/**
 * Section markup of a service page (/services/<slug>), keyed by CMS section key. Markup
 * unchanged from the former /service-details page. Every service has the same sections
 * (API reference, "Service page, every service"); `card` is its card on the Services
 * page and only appears in the admin preview.
 */
import { Fragment } from "react";
import { CtaLink } from "@/app/(site)/_components/home/parts";
import type { CaseStudyContent, CtaImageContent, FeatureCardsContent, PageHeroContent, ProcessStepsContent, ServiceCardContent, TextImageContent } from "@/app/(site)/_lib/pageContent";
import { Columns, PageHero, Paras } from "../blocks";
import { SplitTitle } from "../parts";
import type { SectionRenderers } from "../render";
import { ServiceCardItem } from "../services/sections";

const PRIMARY_BTN =
  "btn inline-flex items-center justify-center px-[26px] py-[14px] text-[11px] font-bold tracking-[0.06em] uppercase rounded-[6px] border-[1.5px] border-transparent bg-[#0056D2] text-white transition-all duration-[180ms] hover:bg-[#004bb8] hover:-translate-y-px";
const P_FIRST = "mt-[18px] text-[15px] leading-[1.75] text-[#3c4554]";
const P_NEXT = "mt-4 text-[15px] leading-[1.75] text-[#3c4554]";

export const SERVICE_SECTIONS: SectionRenderers = {
  /** The service's card as it looks on the Services page (admin preview only). */
  card: (c: ServiceCardContent) => (
    <div className="services-page" style={{ display: "contents" }}>
      <div className="service-cards-section">
        <div className="w-full max-w-[1180px] mx-auto px-6">
          <div className="service-cards-grid service-cards-grid--2">
            <ServiceCardItem card={{ ...c, href: c.href || "#" }} />
          </div>
        </div>
      </div>
    </div>
  ),
  "page-hero": (c: PageHeroContent) => <PageHero c={c} wrap h1="text-[clamp(22px,3vw,34px)]" parents={[{ label: "Products & Services", href: "/product-services" }]} />,
  introduction: (c: TextImageContent) => (
    <section className="bg-white py-12 md:py-16 lg:py-20">
      <div className="w-full max-w-[1180px] mx-auto px-6">
        <div className="grid grid-cols-1 lg:grid-cols-2 gap-7 lg:gap-12 items-center">
          <Columns
            imagePosition={c.imagePosition}
            image={(side) => (
              <div className="product-category-image product-category-image--service-intro" data-reveal={side}>
                <img src={c.image.url} alt={c.image.alt ?? ""} width="560" height="300" loading="lazy" />
              </div>
            )}
            text={(side) => (
              <div data-reveal={side}>
                <h2 className="text-[clamp(22px,2.6vw,28px)] font-black leading-tight tracking-[-0.01em]">
                  <SplitTitle title={c.title} hl="text-[#0056D2]" rest="text-ink" />
                </h2>
                <Paras body={c.body} first={P_FIRST} next={P_NEXT} />
              </div>
            )}
          />
        </div>
      </div>
    </section>
  ),
  "core-innovations": (c: FeatureCardsContent) => (
    <section className="service-innovations-section" aria-labelledby="core-innovations-heading">
      <div className="w-full max-w-[1180px] mx-auto px-6" data-reveal="">
        {c.title && (
          <h2 id="core-innovations-heading" className="text-[clamp(22px,2.6vw,28px)] font-black text-center leading-tight tracking-[-0.01em]">
            <SplitTitle title={c.title} hl="text-[#0056D2]" />
          </h2>
        )}
        <div className="service-innovation-grid">
          {c.items.map((it, i) => (
            <article key={i} className="service-innovation-card">
              {it.icon?.url && (
                <div className="service-innovation-icon" aria-hidden="true">
                  <img src={it.icon.url} alt="" width="48" height="48" loading="lazy" />
                </div>
              )}
              <h3 className="service-innovation-title">{it.title}</h3>
              {it.description && <p className="service-innovation-desc">{it.description}</p>}
            </article>
          ))}
        </div>
      </div>
    </section>
  ),
  process: (c: ProcessStepsContent) => (
    <section className="service-process-section" aria-labelledby="process-heading">
      <div className="w-full max-w-[1180px] mx-auto px-6 text-center" data-reveal="">
        <h2 id="process-heading" className="text-[clamp(22px,2.6vw,28px)] font-black text-ink leading-tight tracking-[-0.01em]">
          <SplitTitle title={c.title} hl="text-[#0056D2]" />
        </h2>
        {c.body && <p className="mt-3 text-[14px] text-[#2E2E2E] max-w-[640px] mx-auto leading-[1.7]">{c.body}</p>}
        <div className="service-process-timeline-track" aria-hidden="true">
          <div className="service-process-timeline-line"></div>
          {c.items.map((_, i) => (
            <Fragment key={i}>
              {" "}
              <span className="service-process-timeline-dot" style={{ left: `${((i + 0.5) * 100) / c.items.length}%` }}></span>
            </Fragment>
          ))}
        </div>
        <div className="service-process-grid text-left">
          {c.items.map((s, i) => (
            <article key={i} className="service-process-card">
              <p className="service-process-step">{s.label}</p>
              <h3 className="service-process-title">{s.title}</h3>
              {s.description && <p className="service-process-desc">{s.description}</p>}
            </article>
          ))}
        </div>
      </div>
    </section>
  ),
  /** ctas[0]: outlined button under the bullets; ctas[1]: centred button below the section. */
  "case-study": (c: CaseStudyContent) => {
    const [caseCta, moreCta] = c.ctas;
    return (
      <section className="service-case-study-section" aria-labelledby="case-study-heading">
        <div className="w-full max-w-[1180px] mx-auto px-6">
          <div className="grid grid-cols-1 lg:grid-cols-2 gap-8 lg:gap-14 items-center">
            <div data-reveal="left">
              <h2 id="case-study-heading" className="text-[clamp(22px,2.6vw,30px)] font-black text-ink leading-tight tracking-[-0.01em] mb-4">
                <SplitTitle title={c.title} hl="text-[#0056D2]" />
              </h2>
              <p className="text-[15px] text-[#2E2E2E] leading-[1.75] mb-[22px]">{c.body}</p>
              {c.bullets.length > 0 && (
                <ul className="case-study-checks flex flex-col gap-3 mb-7">
                  {c.bullets.map((b, i) => (
                    <li key={i} className="text-[14.5px] text-[#3c4554] leading-[1.5]">
                      {b}
                    </li>
                  ))}
                </ul>
              )}{" "}
              {caseCta && (
                <CtaLink
                  cta={caseCta}
                  className="inline-flex items-center justify-center px-[26px] py-[13px] text-[11px] font-bold tracking-[0.06em] uppercase rounded-[6px] border-[1.5px] border-ink bg-transparent text-ink transition-all duration-[180ms] hover:bg-ink hover:text-white hover:-translate-y-px"
                >
                  {" "}
                  {caseCta.label}{" "}
                </CtaLink>
              )}
            </div>
            <div className="rounded-[5px] overflow-hidden border border-[#e2e8f0]" data-reveal="right">
              <img src={c.image.url} alt={c.image.alt ?? ""} className="w-full aspect-[560/380] h-auto block object-cover" width="560" height="380" loading="lazy" />
            </div>
          </div>
          {moreCta && (
            <div className="service-case-study-cta" data-reveal="">
              <CtaLink cta={moreCta} className={PRIMARY_BTN}>
                {" "}
                {moreCta.label}{" "}
              </CtaLink>
            </div>
          )}
        </div>
      </section>
    );
  },
  /** The design has the image on the right; imagePosition isn't used. */
  collaboration: (c: CtaImageContent) => (
    <section className="service-collab-section" aria-labelledby="collab-heading">
      <div className="grid grid-cols-1 lg:grid-cols-2 items-stretch">
        <div className="service-collab-panel" data-reveal="left">
          <h2 id="collab-heading" className="text-[clamp(20px,2.4vw,26px)] font-black text-ink leading-tight tracking-[-0.01em] mb-[14px]">
            <SplitTitle title={c.title} hl="text-[#0056D2]" />
          </h2>
          {c.body && <p className="text-[15px] text-[#3c4554] leading-[1.75] mb-8 max-w-[520px]">{c.body}</p>}
          {c.ctas.map((cta, i) => (
            <Fragment key={i}>
              {" "}
              <CtaLink cta={cta} className={`${PRIMARY_BTN} self-start`}>
                {" "}
                {cta.label}{" "}
              </CtaLink>
            </Fragment>
          ))}
        </div>
        <div className="service-collab-image" data-reveal="right">
          <img src={c.image.url} alt={c.image.alt ?? ""} width="640" height="378" loading="lazy" />
        </div>
      </div>
    </section>
  ),
};
