import type { HeroBanner } from "@/features/shared/types/cms";

/**
 * The home hero's slides, from `GET /banner-images/` (placement `home`).
 *
 * Not `GET /home/` → `hero.banners`: that list ignores the placement the
 * administration chose, so a banner meant for the events or development page
 * also rotated on the home page (BE-290, p.11 «الإدارة تتحكم في … البانرات»).
 * `/banner-images/` filters by placement and defaults to `home`.
 *
 * The endpoint sends no id; the carousel only uses it as a React key, so the
 * position stands in. Rows whose file is missing (`image: null`) are dropped.
 *
 * Newest first, as the hero always showed them (`hero.banners` is
 * `->latest()`): `/banner-images/` has no `ORDER BY` and answers in insertion
 * order, and banners carry no sort field of their own.
 */
export function toHeroBanners(
  images: ReadonlyArray<{ image?: string | null; banner_url?: string | null }> | null | undefined
): HeroBanner[] {
  return [...(images ?? [])]
    .reverse()
    .filter((banner) => Boolean(banner.image))
    .map((banner, index) => ({
      id: index + 1,
      image: banner.image ?? null,
      banner_url: banner.banner_url ?? null,
    }));
}
