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

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

/** A title whose plain text sits outside any span, only the highlight wrapped ("Built To Lead The Future Of <span>…</span>"). */
function InlineTitle({ title }: { title: RichTitle }) {
  const at = title.highlight ? title.text.indexOf(title.highlight) : -1;
  if (!title.highlight || at < 0) return <>{title.text}</>;
  return (
    <>
      {title.text.slice(0, at)}
      <span className={HL}>{title.highlight}</span>
      {title.text.slice(at + title.highlight.length)}
    </>
  );
}

/** Column classes of the comparison table: label column, Unionists column, the others. */
const headClass = (i: number) => (i === 0 ? "compare-th-feature" : i === 1 ? "compare-th-unionists" : "compare-th-traditional");
const cellClass = (i: number) => (i === 1 ? "compare-cell-unionists" : "compare-cell-traditional");

export const WHY_CHOOSE_US_SECTIONS: SectionRenderers = {
  "page-hero": (c: PageHeroContent) => <PageHero c={c} width="max-w-[820px]" h1="text-[clamp(26px,3.8vw,38px)]" />,
  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-[6px] overflow-hidden" data-reveal={side}>
                <img src={c.image.url} alt={c.image.alt ?? ""} className="w-full aspect-[560/340] 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] lg:max-w-[500px]"
                  next="mt-4 text-[15px] leading-[1.75] text-[#2E2E2E] lg:max-w-[500px]"
                />
              </div>
            )}
          />
        </div>
      </div>
    </section>
  ),
  "competitive-edge": (c: ComparisonTableContent) => (
    <section className="bg-white pt-4 md:pt-6 lg:pt-8 pb-12 md:pb-16 lg:pb-20">
      <div className="w-full max-w-[1180px] mx-auto px-6 text-center">
        <div data-reveal="">
          <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-[720px] mx-auto leading-[1.7]">{c.body}</p>}
        </div>
        <div className="compare-table-wrap mt-10 overflow-x-auto" data-reveal="">
          <table className="compare-table w-full min-w-[640px] text-left">
            <thead>
              <tr>
                {c.columns.map((col, i) => (
                  <th key={i} scope="col" className={headClass(i)}>
                    {col}
                  </th>
                ))}
              </tr>
            </thead>
            <tbody>
              {c.rows.map((row, r) => (
                <tr key={r}>
                  {row.cells.map((cell, i) =>
                    i === 0 ? (
                      <th key={i} scope="row" className="compare-row-label">
                        {cell}
                      </th>
                    ) : (
                      <td key={i} className={cellClass(i)}>
                        {cell}
                      </td>
                    ),
                  )}
                </tr>
              ))}
            </tbody>
          </table>
        </div>
      </div>
    </section>
  ),
  "in-action": (c: FeatureCardsContent) => (
    <section className="bg-navy-solid py-14 md: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-semibold text-white leading-tight mb-10" data-reveal="">
            <SplitTitle title={c.title} hl={HL} />
          </h2>
        )}
        <div className="grid grid-cols-1 md:grid-cols-3 gap-4 text-left">
          {c.items.map((it, i) => (
            <article key={i} className="offer-card bg-[#0D2959] rounded-[6px] p-[24px_20px_28px]" 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-[10px] leading-[1.35]">{it.title}</h3>
              {it.description && <p className="text-[13px] text-white/80 leading-[1.55]">{it.description}</p>}
            </article>
          ))}
        </div>
      </div>
    </section>
  ),
  "built-to-lead": (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 data-reveal={side}>
                <h2 className="text-[clamp(22px,2.6vw,28px)] font-semibold leading-tight tracking-[-0.01em]">
                  <InlineTitle title={c.title} />
                </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.stats?.length && (
                  <div className="why-stats-row mt-8">
                    {c.stats.map((s, i) => (
                      <div key={i} className="why-stat">
                        {s.icon?.url && (
                          <>
                            <img src={s.icon.url} alt="" width="48" height="48" loading="lazy" className="why-stat-icon-img" />{" "}
                          </>
                        )}
                        <span className="why-stat-label">{s.label}</span>
                      </div>
                    ))}
                  </div>
                )}
              </div>
            )}
            image={(side) => (
              <div className="rounded-[6px] overflow-hidden" data-reveal={side}>
                <img src={c.image.url} alt={c.image.alt ?? ""} className="w-full aspect-[560/340] h-auto block object-cover" width="560" height="340" loading="lazy" />
              </div>
            )}
          />
        </div>
      </div>
    </section>
  ),
  "partner-cta": (c: CtaButtonsContent) => (
    <section className="contact-cta py-14 md:py-16 text-center">
      <div className="w-full max-w-[800px] 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] sm:flex-row sm:items-center sm:justify-center">
          <ButtonRow ctas={c.ctas} cls={(i) => `contact-cta-btn contact-cta-btn--${i === 0 ? "primary" : "secondary"} font-bold`} />
        </div>
      </div>
    </section>
  ),
};
