/** Section markup of /product-services, keyed by CMS section key (markup unchanged from the static page). */
import type { CategoryListContent, CtaButtonsContent, PageHeroContent, TextImageContent, TextImageListContent } from "@/app/(site)/_lib/pageContent";
import { Button, ButtonRow, Columns, CtaBand, PageHero, Paras } from "../blocks";
import { SplitTitle } from "../parts";
import type { SectionRenderers } from "../render";

const BTN_PRODUCT =
  "btn btn-product 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 text-white transition-all duration-[180ms] hover:-translate-y-px";
const H2 = "text-[clamp(22px,2.6vw,28px)] font-black leading-tight tracking-[-0.01em]";

/** Category blocks alternate: light with the image right, navy with the image left. */
const CATEGORY_IMAGE = ["agricultural", "industrial", "consumer"];

function Categories({ c }: { c: CategoryListContent }) {
  return (
    <>
      {/* CATEGORY HEADER */}
      <section className="bg-white pt-4 md:pt-6 pb-12 md:pb-14 text-center">
        <div className="w-full max-w-[1180px] mx-auto px-6" data-reveal="">
          <h2 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-4 text-[14px] text-[#2E2E2E] max-w-[720px] mx-auto leading-[1.7]">{c.body}</p>}
        </div>
      </section>
      {c.items.map((it, i) => {
        const dark = i % 2 === 1;
        const sectionCls = dark ? "bg-navy-solid py-12 md:py-16 lg:py-20" : i === 0 ? "bg-[#f3f5f8] pb-12 pt-8 md:pb-16 lg:pb-20" : "bg-[#f3f5f8] py-12 md:py-16 lg:py-20";
        const image = (side: string) => (
          <div className={`product-category-image product-category-image--${CATEGORY_IMAGE[i % 3]}`} data-reveal={side}>
            <img src={it.image.url} alt={it.image.alt ?? ""} width="560" height="400" loading="lazy" />
          </div>
        );
        const block = (side: string) => (
          <div className={dark ? "product-category-block product-category-block--dark" : "product-category-block"} data-reveal={side}>
            <h3 className={`text-[clamp(20px,2.4vw,26px)] font-black ${dark ? "text-white" : "text-ink"} leading-tight tracking-[-0.01em]`}>{it.title}</h3>{" "}
            <hr />{" "}
            <ul className={dark ? "product-arrow-list product-arrow-list--on-dark" : "product-arrow-list"}>
              {it.entries.map((e, k) => (
                <li key={k}>
                  <span className="product-arrow-list__text">
                    <strong>{e.label}</strong>
                    {e.detail ? ` (${e.detail})` : null}
                  </span>
                </li>
              ))}
            </ul>
            {it.cta && (
              <div className="mt-8">
                <Button cta={it.cta} className={BTN_PRODUCT} />
              </div>
            )}
          </div>
        );
        return (
          <section key={i} className={sectionCls}>
            <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">
                {dark ? (
                  <>
                    {image("left")}
                    {block("right")}
                  </>
                ) : (
                  <>
                    {block("left")}
                    {image("right")}
                  </>
                )}
              </div>
            </div>
          </section>
        );
      })}
    </>
  );
}

export const PRODUCT_SERVICES_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-6 md: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="product-category-image product-category-image--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={H2}>
                  <SplitTitle title={c.title} hl="text-[#0056D2]" rest="text-ink" />
                </h2>
                <Paras body={c.body} first="mt-[18px] text-[15px] leading-[1.75] text-[#3c4554]" next="mt-4 text-[15px] leading-[1.75] text-[#3c4554]" />
              </div>
            )}
          />
        </div>
      </div>
    </section>
  ),
  categories: (c: CategoryListContent) => <Categories c={c} />,
  logistics: (c: TextImageListContent) => (
    <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--logistics" data-reveal={side}>
                <img src={c.image.url} alt={c.image.alt ?? ""} width="560" height="300" loading="lazy" />
              </div>
            )}
            text={(side) => (
              <div className="product-category-block" data-reveal={side}>
                <h2 className={H2}>
                  <SplitTitle title={c.title} hl="text-[#0056D2]" rest="text-ink" />
                </h2>
                {c.body && <p className="mt-3 text-[14px] text-[#2E2E2E] leading-[1.7]">{c.body}</p>}{" "}
                <hr />{" "}
                <ul className="product-arrow-list">
                  {c.bullets.map((b, i) => (
                    <li key={i}>
                      <span className="product-arrow-list__text">{b}</span>
                    </li>
                  ))}
                </ul>
                {c.note && <p className="mt-4 text-[14px] font-semibold italic text-[#2E2E2E] leading-[1.7]">{c.note}</p>}
                {c.ctas && c.ctas.length > 0 && (
                  <div className="mt-8">
                    <ButtonRow ctas={c.ctas} cls={() => BTN_PRODUCT} />
                  </div>
                )}
              </div>
            )}
          />
        </div>
      </div>
    </section>
  ),
  certifications: (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}
            text={(side) => (
              <div className="product-category-block" data-reveal={side}>
                <h2 className={H2}>
                  <SplitTitle title={c.title} hl="text-[#0056D2]" rest="text-[#2E2E2E]" />
                </h2>
                {c.lead && <p className="mt-3 text-[15px] font-normal text-[#2E2E2E] leading-[1.75]">{c.lead}</p>}{" "}
                <hr />{" "}
                <Paras body={c.body} first="text-[14px] font-normal text-[#2E2E2E] leading-[1.75]" next="mt-4 text-[14px] font-normal text-[#2E2E2E] leading-[1.75]" />
              </div>
            )}
            image={(side) => (
              <div className="product-category-image product-category-image--certifications" data-reveal={side}>
                <img src={c.image.url} alt={c.image.alt ?? ""} width="1024" height="220" loading="lazy" />
              </div>
            )}
          />
        </div>
      </div>
    </section>
  ),
  "business-cta": (c: CtaButtonsContent) => (
    <CtaBand c={c} row="flex flex-col items-stretch gap-[14px] sm:flex-row sm:flex-wrap sm:items-center sm:justify-center" />
  ),
};
