/**
 * Content types of the CMS inner pages (GET /public/pages/:key), as documented in the
 * API's "Section types" reference. The same type has the same fields on every page.
 * All text is plain and rendered escaped.
 */
import type { Background, Cta, Img, PublicLanguage, RichTitle } from "./content";

export type { Background, Cta, Img, RichTitle };

export type PageHeroContent = { title: string; breadcrumb: string };

export type FeatureItem = {
  title: string;
  description?: string;
  bullets?: string[];
  numbered?: boolean;
  details?: { label: string; text: string }[];
  icon?: Img;
};
export type FeatureCardsContent = { title?: RichTitle; body?: string; items: FeatureItem[]; ctas: Cta[] };

export type TextImageContent = {
  title: RichTitle;
  lead?: string;
  body: string;
  stats?: { label: string; icon?: Img }[];
  features?: FeatureItem[];
  ctas?: Cta[];
  image: Img;
  imagePosition: "left" | "right";
};

export type TextImageListContent = {
  title: RichTitle;
  body?: string;
  bullets: string[];
  note?: string;
  ctas?: Cta[];
  image: Img;
  imagePosition: "left" | "right";
};

export type CategoryListContent = {
  title: RichTitle;
  body?: string;
  items: { title: string; entries: { label: string; detail?: string }[]; image: Img; cta?: Cta | null }[];
};

export type ImageCardsContent = {
  title: RichTitle;
  body?: string;
  items: { image: Img; title: string; label?: string; description?: string; cta?: Cta | null }[];
};

/** Text cards around one image: the design shows half of the cards before the image and half after it. */
export type CardsImageContent = { title: RichTitle; body?: string; items: { title: string; description: string }[]; image: Img };

/** `columns` is the header row; each row has one cell per column, the first being the row label. */
export type ComparisonTableContent = { title: RichTitle; body?: string; columns: string[]; rows: { cells: string[] }[] };

export type StatsContent ={ items: { value: string; label?: string; icon?: Img }[] };

/** Cards pinned on a world map; the website places and colours each card by its area. */
export type MapArea = "americas" | "europe" | "middle-east-africa" | "asia-pacific";
export type MapHighlightsContent = { title: RichTitle; body?: string; image: Img; items: { area: MapArea; title: string; description: string }[] };

export type AwardListContent = {
  title: RichTitle;
  body?: string;
  items: { year: string; title: string; badge?: string; description?: string; image?: Img }[];
};

/** `source` is who said it; the website adds the dash. */
export type QuoteCardsContent = { title: RichTitle; body?: string; items: { quote: string; source: string }[] };

export type TestimonialCardsContent = {
  title: RichTitle;
  body?: string;
  items: { quote: string; name: string; role?: string; rating?: number | null }[];
  ctas: Cta[];
};

/** The design alternates the image side, starting with the image on the right. */
export type CaseStudyListContent = {
  title: RichTitle;
  body?: string;
  items: { title: string; body: string; bullets: string[]; image: Img; cta?: Cta | null }[];
  ctas: Cta[];
};

/** The design alternates the image side and the background per phase. */
export type RoadmapPhasesContent = {
  title: RichTitle;
  body?: string;
  items: { label: string; period: string; title: string; subtitle?: string; points: { title: string; description?: string }[]; image: Img }[];
};

/** Labels of the article list; the articles, their categories and pagination come from the articles API. */
export type ArticleListingContent = { heading: string; allFilterLabel: string; noResultsText?: string };

/** Text with an optional featured card beside it (hidden when missing). */
export type TextFeaturedContent = {
  title: RichTitle;
  body: string;
  featured?: { image: Img; title: string; label?: string; description?: string; cta?: Cta | null } | null;
};

/**
 * Heading of a list whose items come from another API (news, campaigns): how many of
 * the latest to show and an optional button (e.g. View All).
 */
export type ListingPreviewContent = { title: RichTitle; body?: string; count: number; cta?: Cta | null; noResultsText?: string };

/** `position` is the office's pin on the page's office-map. */
export type Office = { location: string; name: string; description?: string; position?: { lat: number; lng: number } | null };
export type OfficeListContent = { title: RichTitle; body?: string; items: Office[] };
/** Only the heading: the pins are the offices with a position in the page's office-list. */
export type OfficeMapContent = { title: RichTitle; body?: string };

/** An iframe map: `embedUrl` is its src (Google Maps or OpenStreetMap), `label` its title. */
export type MapEmbedContent = { label: string; embedUrl: string };

export type LogoGridContent = { title: RichTitle; body?: string; logos: { image: Img; name?: string; href?: string }[] };

/**
 * Contact details and an inquiry form. `emails` replaces `email` when present. A form
 * dropdown is hidden when its list is missing; inquiry types show `label` and send `key`.
 */
export type ContactContent = {
  title: RichTitle;
  body?: string;
  phone?: string;
  email?: string;
  emails?: { label: string; email: string }[];
  address?: string;
  mapUrl?: string;
  ctas: Cta[];
  formTitle?: string;
  inquiryTypes?: { key: string; label: string }[];
  meetingDateOptions?: string[];
  meetingTimeOptions?: string[];
  submitLabel: string;
};

export type CtaButtonsContent = { title: RichTitle; body?: string; ctas: Cta[] };
export type CtaImageContent = { title: RichTitle; body?: string; ctas: Cta[]; image: Img; imagePosition: "left" | "right" };
export type CtaBannerContent = { title: RichTitle; body?: string; cta: Cta };

export type ProductCatalogContent = {
  searchPlaceholder: string;
  filtersLabel: string;
  filterHeadings: { category: string; originCountry: string; productType: string; volume: string; certifications: string };
  productTypeLabels: { all: string; organic: string; conventional: string };
  volumeLabels: { upTo10: string; from10To50: string; from50To100: string; from100To500: string; over500: string };
  seeMoreLabel: string;
  noResultsText?: string;
};

/** A timeline of 2-8 steps (service pages). */
export type ProcessStepsContent = { title: RichTitle; body?: string; items: { label: string; title: string; description?: string }[] };

export type CaseStudyContent = { title: RichTitle; body: string; bullets: string[]; image: Img; ctas: Cta[] };

/** A service's card as saved in the CMS (section "card", type service-card). */
export type ServiceCardContent = { image: Img; title: string; description?: string; ctaLabel: string; href?: string };

/**
 * A service card as the website receives it: `href` is where the button goes (the
 * card's own link if set, else `path`, the service's page).
 */
export type ServiceCard = { slug: string; path: string; href: string; language?: string; image: Img; title: string; description?: string; ctaLabel: string };

/** A Services page division: heading and intro from the CMS; `services` added by the API (published cards, in order). */
export type ServiceListingContent = { title: RichTitle; body?: string; services?: ServiceCard[] };

/** GET /public/services */
export type ServiceList = { divisions: { key: string; name: string; services: ServiceCard[] }[] };

/** GET /public/services/:slug. `sections` is empty while the page isn't written. */
export type PublicServicePage = {
  service: { slug: string; division: string; path: string };
  card: ServiceCard;
  language: PublicLanguage;
  sections: PageSection[];
};

/** One section as the website receives it. `content` follows `type`. */
export type PageSection = { key: string; type: string; language?: string; content: unknown };

export type PublicPage = {
  page: { key: string; name: string };
  language: PublicLanguage;
  sections: PageSection[];
};

/** Finds a section's content by key in a list of sections (API or built-in). */
export function sectionContent<T>(sections: PageSection[], key: string): T | undefined {
  return sections.find((s) => s.key === key)?.content as T | undefined;
}
