"use client";

/**
 * HomepageBannerClient — client-side banner for screens that render entirely on
 * the client (authenticated homepage, FAQ, CMS pages).
 *
 * Headline and statistics come from the same `GET /home/` payload as the
 * server-rendered `Banner`, through the shared React Query cache; the slides
 * come from the home placement of `GET /banner-images/` (see `toHeroBanners`).
 * The query key and cached shape are `PlacementBanner`'s, so the two share an
 * entry instead of clashing.
 */
import { useQuery } from "@tanstack/react-query";
import BannerCarousel from "./BannerCarousel";
import { useHomeCms } from "@/features/cms/hooks/useHomeCms";
import { getBannerImages } from "@/features/shared/services/banners";
import { toHeroBanners } from "@/features/home/homeHeroBanners";

export default function HomepageBannerClient({ showStatistics = true }: { showStatistics?: boolean }) {
  const { cms } = useHomeCms();
  const { data: homeBanners } = useQuery({
    queryKey: ["banner-images", "home"],
    queryFn: async () =>
      ((await getBannerImages("home"))?.data?.banner_images ?? []).filter((banner) => Boolean(banner.image)),
    staleTime: 60 * 1000,
  });

  return (
    <BannerCarousel
      showStatistics={showStatistics}
      banners={toHeroBanners(homeBanners)}
      statistics={cms?.statistics ?? null}
      heroTitleEn={cms?.hero?.title_en}
      heroTitleAr={cms?.hero?.title_ar}
    />
  );
}
