import FacilitySearch from "@/app/FacilitySearch";
import { API_URL } from "@/config/constants";
import {
  createBreadcrumbJsonLd,
  createCollectionPageJsonLd,
  createPageMetadata,
} from "@/app/seo";

export const dynamic = "force-dynamic";

const pageTitle = "Alle golfbaner i Norge";
const pageDescription =
  "Filtrer norske golfbaner etter område, banestatus, antall hull og fasiliteter i TeeOffs samlede oversikt.";

export const metadata = createPageMetadata({
  title: pageTitle,
  description: pageDescription,
  path: "/golfbaner",
});

export default async function GolfCoursesIndexPage() {
  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 golfbaner:", error);
    facilities = [];
  }

  const safeData = Array.isArray(facilities) ? facilities : [];
  const collectionJsonLd = createCollectionPageJsonLd({
    name: pageTitle,
    description: pageDescription,
    path: "/golfbaner",
  });
  const breadcrumbJsonLd = createBreadcrumbJsonLd([
    { name: "Hjem", path: "/" },
    { name: "Golfbaner", path: "/golfbaner" },
  ]);

  return (
    <>
      <script
        type="application/ld+json"
        dangerouslySetInnerHTML={{ __html: JSON.stringify(collectionJsonLd) }}
      />
      <script
        type="application/ld+json"
        dangerouslySetInnerHTML={{ __html: JSON.stringify(breadcrumbJsonLd) }}
      />
      <main className="site-shell min-h-screen">
        <FacilitySearch
          initialFacilities={safeData}
          variant="catalog"
          eyebrow="Golfbaner"
          title="Alle golfbaner samlet på ett sted"
          intro="Bruk område, banestatus og fasiliteter for å snevre inn oversikten. Her får katalogen være arbeidsflate, ikke hero."
        />
      </main>
    </>
  );
}
