/**
 * Articles on the website (API reference: "How articles work (website)").
 *
 * Thought Leadership page → category tab → card → /articles/<slug>. The tabs come from
 * GET /public/article-categories, the cards and pagination from GET /public/articles,
 * the article page from GET /public/articles/:slug. The page's "articles" section only
 * holds the list's labels.
 */
import type { Img } from "./content";

export const ARTICLE_BASE = "/articles";
export const ARTICLES_PAGE = "/thought-leadership-articles";
/** Cards per page (the design's 3 × 3 grid). */
export const ARTICLES_PER_PAGE = 9;

export type ArticleCategoryRef = { slug: string; name: string };

/** A card (list item, related article). */
export type ArticleCard = {
  slug: string;
  path: string;
  /** Language of the text served (may be a fallback). */
  language: string;
  title: string;
  excerpt: string;
  category: ArticleCategoryRef;
  coverImage: Img;
  publishedAt: string | null;
  readingMinutes: number;
};

/** GET /public/articles */
export type ArticleList = { items: ArticleCard[]; total: number; page: number; limit: number; totalPages: number };

/** GET /public/article-categories: active categories in admin order; `total` = every article ("All"). */
export type ArticleCategoryList = { total: number; items: (ArticleCategoryRef & { count: number })[] };

/** The article body: a list of blocks (no HTML). Paragraph text is plain. */
export type ArticleBlock =
  | { type: "paragraph"; text: string }
  | { type: "heading"; level: 2 | 3; text: string }
  | { type: "list"; style: "bullet" | "numbered"; items: string[] }
  | { type: "quote"; text: string; source?: string | null }
  | { type: "image"; url: string; alt?: string | null; caption?: string | null };

/** GET /public/articles/:slug (also the admin preview, which has no `related`). */
export type ArticlePageData = ArticleCard & {
  author?: { name: string; role?: string | null } | null;
  body: ArticleBlock[];
  seo?: { title?: string | null; description?: string | null } | null;
  related?: ArticleCard[];
};

export const articleHref = (slug: string) => `${ARTICLE_BASE}/${slug}`;

/** The Thought Leadership list filtered to a category (shareable link). */
export const categoryHref = (slug: string) => `${ARTICLES_PAGE}?category=${encodeURIComponent(slug)}`;

export const isArticleSlug = (s: string) => /^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(s);

/** "24 Nov 2026" (fixed English format, so server and browser render the same text). */
export function articleDate(iso: string | null | undefined): string {
  if (!iso) return "";
  const d = new Date(iso);
  if (Number.isNaN(d.getTime())) return "";
  return `${d.getUTCDate()} ${["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][d.getUTCMonth()]} ${d.getUTCFullYear()}`;
}

/**
 * Page numbers for the pagination, with null for a gap ("…"): the first and last page,
 * the current one and its neighbours (1 2 3 … 10 on page 1, as in the design).
 */
export function pageList(current: number, total: number): (number | null)[] {
  if (total <= 5) return Array.from({ length: total }, (_, i) => i + 1);
  const keep = new Set([1, total, current - 1, current, current + 1]);
  if (current <= 3) [2, 3].forEach((p) => keep.add(p));
  if (current >= total - 2) [total - 1, total - 2].forEach((p) => keep.add(p));
  const pages = [...keep].filter((p) => p >= 1 && p <= total).sort((a, b) => a - b);
  const out: (number | null)[] = [];
  pages.forEach((p, i) => {
    if (i > 0 && p - pages[i - 1] > 1) out.push(null);
    out.push(p);
  });
  return out;
}
