import FacilitySearch from "./FacilitySearch";
import HeroSlider from "./HeroSlider";
import { API_URL } from "@/config/constants";
import { createPageMetadata } from "@/app/seo";

export const dynamic = "force-dynamic";
export const metadata = createPageMetadata({
  title: "Komplett oversikt over ALLE norske golfbaner",
  description:
    "Utforsk norske golfbaner med oppdatert banestatus, kart, priser, medlemskap og Veien til Golf samlet på TeeOff.",
  path: "/",
});

const getHeroRotationSeed = () => new Date().toISOString().slice(0, 13);

export default async function Home() {
  let facilities = [];

  try {
    const res = await fetch(`${API_URL}/facilities`, {
      next: { revalidate: 0 },
      cache: "no-store",
    });

    if (!res.ok) {
      throw new Error(`API returnerte status ${res.status}`);
    }

    facilities = await res.json();
  } catch (error) {
    console.error("Kritisk feil ved henting av data:", error);
    facilities = [];
  }

  const safeData = Array.isArray(facilities) ? facilities : [];

  return (
    <main className="site-shell min-h-screen">
      <HeroSlider facilities={safeData} rotationSeed={getHeroRotationSeed()} />
      <FacilitySearch initialFacilities={safeData} variant="home" />
    </main>
  );
}
