/** Section markup of /careers, keyed by CMS section key (markup unchanged from the static page). */
import type { CtaButtonsContent, FeatureCardsContent, FeatureItem, PageHeroContent, TextImageContent } from "@/app/(site)/_lib/pageContent";
import { ButtonRow, Columns, PageHero, Paras, sixColSpan } from "../blocks";
import { SplitTitle } from "../parts";
import type { SectionRenderers } from "../render";

const HL = "text-[#0056D2]";

/** An offerings card: icon, title and bullet list. */
function OfferCard({ it, className = "" }: { it: FeatureItem; className?: string }) {
  return (
    <article className={`offer-card bg-[#0D2959] rounded-[6px] p-[24px_20px_28px]${className}`} data-reveal="">
      {it.icon?.url && (
        <div className="w-[52px] h-[52px] mb-[18px]">
          <img src={it.icon.url} alt="" width="52" height="52" loading="lazy" className="w-[52px] h-[52px] object-contain" />
        </div>
      )}
      <h3 className="text-[14.5px] font-semibold text-white mb-3 leading-[1.35]">{it.title}</h3>
      {!!it.bullets?.length && (
        <ul className="careers-bullet-list careers-offerings-list text-[13px] text-white leading-[1.55]">
          {it.bullets.map((b, i) => (
            <li key={i}>{b}</li>
          ))}
        </ul>
      )}
    </article>
  );
}

export const CAREERS_SECTIONS: SectionRenderers = {
  "page-hero": (c: PageHeroContent) => <PageHero c={c} />,
  introduction: (c: TextImageContent) => (
    <section className="bg-white pt-12 md:pt-16 lg:pt-20 pb-4 md:pb-6 lg:pb-8">
      <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="rounded-[5px] overflow-hidden" data-reveal={side}>
                <img src={c.image.url} alt={c.image.alt ?? ""} className="w-full aspect-[560/500] h-auto block object-cover" width="560" height="340" loading="lazy" />
              </div>
            )}
            text={(side) => (
              <div data-reveal={side}>
                <h2 className="text-[clamp(22px,2.6vw,28px)] font-semibold leading-tight tracking-[-0.01em]">
                  <SplitTitle title={c.title} hl={HL} rest="text-ink" />
                </h2>
                <Paras body={c.body} first="mt-[18px] text-[15px] leading-[1.75] text-[#2E2E2E]" next="mt-4 text-[15px] leading-[1.75] text-[#2E2E2E]" />
                {!!c.features?.length && (
                  <ul className="careers-feature-list mt-8 flex flex-col gap-5">
                    {c.features.map((f, i) => (
                      <li key={i} className="careers-feature-item flex items-start gap-4">
                        {f.icon?.url && (
                          <>
                            <img src={f.icon.url} alt="" width="40" height="40" loading="lazy" className="careers-feature-icon-img shrink-0" />{" "}
                          </>
                        )}
                        <div>
                          <h3 className="text-[15px] font-semibold text-ink leading-[1.35]">{f.title}</h3>
                          {f.description && <p className="mt-1 text-[14px] text-[#2E2E2E] leading-[1.6]">{f.description}</p>}
                        </div>
                      </li>
                    ))}
                  </ul>
                )}
              </div>
            )}
          />
        </div>
      </div>
    </section>
  ),
  offerings: (c: FeatureCardsContent) => {
    // First row: up to four cards; the rest in a narrower centred grid of three
    // (an odd last card spans both columns on tablets), as on the static page.
    const first = c.items.slice(0, 4);
    const rest = c.items.slice(4);
    return (
      <section className="bg-navy-solid pt-4 md:pt-6 lg:pt-8 pb-14 md:pb-20 text-center">
        <div className="w-full max-w-[1180px] mx-auto px-6">
          <div data-reveal="">
            {c.title && (
              <h2 className="text-[clamp(24px,3vw,32px)] font-semibold text-white leading-tight tracking-[-0.01em]">
                <SplitTitle title={c.title} hl={HL} />
              </h2>
            )}
            {c.body && <p className="mt-4 text-[14px] text-white/80 max-w-[720px] mx-auto leading-[1.7]">{c.body}</p>}
          </div>
          <div className="careers-offerings-wrap mt-10 space-y-4 text-left">
            <div className="grid grid-cols-1 sm:grid-cols-2 xl:grid-cols-4 gap-4">
              {first.map((it, i) => (
                <OfferCard key={i} it={it} />
              ))}
            </div>
            {rest.length > 0 && (
              <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4 xl:max-w-[900px] xl:mx-auto">
                {rest.map((it, i) => (
                  <OfferCard key={i} it={it} className={i === rest.length - 1 && rest.length % 2 === 1 ? " sm:col-span-2 lg:col-span-1" : ""} />
                ))}
              </div>
            )}
          </div>
        </div>
      </section>
    );
  },
  "hiring-areas": (c: FeatureCardsContent) => (
    <section className="bg-white py-14 md:py-20 text-center" id="career-areas">
      <div className="w-full max-w-[1180px] mx-auto px-6">
        <div data-reveal="">
          {c.title && (
            <h2 className="text-[clamp(22px,2.6vw,28px)] font-semibold text-ink leading-tight tracking-[-0.01em]">
              <SplitTitle title={c.title} hl={HL} />
            </h2>
          )}
          {c.body && <p className="mt-4 text-[14px] text-[#2E2E2E] max-w-[640px] mx-auto leading-[1.7]">{c.body}</p>}
        </div>
        <div className="careers-hire-grid mt-10 grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-6 gap-5 text-left">
          {c.items.map((it, i) => (
            <article key={i} className={`commitment-card bg-[#f3f5f8] rounded-[6px] p-[28px_24px_30px] ${sixColSpan(i, c.items.length)}`} data-reveal="">
              {it.icon?.url && (
                <div className="w-12 h-12 mb-[18px] flex items-center justify-center">
                  <img src={it.icon.url} alt="" width="48" height="48" loading="lazy" className="w-12 h-12 object-contain" />
                </div>
              )}
              <h3 className="text-[15px] font-semibold text-ink mb-[10px] leading-[1.4]">{it.title}</h3>
              {it.description && <p className="text-sm text-[#2E2E2E] leading-[1.6]">{it.description}</p>}
            </article>
          ))}
        </div>
      </div>
    </section>
  ),
  "how-to-apply": (c: CtaButtonsContent) => (
    <section className="careers-apply-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-semibold text-ink leading-tight mb-[14px]">
          <SplitTitle title={c.title} hl={HL} />
        </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"} font-bold`} />
        </div>
      </div>
    </section>
  ),
};
