/** Public reads for the homepage Slides carousel + Home Events banner sections. */
import { gatewayUrl } from "@/lib/api/gateway";

export interface HomeSlide {
  id: string;
  title?: string | null;
  image?: string | null;
  parameter?: string | null; // link target
  keyword?: string | null;
}

export interface HomeEvent {
  id: string;
  title?: string | null;
  subtitle?: string | null;
  badge?: string | null;
  button_text?: string | null;
  button_url?: string | null;
  background_color?: string | null;
  text_color?: string | null;
  image?: string | null;
}

async function getList<T>(path: string): Promise<T[]> {
  try {
    const r = await fetch(gatewayUrl(path), { next: { revalidate: 30 }, headers: { Accept: "application/json" } });
    if (!r.ok) return [];
    const d = await r.json();
    return Array.isArray(d) ? (d as T[]) : ((d?.items ?? []) as T[]);
  } catch {
    return [];
  }
}

export const fetchSlides = () => getList<HomeSlide>("/v1/slides/");
export const fetchHomeEvents = () => getList<HomeEvent>("/v1/home-events/");
