import apiClient from "@/lib/api/client";
import type { ApiResponse } from "@/lib/api/types";
import type { ArabicNumeric } from "@/features/shared/apiPrimitives";

/** One admin-managed banner slide. */
export interface BannerImageRecord {
  /** `getimg()` — absolute URL, or null when the row's file is missing. */
  image: string | null;
  banner_url: string | null;
  /** The page it belongs to; null on a legacy home-carousel row. */
  placement: string | null;
}

/**
 * The three headline counts that ride along with the banners.
 *
 * `ArabicNumeric`, not `number`: all three go through `ar_num()`, so under an
 * `ar` session they arrive as digit *strings* — `lib/api/client.ts` rewrites
 * the digits to Western but leaves the type a string. Anything doing
 * arithmetic on them has to coerce first.
 *
 * `volunteer_team_count` is carved out of the organization count rather than
 * added to it: the query excludes volunteer teams from `organization_count`,
 * so the two do not overlap.
 */
export interface PlatformHeadlineStatistics {
  volunteer_count: ArabicNumeric;
  volunteer_team_count: ArabicNumeric;
  organization_count: ArabicNumeric;
}

/**
 * `BaseController::bannerImages()` — the banners **and** the counts in one
 * envelope, which is why this is not two endpoints.
 *
 * Omitting `placement` returns the legacy home carousel; the other pages pass
 * their own. Only currently-visible, non-deleted rows come back, so an empty
 * array is the normal out-of-window case and not an error.
 */
export const getBannerImages = (
  placement?: "home" | "opportunities" | "development" | "events"
): Promise<
  ApiResponse<{
    banner_images: BannerImageRecord[];
    statistics: PlatformHeadlineStatistics;
  }>
> =>
  apiClient
    .get("/banner-images/", { params: placement ? { placement } : undefined })
    .then((r) => r.data);
