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

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

type Phase = RoadmapPhasesContent["items"][number];

/** A phase's text: title, subtitle, divider and its points ("Title – description"). */
function PhaseContent({ p, n, dark }: { p: Phase; n: number; dark: boolean }) {
  return (
    <div className={dark ? "roadmap-phase-content roadmap-phase-content--dark" : "roadmap-phase-content"} data-reveal={dark ? "right" : "left"}>
      <h3 id={`phase-${n}-heading`} className="roadmap-phase-title">
        {p.title}
      </h3>
      {p.subtitle && <p className="roadmap-phase-subtitle">{p.subtitle}</p>} <hr className="roadmap-phase-divider" />{" "}
      <ul className={dark ? "product-arrow-list product-arrow-list--on-dark" : "product-arrow-list"}>
        {p.points.map((pt, i) => (
          <li key={i}>
            <span className="product-arrow-list__text">
              <strong>{pt.title}</strong>
              {pt.description ? ` – ${pt.description}` : null}
            </span>
          </li>
        ))}
      </ul>
    </div>
  );
}

/** A phase's card: period, label and image. */
function PhaseCard({ p, dark }: { p: Phase; dark: boolean }) {
  return (
    <article className={dark ? "roadmap-phase-card roadmap-phase-card--overlay" : "roadmap-phase-card"} data-reveal={dark ? "left" : "right"}>
      <div className={dark ? "roadmap-phase-card-header roadmap-phase-card-header--dark" : "roadmap-phase-card-header"}>
        <span className="roadmap-phase-card-dates">{p.period}</span> <span className="roadmap-phase-card-label">{p.label}</span>
      </div>
      <div className="roadmap-phase-card-image">
        <img src={p.image.url} alt={p.image.alt ?? ""} width="520" height="320" loading="lazy" />
      </div>
    </article>
  );
}

export const ROADMAP_SECTIONS: SectionRenderers = {
  "page-hero": (c: PageHeroContent) => <PageHero c={c} wrap />,
  introduction: (c: TextImageContent) => (
    <section className="roadmap-intro-section">
      <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-12 items-center">
          <Columns
            imagePosition={c.imagePosition}
            image={(side) => (
              <div className="roadmap-intro-image" data-reveal={side}>
                <img src={c.image.url} alt={c.image.alt ?? ""} width="560" height="340" loading="lazy" />
              </div>
            )}
            text={(side) => (
              <div data-reveal={side}>
                <h2 className="text-[clamp(22px,2.6vw,28px)] font-black 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]" />
              </div>
            )}
          />
        </div>
      </div>
    </section>
  ),
  // The heading band, then one band per phase: light ones text first, dark ones card first.
  roadmap: (c: RoadmapPhasesContent) => (
    <>
      <section className="roadmap-heading-section" aria-labelledby="roadmap-phases-heading">
        <div className="w-full max-w-[1180px] mx-auto px-6 text-center" data-reveal="">
          <h2 id="roadmap-phases-heading" className="text-[clamp(22px,2.6vw,28px)] font-black 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>
      </section>
      {c.items.map((p, i) => {
        const n = i + 1;
        const dark = i % 2 === 1;
        return (
          <Fragment key={i}>
            <section className={`roadmap-phase roadmap-phase--${dark ? "dark" : "light"}`} aria-labelledby={`phase-${n}-heading`}>
              <div className="w-full max-w-[1180px] mx-auto px-6">
                {dark ? (
                  <div className="roadmap-phase-grid roadmap-phase-grid--reverse">
                    <PhaseCard p={p} dark />
                    <PhaseContent p={p} n={n} dark />
                  </div>
                ) : (
                  <div className="roadmap-phase-grid">
                    <PhaseContent p={p} n={n} dark={false} />
                    <PhaseCard p={p} dark={false} />
                  </div>
                )}
              </div>
            </section>
          </Fragment>
        );
      })}
    </>
  ),
  "final-words": (c: FeatureCardsContent) => (
    <section className="roadmap-final-section" aria-labelledby="final-words-heading">
      <div className="w-full max-w-[1180px] mx-auto px-6 text-center" data-reveal="">
        {c.title && (
          <h2 id="final-words-heading" className="text-[clamp(22px,2.6vw,28px)] font-black 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-[760px] mx-auto leading-[1.7]">{c.body}</p>}
        <div className="roadmap-final-grid">
          {c.items.map((it, i) => (
            <article key={i} className="roadmap-final-card">
              {it.icon?.url && (
                <div className="roadmap-final-icon" aria-hidden="true">
                  <img src={it.icon.url} alt="" width="48" height="48" loading="lazy" />
                </div>
              )}
              <h3 className="roadmap-final-title">{it.title}</h3>
              {it.description && <p className="roadmap-final-desc">{it.description}</p>}
            </article>
          ))}
        </div>
      </div>
    </section>
  ),
  "leadership-cta": (c: CtaButtonsContent) => (
    <section className="contact-cta py-14 md:py-16 text-center" aria-labelledby="roadmap-cta-heading">
      <div className="w-full max-w-[960px] mx-auto px-6" data-reveal="">
        <h2 id="roadmap-cta-heading" className="text-[clamp(20px,2.5vw,26px)] font-black 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 sm:flex-wrap">
          <ButtonRow ctas={c.ctas} cls={(i) => `${i === 0 ? BTN_PRIMARY : BTN_OUTLINE} w-full sm:w-auto`} />
        </div>
      </div>
    </section>
  ),
};
