/**
 * Home page sections rendered from CMS content. The markup and classes are the
 * static site's, unchanged; only text, links, images and item counts come from
 * the content. Section → design block:
 *
 *   hero → HERO (its background is the .home-top-bg band, see HeroBackground)
 *   proof-of-scale → STATS            trust-badges → HIGHLIGHT CARDS
 *   about / mission → CONTENT BLOCK 1 / 2       solutions → OFFERS
 *   global-impact → SUPPLY FUTURE     strategic-partners → BUSINESS NETWORK
 *   become-partner → PARTNER CTA      case-study → CASE STUDY      contact → CONTACT
 */
import type {
  CaseStudyContent,
  ContactContent,
  CtaBannerContent,
  FeatureCardsContent,
  HeroContent,
  LogoGridContent,
  StatsContent,
  TextImageContent,
} from "@/app/(site)/_lib/content";
import type { ReactNode } from "react";
import { CtaLink, Lines, Title, joined } from "./parts";
import { InquiryForm } from "@/app/(site)/_components/InquiryForm";

const BTN_PRIMARY =
  "btn inline-flex items-center justify-center px-[26px] py-[14px] text-[13px] font-bold tracking-[0.06em] uppercase rounded-[6px] border-[1.5px] border-transparent bg-[#1f63d6] text-white transition-all duration-[180ms] hover:bg-[#194fab] hover:-translate-y-px";

/* ------------------------------------------------------------------ */
/* HERO                                                                */
/* ------------------------------------------------------------------ */

/**
 * The image (or video) band behind the header and hero. Normally it sits at the top of
 * the page and scripts.ts sizes it to header + hero. With `inSection` (hero moved below
 * other sections in the CMS layout) it fills the hero section itself instead.
 */
export function HeroBackground({ background, inSection = false }: { background: HeroContent["background"]; inSection?: boolean }) {
  return (
    <div className="home-top-bg" id={inSection ? undefined : "home-top-bg"} style={inSection ? { bottom: 0 } : undefined} aria-hidden="true">
      {background.type === "video" ? (
        <video
          src={background.url}
          poster={background.posterUrl}
          className="home-top-bg__image"
          width="1280"
          height="540"
          autoPlay
          muted
          loop
          playsInline
          preload="metadata"
        />
      ) : (
        <img
          src={background.url}
          alt=""
          className="home-top-bg__image"
          width="1280"
          height="540"
          decoding="async"
          fetchPriority="high"
        />
      )}{" "}
      <div className="home-top-bg__overlay"></div>
    </div>
  );
}

const HERO_SECONDARY =
  "inline-flex items-center justify-center px-[26px] py-[14px] text-[13px] font-bold tracking-[0.06em] uppercase rounded-[6px] border-[1.5px] border-white/55 bg-transparent text-white transition-all duration-[180ms] hover:bg-white/[0.12] hover:border-white hover:-translate-y-px";

/** `ownBackground`: draw the background inside the section (hero is not the first section). */
export function Hero({ c, ownBackground = false }: { c: HeroContent; ownBackground?: boolean }) {
  return (
    <section className="hero relative overflow-hidden min-h-[min(62vh,560px)] xl:min-h-[min(68vh,640px)] py-14 px-5 pb-20 xl:py-[72px] xl:pb-24 text-center flex items-center justify-center">
      {ownBackground && <HeroBackground background={c.background} inSection />}
      <div className="relative z-[3] max-w-[820px] mx-auto w-full">
        <h1 className="text-[clamp(28px,4.4vw,44px)] font-black text-white leading-tight tracking-[-0.01em]">
          <Title title={c.title} />
        </h1>
        {c.subtitle && (
          <p className="mt-[22px] text-base leading-[1.65] text-[rgba(216,226,242,0.85)] max-w-[680px] mx-auto">
            <Lines text={c.subtitle} />
          </p>
        )}
        {c.ctas.length > 0 && (
          <div className="mt-[34px] flex flex-col items-stretch gap-[14px] md:flex-row md:items-center md:justify-center md:flex-wrap">
            {joined(c.ctas.map((cta, i) => <CtaLink key={i} cta={cta} className={i === 0 ? BTN_PRIMARY : HERO_SECONDARY} />))}
          </div>
        )}
      </div>
    </section>
  );
}

/* ------------------------------------------------------------------ */
/* STATS (proof-of-scale)                                              */
/* ------------------------------------------------------------------ */

export function Stats({ c }: { c: StatsContent }) {
  const n = c.items.length;
  return (
    <section className="bg-white pt-16 pb-8">
      <div className="w-full max-w-[1180px] mx-auto px-6">
        <div className="stats-grid grid grid-cols-2 md:grid-cols-3 xl:grid-cols-5 gap-5 text-center">
          {c.items.map((item, i) => (
            // An odd last item spans both columns of the 2-column (mobile) grid so it centers on its own row.
            <div
              key={i}
              className={n % 2 === 1 && i === n - 1 ? "flex flex-col items-center col-span-2 md:col-span-1" : "flex flex-col items-center"}
              data-reveal=""
            >
              {item.icon && (
                <div className="w-20 h-20 mx-auto mb-5 flex items-center justify-center shrink-0">
                  <img src={item.icon.url} alt={item.icon.alt ?? ""} width="80" height="80" loading="lazy" className="w-20 h-20 object-contain" />
                </div>
              )}
              <div
                className="stat-number text-[26px] font-bold text-[#1f63d6] leading-tight tracking-[-0.01em] transition-opacity duration-[400ms]"
                data-stat={item.value}
              >
                {item.value}
              </div>
              {item.label && <div className="mt-[10px] text-sm text-ink leading-[1.5] max-w-[190px] mx-auto">{item.label}</div>}
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

/* ------------------------------------------------------------------ */
/* HIGHLIGHT CARDS (trust-badges)                                      */
/* The design has no heading or buttons here, so `title` / `ctas` are  */
/* not shown.                                                          */
/* ------------------------------------------------------------------ */

export function TrustBadges({ c }: { c: FeatureCardsContent }) {
  return (
    <section className="bg-white py-10">
      <div className="w-full max-w-[1180px] mx-auto px-6">
        <div className="highlights-grid grid grid-cols-1 md:grid-cols-2 xl:grid-cols-4 gap-[18px]">
          {c.items.map((item, i) => (
            <div key={i} className="highlight-card bg-[#f3f5f8] rounded-[12px] p-[28px_24px_30px]" data-reveal="">
              {item.icon && (
                <div className="w-12 h-12 mb-[18px]">
                  <img src={item.icon.url} alt={item.icon.alt ?? ""} width="48" height="48" loading="lazy" className="w-12 h-12 object-contain" />
                </div>
              )}
              <h3 className="text-[15px] font-bold text-ink mb-[10px] leading-[1.4]">{item.title}</h3>
              {item.description && <p className="text-sm text-[#3c4554] leading-[1.6]">{item.description}</p>}
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

/* ------------------------------------------------------------------ */
/* Text + image blocks (about, mission, global-impact)                 */
/* ------------------------------------------------------------------ */

type TextImageStyle = { section: string; imageBox: string; h2: string; p: string };

const CONTENT_BLOCK: Omit<TextImageStyle, "section"> = {
  imageBox: "rounded-[12px] overflow-hidden border border-[#e2e8f0]",
  h2: "text-[clamp(22px,2.6vw,28px)] font-black text-ink leading-tight tracking-[-0.01em]",
  p: "mt-[18px] text-[15px] leading-[1.7] text-[#2E2E2E] lg:max-w-[480px]",
};
const STYLES: Record<"about" | "mission" | "global-impact", TextImageStyle> = {
  about: { section: "bg-white py-10 pb-[72px]", ...CONTENT_BLOCK },
  mission: { section: "bg-[#f5f6f8] py-10 pb-[72px]", ...CONTENT_BLOCK },
  "global-impact": {
    section: "bg-white py-[72px]",
    imageBox: "rounded-[12px] overflow-hidden",
    h2: "text-[clamp(22px,2.6vw,30px)] font-black text-ink leading-tight tracking-[-0.01em]",
    p: "mt-[18px] text-[15px] leading-[1.75] text-[#2E2E2E]",
  },
};

/**
 * imagePosition "left": image first (also first on mobile).
 * imagePosition "right": text first on desktop; on mobile the image still comes first via order utilities.
 */
export function TextImage({ c, variant }: { c: TextImageContent; variant: keyof typeof STYLES }) {
  const s = STYLES[variant];
  const left = c.imagePosition !== "right";
  const image = (
    <div className={left ? s.imageBox : `order-1 lg:order-2 ${s.imageBox}`} data-reveal={left ? "left" : "right"}>
      <img
        src={c.image.url}
        alt={c.image.alt ?? ""}
        className="w-full aspect-[560/300] h-auto block object-cover"
        width="560"
        height="300"
        loading="lazy"
      />
    </div>
  );
  const text = (
    <div className={left ? undefined : "order-2 lg:order-1"} data-reveal={left ? "right" : "left"}>
      <h2 className={s.h2}>
        <Title title={c.title} />
      </h2>
      <p className={s.p}>
        <Lines text={c.body} />
      </p>
    </div>
  );
  return (
    <section className={s.section}>
      <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">
          {left ? image : text}
          {left ? text : image}
        </div>
      </div>
    </section>
  );
}

/* ------------------------------------------------------------------ */
/* OFFERS (solutions)                                                  */
/* ------------------------------------------------------------------ */

const OFFERS_SECONDARY =
  "inline-flex items-center justify-center px-[26px] py-[14px] text-[13px] font-bold tracking-[0.06em] uppercase rounded-[6px] border-[1.5px] border-white/40 bg-transparent text-white transition-all duration-[180ms] hover:bg-white/10 hover:border-white hover:-translate-y-px";

export function Solutions({ c }: { c: FeatureCardsContent }) {
  return (
    <section className="bg-geometric-navy py-20 text-center">
      <div className="w-full max-w-[1180px] mx-auto px-6">
        {c.title && (
          <h2 className="text-[clamp(24px,3vw,32px)] font-black text-white leading-tight mb-10">
            <Title title={c.title} />
          </h2>
        )}
        <div className="offers-grid grid grid-cols-1 md:grid-cols-3 xl:grid-cols-5 gap-4 text-left">
          {c.items.map((item, i) => (
            <div key={i} className="offer-card bg-[#0D2959] rounded-[12px] p-[24px_20px_28px]" data-reveal="">
              {item.icon && (
                <div className="w-[52px] h-[52px] mb-[18px]">
                  <img src={item.icon.url} alt={item.icon.alt ?? ""} width="52" height="52" loading="lazy" className="w-[52px] h-[52px] object-contain" />
                </div>
              )}
              <h3 className="text-[14.5px] font-bold text-white mb-[10px] leading-[1.35]">{item.title}</h3>
              {item.description && <p className="text-[13px] text-[rgba(216,226,242,0.78)] leading-[1.55]">{item.description}</p>}
            </div>
          ))}
        </div>
        {c.ctas.length > 0 && (
          <div className="mt-11 flex flex-col items-stretch gap-[14px] md:flex-row md:items-center md:justify-center md:flex-wrap">
            {joined(c.ctas.map((cta, i) => <CtaLink key={i} cta={cta} className={i === 0 ? BTN_PRIMARY : OFFERS_SECONDARY} />))}
          </div>
        )}
      </div>
    </section>
  );
}

/* ------------------------------------------------------------------ */
/* BUSINESS NETWORK (strategic-partners)                               */
/* ------------------------------------------------------------------ */

export function StrategicPartners({ c }: { c: LogoGridContent }) {
  return (
    <section className="bg-white py-14 pb-16">
      <div className="w-full max-w-[1180px] mx-auto px-6">
        <h2 className="text-[clamp(20px,2.4vw,26px)] font-black text-ink text-center leading-tight mb-10" data-reveal="">
          <Title title={c.title} />
        </h2>
        {c.logos.length > 0 && (
          <div className="business-network-banner" data-reveal="">
            {c.logos.map((logo, i) => {
              const img = (
                <img
                  key={i}
                  src={logo.image.url}
                  alt={logo.image.alt || logo.name || ""}
                  className="business-network-banner__img"
                  width="1024"
                  height="167"
                  loading="lazy"
                />
              );
              return logo.href ? <CtaLink key={i} cta={{ label: logo.name ?? "", href: logo.href }}>{img}</CtaLink> : img;
            })}
          </div>
        )}
      </div>
    </section>
  );
}

/* ------------------------------------------------------------------ */
/* PARTNER CTA (become-partner)                                        */
/* ------------------------------------------------------------------ */

export function BecomePartner({ c }: { c: CtaBannerContent }) {
  return (
    <section className="bg-gradient-to-br from-[#dde8f8] via-[#e8eef8] to-[#d8e6f5] py-14 text-center" data-reveal="">
      <div className="w-full max-w-[700px] mx-auto px-6">
        <h2 className="text-[clamp(20px,2.5vw,26px)] font-black text-ink leading-tight mb-[14px]">
          <Title title={c.title} />
        </h2>
        {c.body && (
          <p className="text-[15px] text-[#3c4554] mb-7 leading-[1.65]">
            <Lines text={c.body} />
          </p>
        )}{" "}
        <CtaLink cta={c.cta} className={BTN_PRIMARY} />
      </div>
    </section>
  );
}

/* ------------------------------------------------------------------ */
/* CASE STUDY                                                          */
/* First button sits under the text; the rest go in the row below.     */
/* ------------------------------------------------------------------ */

const OUTLINE_NAVY =
  "btn-outline-navy inline-flex items-center justify-center px-[26px] py-[13px] text-[13px] font-bold tracking-[0.06em] uppercase rounded-[6px] border-[1.5px] border-ink bg-transparent text-ink";

export function CaseStudy({ c }: { c: CaseStudyContent }) {
  const [first, ...rest] = c.ctas;
  return (
    <section className="bg-white pt-20">
      <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 className="order-2 lg:order-1" data-reveal="left">
            <h2 className="text-[clamp(22px,2.6vw,30px)] font-black text-ink leading-tight tracking-[-0.01em] mb-4">
              <Title title={c.title} />
            </h2>
            <p className="text-[15px] text-[#2E2E2E] leading-[1.75] mb-[22px]">
              <Lines text={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>
            )}{" "}
            {first && (
              <CtaLink
                cta={first}
                className={`${OUTLINE_NAVY} whitespace-nowrap transition-all duration-[180ms] hover:bg-ink hover:text-white hover:-translate-y-px mx-auto lg:mx-0 w-full max-w-[320px] sm:w-auto sm:max-w-none`}
              />
            )}
          </div>
          <div className="order-1 lg:order-2 rounded-[12px] overflow-hidden border border-[#e2e8f0]" data-reveal="right">
            <img
              src={c.image.url}
              alt={c.image.alt ?? ""}
              className="w-full aspect-[560/300] h-auto block object-cover"
              width="560"
              height="300"
              loading="lazy"
            />
          </div>
        </div>
        {rest.length > 0 && (
          <div
            className="flex flex-col items-center justify-center gap-4 py-16 border-t border-[#eef0f3] mt-14 md:flex-row md:flex-wrap"
            data-reveal=""
          >
            {joined(
              rest.map((cta, i) => (
                <CtaLink
                  key={i}
                  cta={cta}
                  className={
                    i === 0
                      ? `${BTN_PRIMARY} w-full max-w-[320px] sm:w-auto sm:max-w-none text-center`
                      : `${OUTLINE_NAVY} transition-all duration-[180ms] hover:bg-ink hover:text-white hover:-translate-y-px w-full max-w-[320px] sm:w-auto sm:max-w-none text-center whitespace-normal sm:whitespace-nowrap`
                  }
                />
              )),
            )}
          </div>
        )}
      </div>
    </section>
  );
}

/* ------------------------------------------------------------------ */
/* CONTACT (the form is static: no submit endpoint yet)                */
/* ------------------------------------------------------------------ */

const INPUT =
  "w-full px-[14px] py-3 text-sm text-ink bg-white border border-[#d6dbe2] rounded-[6px] outline-none font-[inherit] transition-[border-color,box-shadow] duration-[180ms] focus:border-[#1f63d6] focus:shadow-[0_0_0_3px_rgba(31,99,214,0.12)]";

function InfoRow({ icon, label, value, children }: { icon: string; label: string; value: string; children?: ReactNode }) {
  return (
    <div className="flex items-start gap-[14px]">
      <div className="contact-info-icon w-6 h-6 min-w-[24px] flex items-center justify-center mt-0.5 shrink-0">
        <img src={`/assets/images/contact/${icon}.png`} alt="" width="22" height="22" decoding="async" />
      </div>
      <div className="flex flex-col gap-1 flex-1 min-w-0">
        <span className="text-[13px] text-[#3c4554]">{label}</span> <span className="text-sm text-ink font-bold leading-[1.5]">{value}</span>
        {children}
      </div>
    </div>
  );
}

export function Contact({ c }: { c: ContactContent }) {
  const hasInfo = Boolean(c.phone || c.email || c.address);
  return (
    <section className="bg-white pt-[10px] overflow-x-clip" id="contact">
      <div className="w-full max-w-[1180px] mx-auto px-6">
        <div className="grid grid-cols-1 lg:grid-cols-2 items-stretch [&amp;>*]:min-w-0">
          {/* Info panel */}
          <div className="bg-[#0056D233] p-[28px_22px] md:p-[40px_36px_44px]" data-reveal="left">
            <h2 className="text-[clamp(20px,2.2vw,24px)] font-black text-ink leading-tight mb-[14px]">
              <Title title={c.title} />
            </h2>
            {c.body && (
              <p className="text-sm text-[#3c4554] leading-[1.7] mb-8 max-w-[420px]">
                <Lines text={c.body} />
              </p>
            )}
            {hasInfo && (
              <div className="flex flex-col gap-[22px] mb-9">
                {c.phone && <InfoRow icon="icon-phone" label="Phone Number:" value={c.phone} />}
                {c.email && <InfoRow icon="icon-mail" label="Email:" value={c.email} />}
                {c.address && (
                  <InfoRow icon="icon-location" label="Location:" value={c.address}>
                    {c.mapUrl && (
                      <>
                        {" "}
                        <CtaLink cta={{ label: "View Map", href: c.mapUrl, openInNewTab: /^https?:/i.test(c.mapUrl) }} className="contact-view-map" />
                      </>
                    )}
                  </InfoRow>
                )}
              </div>
            )}
            {c.ctas.length > 0 && (
              <div className="contact-panel-ctas">
                {joined(
                  c.ctas.map((cta, i) => (
                    <CtaLink key={i} cta={cta} className={i === 0 ? "contact-cta-btn contact-cta-btn--primary" : "contact-cta-btn contact-cta-btn--secondary"} />
                  )),
                )}
              </div>
            )}
          </div>
          {/* Form panel */}
          <div className="bg-[#F4F6F8] p-[28px_22px] md:p-[40px_36px_44px]" data-reveal="right">
            {c.formTitle && (
              <h2 className="text-[clamp(20px,2.2vw,24px)] font-black text-ink leading-tight mb-7 text-center">{c.formTitle}</h2>
            )}
            <InquiryForm page="home" className="contact-form flex flex-col gap-[18px]">
              <div className="flex flex-col gap-2">
                <label htmlFor="cf-name" className="text-[13px] font-semibold text-ink">
                  Your Name
                </label>{" "}
                <input type="text" id="cf-name" name="name" placeholder="Enter your name" autoComplete="name" className={INPUT} />
              </div>
              <div className="flex flex-col gap-2">
                <label htmlFor="cf-email" className="text-[13px] font-semibold text-ink">
                  Email Address
                </label>{" "}
                <input type="email" id="cf-email" name="email" placeholder="Enter your email" autoComplete="email" className={INPUT} />
              </div>
              <div className="flex flex-col gap-2">
                <label htmlFor="cf-phone" className="text-[13px] font-semibold text-ink">
                  Phone Number
                </label>{" "}
                <div className="phone-input-wrap flex items-center bg-white border border-[#d6dbe2] rounded-[6px] overflow-hidden transition-[border-color,box-shadow] duration-[180ms]">
                  <span className="phone-prefix px-[14px] py-3 text-sm font-semibold text-[#3c4554] border-r border-[#d6dbe2] whitespace-nowrap shrink-0">
                    +31
                  </span>{" "}
                  <input
                    type="tel"
                    id="cf-phone"
                    name="phone"
                    placeholder="Enter your phone number"
                    autoComplete="tel"
                    className="flex-1 border-none bg-transparent px-[14px] py-3 text-sm font-[inherit] text-ink outline-none"
                  />
                </div>
              </div>
              <div className="flex flex-col gap-2">
                <label htmlFor="cf-message" className="text-[13px] font-semibold text-ink">
                  Write Your Message
                </label>{" "}
                <textarea
                  id="cf-message"
                  name="message"
                  rows={4}
                  placeholder="Write us your question here...."
                  className="w-full px-[14px] py-3 text-sm text-ink bg-white border border-[#d6dbe2] rounded-[6px] outline-none font-[inherit] resize-y min-h-[110px] transition-[border-color,box-shadow] duration-[180ms] focus:border-[#1f63d6] focus:shadow-[0_0_0_3px_rgba(31,99,214,0.12)]"
                ></textarea>
              </div>{" "}
              <button
                type="submit"
                className="btn btn-submit-form inline-flex items-center justify-center mt-2 px-[28px] py-[14px] text-[13px] font-bold tracking-[0.06em] uppercase rounded-[6px] border-[1.5px] border-transparent bg-[#1f63d6] text-white transition-all duration-[180ms] hover:bg-[#194fab] hover:-translate-y-px"
              >
                {" "}
                {c.submitLabel}{" "}
              </button>
            </InquiryForm>
          </div>
        </div>
      </div>
    </section>
  );
}
