/**
 * Section markup shared by several inner pages, copied from the static pages.
 * Page-specific variations (colours, spacing, wrapping) are props so each page
 * renders exactly as before.
 */
import { Fragment, type ReactNode } from "react";
import { CtaLink } from "@/app/(site)/_components/home/parts";
import type { Cta, CtaButtonsContent, PageHeroContent } from "@/app/(site)/_lib/pageContent";
import { Breadcrumb, SplitTitle, paragraphs, type Crumb } from "./parts";

export const BTN_PRIMARY =
  "btn inline-flex items-center justify-center px-[22px] 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 text-center";
export const BTN_OUTLINE =
  "btn-outline-blue inline-flex items-center justify-center px-[22px] py-[13px] text-[11px] font-bold tracking-[0.06em] uppercase rounded-[6px] border-[1.5px] border-[#0056D2] bg-transparent text-[#0056D2] transition-all duration-[180ms] hover:bg-[#0056D2] hover:text-white hover:-translate-y-px text-center";

/** A button whose label sits between {" "} as in the static markup. */
export function Button({ cta, className }: { cta: Cta; className: string }) {
  return (
    <CtaLink cta={cta} className={className}>
      {" "}
      {cta.label}{" "}
    </CtaLink>
  );
}

/** Buttons separated by {" "}. */
export function ButtonRow({ ctas, cls }: { ctas: Cta[]; cls: (i: number) => string }) {
  return (
    <>
      {ctas.map((c, i) => (
        <Fragment key={i}>
          {i > 0 && " "}
          <Button cta={c} className={cls(i)} />
        </Fragment>
      ))}
    </>
  );
}

/** Links whose label is their only child (no padding spaces), separated by {" "}. */
export function LinkRow({ ctas, cls }: { ctas: Cta[]; cls: (i: number) => string }) {
  return (
    <>
      {ctas.map((c, i) => (
        <Fragment key={i}>
          {i > 0 && " "}
          <CtaLink cta={c} className={cls(i)} />
        </Fragment>
      ))}
    </>
  );
}

/** "contact-cta-btn" links: the first primary, the others secondary. */
export const contactBtn = (extra = "") => (i: number) => `contact-cta-btn contact-cta-btn--${i === 0 ? "primary" : "secondary"}${extra}`;

/** PAGE HERO: navy band with the page title and breadcrumb. */
export function PageHero({
  c,
  parents = [],
  wrap = false,
  h1 = "text-[clamp(24px,3.4vw,36px)]",
  weight = "font-black",
  width = "max-w-[900px]",
}: {
  c: PageHeroContent;
  /** Breadcrumb items between Home and the current page. */
  parents?: Crumb[];
  /** Breadcrumb list wraps and centres (most pages); off for the oldest pages. */
  wrap?: boolean;
  /** Font-size class of the title. */
  h1?: string;
  /** Font-weight class of the title. */
  weight?: string;
  /** Max-width class of the title block. */
  width?: string;
}) {
  return (
    <section className="page-hero relative overflow-hidden py-[52px] md:py-[64px] px-5 text-center">
      <div className="page-hero-pattern" aria-hidden="true"></div>
      <div className={`relative z-[2] ${width} mx-auto`} data-reveal="">
        <h1 className={`${h1} ${weight} text-white leading-tight tracking-[-0.01em]`}>{c.title}</h1>
        <Breadcrumb wrap={wrap} items={[{ label: "Home", href: "/" }, ...parents, { label: c.breadcrumb }]} />
      </div>
    </section>
  );
}

/** Paragraphs of a body: the first gets `first`, the others `next` (both include the text classes). */
export function Paras({ body, first, next }: { body: string; first: string; next: string }) {
  return (
    <>
      {paragraphs(body).map((p, i) => (
        <p key={i} className={i === 0 ? first : next}>
          {p}
        </p>
      ))}
    </>
  );
}

/** CTA: the light "contact-cta" band with a title, text and 1-3 buttons (first solid, others outlined). */
export function CtaBand({ c, row }: { c: CtaButtonsContent; row: string }) {
  return (
    <section className="contact-cta py-14 md:py-16 text-center">
      <div className="w-full max-w-[960px] mx-auto px-6" data-reveal="">
        <h2 className="text-[clamp(20px,2.5vw,26px)] font-black text-ink leading-tight mb-[14px]">
          <SplitTitle title={c.title} hl="text-[#0056D2]" />
        </h2>
        {c.body && <p className="text-[15px] text-[#3c4554] mb-8 leading-[1.65]">{c.body}</p>}
        <div className={row}>
          <ButtonRow ctas={c.ctas} cls={(i) => (i === 0 ? BTN_PRIMARY : BTN_OUTLINE)} />
        </div>
      </div>
    </section>
  );
}

/**
 * CTA: the light "careers-apply-cta" band with a title, text and 1-3 buttons (first
 * primary, others secondary) that stack until lg.
 */
export function ApplyCta({ c, width = "max-w-[960px]", bold = false }: { c: CtaButtonsContent; width?: string; bold?: boolean }) {
  return (
    <section className="careers-apply-cta py-14 md:py-16 text-center">
      <div className={`w-full ${width} mx-auto px-6`} data-reveal="">
        <h2 className="text-[clamp(20px,2.5vw,26px)] font-semibold text-ink leading-tight mb-[14px]">
          <SplitTitle title={c.title} hl="text-[#0056D2]" />
        </h2>
        {c.body && <p className="text-[15px] text-[#2E2E2E] mb-8 leading-[1.65]">{c.body}</p>}
        <div className="flex flex-col items-stretch gap-[14px] lg:flex-row lg:items-center lg:justify-center">
          <ButtonRow ctas={c.ctas} cls={(i) => `contact-cta-btn contact-cta-btn--${i === 0 ? "primary" : "secondary"}${bold ? " font-bold" : ""}`} />
        </div>
      </div>
    </section>
  );
}

/**
 * Column span of a card in a 6-column grid: rows of three cards (2 columns each); a
 * last row of two cards gets 3 columns each, a single last card the whole row.
 * With 5 cards: 3 + 2, as on the static pages.
 */
export function sixColSpan(i: number, n: number) {
  const rest = n % 3;
  if (i < n - rest) return "lg:col-span-2";
  return rest === 2 ? "lg:col-span-3" : "lg:col-span-6";
}

type Side = "left" | "right";

/**
 * The two columns of a text + image section in the order the CMS asks for
 * (`imagePosition`). Each column gets the matching data-reveal direction, as the
 * static pages had it: the left column slides in from the left.
 */
export function Columns({ imagePosition, image, text }: { imagePosition?: Side; image: (side: Side) => ReactNode; text: (side: Side) => ReactNode }) {
  return imagePosition === "right" ? (
    <>
      {text("left")}
      {image("right")}
    </>
  ) : (
    <>
      {image("left")}
      {text("right")}
    </>
  );
}
